Quantcast can any one help me me???? need a c program to open a file using c Text - Physics Forums Library

PDA

View Full Version : can any one help me me???? need a c program to open a file using c


anbhu28
Jul19-08, 04:21 AM
pls can any one send me a c programing code for opening a files

Defennder
Jul19-08, 04:30 AM
You mean you want to read from a file? You can use "fgets" or "fscanf", depending on the file contents you want to read from.

http://en.wikipedia.org/wiki/Fgets
http://en.wikipedia.org/wiki/Fscanf#fscanf

zyh
Jul19-08, 12:26 PM
pls can any one send me a c programing code for opening a files
Any books talk about c language will give you a sample code to do file IO. Also, you can get too many results by Google
http://www.cprogramming.com/tutorial/cfileio.html
http://www.cs.bu.edu/teaching/c/file-io/intro/
......

HallsofIvy
Jul21-08, 09:41 AM
The simplest way to open a file is "fopen(Filename, mode)" where Filename is the file name as a string and mode is string such as "r" for "read only", "w" for "write", "a" for "append" (add to end of file), and a number of others.
That returns a pointer to the open file that you would then use in reading or writing to the file.

For example,
FILE *f= fopen("myFile","r");

makes f a pointer that can be used to read from "myFile".

You could then use "fread(buffer, 10,10, f)" to read up to 10 bytes from the file to the variable "buffer" (which you would declare as a pointer to the correct type of variable) or you could use fscanf(f, ...) to read a formatted string just as you would with "scanf".