In this tutorial we will be covering the basics of file reading and writing in C.
We will be using the following:
Functions: fopen, fread, fseek fclose, fgetc, malloc, fwrite and fprintf.
We will also be using simple maths and variables.
Contents:(

= done,

= not done,

= working on it)
File Input (Reading)1. Printing an entire file. [
]2. Reading x to y bytes from a file. [
]3. Searching to a certain point. [
][/list]
File Output (Writing)1. Writing a block of data to a file. [
]2. Writing formatted data to a file. [
][/list]
General file examples and samples (wiki)Print file size - Print a file size
Get configuration - Read a file, search for a string and return whats after '=', great for use with configuration files!
[/list]
---------------------------------------------------------------------------
If you have read these tutorials and found them helpful, please comment on them in this thread or send an email to me at: harleyg {AT} linuxlabs.co.uk ({AT} = @).
Also, if you have read these, you will be able to combine certain things, eg. searching to a certain point in a file and writing to it, writing and then reading, putting the contents of a file into an array, reading a file as config, saving users settings into a config file etc. The possibilities are endless!
---------------------------------------------------------------------------
Please note: Any content writen on this site by harleyg, unless stated otherwise belongs to me. You are forbiden to modify, copy or distribute it in any form. If found, credited or not on sites outside the LinuxLabs network or the
http://psp-programming.com domain, action
will be taken against you. Modification of the content is also prohibited without the authors consent. That is all.
---------------------------------------------------------------------------
File Input (Reading) - 1. Printing an entire fileWhat we will learn: How to read an entire file into the memory, printing the contents and then terminating.
First we will need to make a few includes, so open up your favourite text editor (vim

) and create a new file, with the extenstion of .c, eg. foo.c. Now inside the text editor, type:
#include <stdio.h>
#include <stdlib.h>
These includes do the following:
stdio.h: "standard input-output header," is the library in C programming language which contain functions for manipulating standard input and output.
stdlib.h: "general purpose standard", is the library of C programming language which includes functions involving memory allocation, process control, conversions and others.
Ok, "enough about includes already" i see you saying
Now start your main function:
int main(void) {
int, meaning interger is used to refer to any data type which can represent some subset of the mathematical integers.
void, is used because in earlier versions of C, functions with no specific result defaulted to a return type of "int" and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to "char".
{, is used to show that the function has started!
Next we will declare some variables:
FILE * pFile;
long lSize;
char * buffer;
FILE, we use this to make the pFile variable a "file pointer".
long, now this is tricky as int and long int both can be from -2,147,483,648 to +2,147,483,647. I would normally use int, but im just copying this example from a cool site and explaining it to you in english, i hope

Oh, and an
int is an explained above!
char, we use this to declare the variable "buffer" contains characters (a-z).
Next we give the pFile variable some information:
pFile = fopen ( "myfile.txt" , "rb" );
pFile, this is a variable, already made into a "file pointer" above.
=, this means foo IS bar (foo = bar).
fopen, this is a function in c that is used to open a file (duh). In requires
2 parameters, the "filename" and "mode".
"myfile.txt", this is the first parameter, the "filename".
rb, this contains 2 modes used:
r: Open a file for reading. The file must exist.
-
b: Binary mode. End of file is reached at last byte of the file. No conversions.[/list]
So now we need to check if the file has any data inside, we use the following:
if (pFile==NULL) exit (1);
if, this is an IF STATEMENT, it can be used to compair things, eg. if foo is bar etc.
( +
), this is to show this is where i start, and this is where i close.
==, the
== means
equal to, unlike =, which means the same as.
NULL, this is the same as empty, or "nothing".
exit (1), this terminates the process. It also has the number "1", so we can see what point it terminates at.
So, this entire if statement means in english:
if pFile is empty, terminate/quite
Next, we are going to check the file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
fseek, "Reposition stream's position indicator." This requires 3 parameters, the pointer to an open file, the offset (how many bytes from the origin) and the origin.
( +
), this is to show this is where i start, and this is where i close.
pFile, this is the pointer to an open file which we declared earlia.
0, this is the offset, it is saying we want it to be exactly on the origin.
SEEK_END, we are saying here that we want the offset to be at the end of the file.
lSize = ftell (pFile);, this is saying lSize IS ftell (pFile).
ftell(pFile);, "Return the current position in a stream". ftell only requires one parameter, a pointer to an open file, in this case it is pFile. ftells main function is to tell you how many bytes until the begining from the position you are at.
rewind (pFile);, rewinds function is to reopen the file back to the begining. It also only requires one parameter, which is a pointer to an open file.
So, how we find out the file size with this is:
Open the file, seek the end, then tell how many bytes there are until the begining, so if the file is 10 bytes, it will seek to the end, and count back until the begining, which is 10 bytes. The file size is now in the variable lSize.
Next we need to free lSize amount of memory to fit the file in (lSize being the size of the file).
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
buffer =, saying buffer IS foo.
( +
), this is to show this is where i start, and this is where i close.
char *, this is telling malloc that it contains characters.
malloc, this is a function to allocate foo memory. eg, malloc(56); will make sure there is 56 bytes of memory for you to store information in. So malloc (lSize) is allocating the the file size.
if (buffer == NULL) exit (2);, (ive already explained these, refer above for information).
Ok, so now we have successfully opened a file, checked its file size and allocated it in the memory.
Next, we copy the file contents into the buffer (memory).
fread (buffer,1,lSize,pFile)
fread, " Read block of data from a stream". This function requires 4 parameters.
buffer, the first parameter, the "buffer", which coincidently is "buffer", by buffer, i mean location to read it to.
1, this is the second parameter, the "size". This is the size in bytes of each item to be read.
lSize, this is the third parameter, "count". This is the amount of bytes to read into the buffer, in this case its lSize, the file size.
pFile, the fourth and final parameter "stream". This is the pointer to an open file, being pFile.
Ok, so now we have just read the file into the memory.
Now, we need to close the file as it is not needed anymore!
fclose (pFile);
fclose (pFile);[/close], "flushes any buffers maintained for the given file and closes the file". I dont really need to explain it, do i?
( + ), this is to show this is where i start, and this is where i close.
Next we are going to print the buffer/file contents to the terminal.
printf("%s\n", buffer);
printf, "Prints to standard output (stdout) a sequence of arguments formatted as the format argument specifies". Basically, it can print things!
%s, it is saying that its a variable, and its also a string (characters etc).
\n, new line.
"%s\n", buffer, %s refers to a variable, but where do we declare it? When we type buffer after the closed "" we are saying %s = buffer. If we had for example:
"Variable 1: %s, Variable 2: %s\n", foo, bar
we would be saying the 1st %s is foo and the 2nd %s is bar.
( +
), this is to show this is where i start, and this is where i close.
Next, we free the memory we allocated.
free (buffer);
Its pretty obvious, but ill explain.
free, this is a function used to un-allocate memory you allocated. It requires one parameter.
( +
), this is to show this is where i start, and this is where i close.
We now return 0. This shows everything ran fine if it got to this point.
return 0;
Close the main function.
}
Here is what your code
should look like.
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * pFile;
long lSize;
char * buffer;
pFile = fopen ( "myfile.txt" , "rb" );
if (pFile==NULL) exit (1);
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
fread (buffer,1,lSize,pFile);
fclose (pFile);
free (buffer);
return 0;
}
We should also have something inside myfile.txt, try this:
I CAN READ FILES IN C!
Now, use a compiler (gcc) to compile it:
gcc foo.c
(replace foo with your filename)
Now you should be given a binary. Create a file called myfile.txt and type anything inside it. Now, run the binary and it will print the contents.
Congratulations, you just read a file in CI will write up more tutorials when i can be assed, post here if theres any errors or you need help!