|
|
 |
|
 |
|
|
C++ Language Tutorial Page 9
Reading From a File
Ned Bingham
Previous Index Next
Files
The C language actually has a better way to access files than C++ does, and both work
in C++. To access a file, you need to create a file pointer. Then, you need to call the fopen function.
// FILE *fopen(filename, access)
FILE *file;
file = fopen("test_file.txt", "w");
The function fopen opens the file specified by the first string input
("test_file.txt" in this case) with the permissions specified by the second string input
("w" in this case). Then it creates a FILE object and assigns it's address to the file variable.
If the file variable is NULL, then the function failed, so you should probably exit.
if (file == NULL)
{
printf("Error: failed to open file: %s", "test_file.txt");
return;
}
If the filename does not begin with a slash (linux and unix) or
backslash (windows), then the function looks in the directory which contains the program.
Otherwise, the function looks in the root directory. Each directory that you want to navigate
through is then separated with a slash or backslash.
Here is a list of the various possible permissions and what they do:
w - opens a file for writing. If the file already exists, it will be erased. If not, it will be
created
w+ - opens a file for reading and writing. If the file already exists, it will be erased. If not, it will be
created
r - opens a file for reading. If the file doesn't already exist, it will return NULL.
r+ - opens a file for reading and writing. If the file doesn't already exist, it will return NULL.
a - opens a file for appending. Written data is added to the end of a file. If the file doesn't exit,
then it is created.
a+ - opens a file for appending and reading. Written or read data is added to or read from to the end of a
file.
Whenever you open a file, the computer is allocating memory for that file pointer,
so you must close the file after you are done using it. This can happen at the end of the program if
you want, just as long as it is closed. The function to close a file is as follows:
fclose(file);
Two main functions are used to read from and write to files. The function fprintf
prints to a file, while the function fgets gets a string from a file. I figure, the best way to explain
fprintf is by example.
for (int x = 0; x < 5; x++)
fprintf(file, "Yay I'm a string!! and I have been printed %d times!", x);
These two lines of code will print:
Yay I'm a string!! and I have been printed 0 times!
Yay I'm a string!! and I have been printed 1 times!
Yay I'm a string!! and I have been printed 2 times!
Yay I'm a string!! and I have been printed 3 times!
Yay I'm a string!! and I have been printed 4 times!
to a file. Notice how the value of x was substituted in for %d. You may also substitute other variable
types in. For example, %f is a float, %c is a char, and %s is a string. You may include as many variables
into the function as you want, just as long as there is a place for them to go. The first included variable
is substituted in for the first % found, the second for the second, and on.
To read a string from a file, use the function fgets(char *str, int num, FILE *fileptr) where str is a
pointer to the character array to be written to, num is the maximum number of characters to read from
the file, and fileptr is the file you want to read from. Each time fgets is called, it reads until it reaches
a \n, r, end of file, or num. Then it increments to the next line so that the next time it is called, it doesn't
read the same line.
After you have read the string, sscanf(char *str, char *format, ...) can be used to scan for specific values.
For example, say the line contains vertex information for a 3D model as such:
v: 2.637 7.256 8.251
and you read that line into a character array and you now want to extract the three numbers. sscanf is very
similar to printf, and fprintf. Extracting the three numbers from the a character array call str would be a
simple call to
sscanf(str, "v: %f %f %f", &x, &y, &z);
You can check whether sscanf has found all the values because it returns the number of values it finds that fit
that format, in this case, three.
To check whether you have reached the end of the file you use feof(FILE *fileptr) which returns a boolean that is
true if the end of the file has been reached. A simple while loop implementation works for reading the entire file.
FILE *ptr = fopen("texfile.txt", "r+");
if (ptr == NULL)
return;
char str[256];
float x, y, z;
while (!feof(ptr))
{
if (fgets(str, 256, ptr) == NULL)
break;
if (sscanf(str, "v: %f %f %f", &x, &y, &z) == 3)
addvert(x, y, z);
}
fclose(ptr);
|
|
|
|
|
|