Text files in C
In this chapter we're going to cover how we read from and write to text files using C. We will cover a few new functions:
All these functions are in < stdio.h > , so we only need to include that header file for any of these functions to work. Saying that, when working with text files we'll have to manipulate strings, functions for working with strings are contained within
We use fopen() to open a file. This function requires two parameters, the first is the name of the file we wish to open, and the second is the "mode" we want to use to open this file. The three main modes are "r", "w" and "a" -- read, write and append.
The function returns a file pointer we can then use to access the file, in this case f.
Firstly we declare a pointer to use with the file operations, declaring it as type "FILE" tells C this pointer will be used with a file. We then define our integer to use in the program.f = fopen("file.txt", "w");
This line opens a file named "file.txt" for writing (w). "w" is a destructive write mode, meaning if the file doesn't exist already, it is created; if it already exists it is destroyed and a blank copy put in it's place for the program to use. The file is opened and assigned to the pointer.
Now we test if the file opening was successful. If f is not true, return an error from main(), halting and exiting the program, as we can't open the file, there's no point going on.
Now we use a for() loop to initialise our variable at 1 and loop through over and over until the value is no longer less than 11 (or until it reaches 10). The fprinf() function should look familiar. It's similar to printf() only it has an extra parameter before the parameter that contains what we wish to print. This parameter contains the pointer to the file we wish to print to.fclose(f);
We then close the file using fclose() and finish the program.
Reading From a File
To read a file, open it with "r" mode. Now this is the first time we've used strings with C. Previously we have only used integers and single characters. To work with strings in C we store them in an array.
Compile that and run it, make a file named "file.txt" in the same directory as the executable and write a few lines to it. The run the executable and observe, it prints out the lines in the file to the screen.char string;
In this line we declare an array named "string" that has 1000 items. It is an array of char variables, we use this to extract the data from the file in string form.
This is the code that does the work. Let's slow down, look at this and ensure we fully understand it.
Remember the while() function? While a condition is true, execute the code between the curly brackets.fgets(string, 1000, f)
This is the new part, we are using the fgets() function, which as said above, reads a string from a file. The first parameter is our array, in which we want to put a line from the file. The second is the maximum number of characters we allow our program to read for each line, it's unlikely a line will be more than 1000 characters long. The last parameter is the file pointer, f.
When we use fgets() we read one line of the file into our array. If we put this in a while() function, it keeps looping over and over, reading each line in, while there are more lines to read in the file, it will keep looping, until it reads in each line of the file one by one, using printf() to print each line out. This happens so fast it looks as if the program read the entire file.
Observe in the printf() function. We didn't include a newline (n) character after we print out our array, this is because fgets reads the line in complete with its newline character, so we don't need to add one. We also used to %s sign to signify we are dealing with string output.
Remember, programming isn't hard, it's logic -- everyone can solve logic. It's learning a programming language that takes time and practice. You have to sit and practice, and write these programs out until you know the functions, and know what they do. Soon you will be thinking up your own programs, stick with it.
No Comments for this page.