C program to open a file

  • Thread starter Thread starter anbhu28
  • Start date Start date
  • Tags Tags
    File Program
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
3 replies · 3K views
anbhu28
Messages
1
Reaction score
0
can anyone help me me? need a c program to open a file using c

pls can anyone send me a c programing code for opening a files
 
Physics news on Phys.org


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".