C File Io
C File Io
Register
Sign In
SOURCE CODE
Page Info
DOWNLOADS
DOCUMENTATION
DISCUSSIONS
In this tutorial, you'll learn how to do file IO, text and binary, in C, usingfopen,fwrite,
andfread,fprintf,fscanf,fgetcandfputc.
FILE *
ISSUES
PEOPLE
Follow 1
SYSTEM REQUIREMENTS
For example:
FILE*fp;
fopen
FILE*fopen(constchar*filename,constchar*mode);
In the filename, if you use a string literal as the argument, you need to remember to use double backslashes
rather than a single backslash as you otherwise risk an escape character such as \t. Using double backslashes \\
escapes the \ key, so the string works as it is expected. Your users, of course, do not need to do this! It's just the
way quoted strings are handled in C and C++.
fopen modes
The allowed modes for fopen are as follows:
ropenforreading
wopenforwriting(fileneednotexist)
aopenforappending(fileneednotexist)
r+openforreadingandwriting,startatbeginning
w+openforreadingandwriting(overwritefile)
a+openforreadingandwriting(appendiffileexists)
Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file
specified by the user, and that file might not exist or it might be writeprotected. In those cases, fopen will
return 0, the NULL pointer.
Here's a simple example of using fopen:
FILE*fp;
fp=fopen("c:\\test.txt","r");
This code will open test.txt for reading in text mode. To open a file in a binary mode you must add a b to the end
of the mode string; for example, "rb" for the reading and writing modes, you can add the b either after the plus
sign "r+b" or before "rb+"
Subscribe
For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed.
You can think of it as the memory address of the file or the location of the file.
To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you
can use the FILE pointer to let the compiler perform input and output functions on the file.
LICENSE
fclose
When you're done working with a file, you should close it using the function
intfclose(FILE*a_file);
fclose returns zero if the file is closed successfully.
An example of fclose is
fclose(fp);
size_tfwrite(constvoid*ptr,size_tsize_of_elements,size_tnumber_of_elements,FILE
*a_file);
Both of these functions deal with blocks of memories usually arrays. Because they accept pointers, you can also
use these functions with other data structures; you can even write structs to a file or a read struct into memory.
Let's look at one function to see how the notation works.
fread takes four arguments. Don't be confused by the declaration of a void *ptr; void means that it is a pointer
that can be used for any type variable. The first argument is the name of the array or the address of the structure
you want to write to the file. The second argument is the size of each element of the array; it is in bytes. For
example, if you have an array of characters, you would want to read it in one byte chunks, so size_of_elements is
one. You can use the sizeof operator to get the size of the various datatypes; for example, if you have a variable
int x; you can get the size of x with sizeofx;. This usage works even for structs or arrays. E.g., if you have a
variable of a struct type with the name a_struct, you can use sizeofa_struct to find out how much memory it is
taking up.
e.g.,
sizeof(int);
The third argument is simply how many elements you want to read or write; for example, if you pass a 100
element array, you want to read no more than 100 elements, so you pass in 100.
The final argument is simply the file pointer we've been using. When fread is used, after being passed an array,
fread will read from the file until it has filled the array, and it will return the number of elements actually read. If
the file, for example, is only 30 bytes, but you try to read 100 bytes, it will return that it read 30 bytes. To check to
ensure the end of file was reached, use the feof function, which accepts a FILE pointer and returns true if the end
of the file has been reached.
fwrite is similar in usage, except instead of reading into the memory you write from memory into a file.
For example,
FILE*fp;
fp=fopen("c:\\test.bin","wb");
charx[10]="ABCDEFGHIJ";
fwrite(x,sizeof(x[0]),sizeof(x)/sizeof(x[0]),fp);
Last edited Sep 2 at 4:55 AM by vineetchoudhary, version 1
20062015 Microsoft
Get Help
Privacy Statement
Terms of Use
Code of Conduct
Advertise With Us
Version 8.21.2015.21031