Can any one help me me? need a c program to open a file using c

  • Thread starter Thread starter anbhu28
  • Start date Start date
  • Tags Tags
    File Program
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
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
 
on Phys.org
anbhu28 said:
pls can anyone 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/
...
 


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