File Input (Reading) - 3. Searching to a certain point. Ok, so the basic idea for this tutorial is searching to a certain point inside a string, or file.
For instance, your file contains "bla;blah", you can search to the ";" and print everything after it, which would be "blah".
This can be useful when reading a file into an array etc...
So first we are going to open up testfile.txt in our favourity text edit which by now should be vim

Inside this file we are going to have the example i just gave:
this is not usefull;super useful!
Now we are going to open up main.c, so go ahead and create/open up main.c.
Lets include some files:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
I shant go on about the includes anymore, you should know them by now.
start the main function:
int main() {
You should by now, if you have been following these tutorials what this means.
We are now going to declare some variables:
char c, *buffer;
int n = 0, x, size;
FILE *pFile;
,: We can use a simple comma to declare multiple things on one line, eg. "int foo, bar".
[200]: This shows that the variable, in this case being "buffer" can store up to 200 bytes of data.
The rest should be easy for you to understand.
We are now going to give the variable "pFile" a function.
pFile = fopen("test.txt", "rb");
So now, we have opened our file.
Now we check the file, too see if it opened correctly.
if(file == NULL) perror("Error opening file!");
perror, " Print error message". In other words, if the file is empty, print an error.
NULL, this is another way of saying empty, or 0... its "null".
We next use "else".
else {
In simple terms, this if and else at the moment is saying "if file is empty, print error, else do....", if its not, do foo.
Ok, now what? We know are going to check we are not at the end of the file in a loop, as we will be reading every char in the file searching for ";"..
while(c != EOF) {
!=, aka, is not... "while c is not EOF do foo..."
EOF, "End of File".
So this is doing "while c is not the end of the file do foo".
Next, we declare c.
c = fgetc(pFile);
fgetc, Get the next character from a stream". This will be explained later.
Now we are going to check if the character is ";".
if(c == ';') {
x = ftell(pFile);
}
This is saying, if c, as declared above to be the next char is the same as ";", x = ftell(pFile). ftell(pFile) will be the length of bytes from where you are to the start of the file. This means, x now has the byte where the ";" is located.
Close the while loop:
}
So, what have we done so far?
In english:
"Opened a file, and searched through it byte by byte to file the character ";", if we have found it we set a variable with its location on it (bytes in)".
We are now going to find out the file size.
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
rewind(pFile);
I have already explained this code in my last tutorial(s).
We also need to free the memory for the file to go into.
buffer = (char*) malloc(size-x);
if(buffer == NULL) perror("Error allocating memory!");
size-x, the original size minus the location of the char ";" is how much data is left in the file.
I have also explained the rest in my last tutorial(s).
Next we are going to seek the "x" bytes. We have already declared the "x" variable as the location of the char ";".
fseek(pFile, x, SEEK_SET);
SEEK_SET, the start.
We are now going to load the rest of the file into "buffer".
fread(buffer, 1, size-x, pFile);
buffer, destination.
size-x, the original size minus the location of the char ";" is how much data is left in the file.
pFile, location.
We can know close the file:
fclose(pFile);
So, what have we done so far?
We have loaded a file, searched through it in a "while" loop to find the ";" char. If found, the variable "x" is set to its location (byte). We have then checked the file size (minus "x") and freed that much in the memory. Then we used fseek to seek to "x"'s position in the file (after ";") and then loaded the rest into the buffer.
So, what next you ask?
We are now going to give as much information as possible, so you can see how to use it for later use.
printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer);
This is going to print the file size in bytes, the location/byte of the char ";", bytes left after ";" and the text after ";".
Free the buffer from the memory:
free(buffer);
End the if / else:
}
Return 0...
return 0
Finally, end the main function:
}
This is what your main.c file should look like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char c, *buffer;
int n = 0, x, size;
FILE *file;
file = fopen("test.txt", "rb");
if(file == NULL) perror("Error opening file!");
else {
while(c != EOF) {
c = fgetc(file);
if(c == ';') {
x = ftell(file);
}
}
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
buffer = (char*) malloc(size-x);
if(buffer == NULL) perror("Error allocating memory!");
fseek(file, x, SEEK_SET);
fread(buffer, 1, size-x, file);
fclose(file);
printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer);
free(buffer);
}
return 0;
}
And your test.txt:
this is not usefull;super useful!
Ok, so now compile your program (gcc!)... and when it has finished, run the binary it makes, you should get the following output:
File Size: 34
Byte: 20
Bytes Left: 14
Text After: super useful!
Awesom, eh?
I hope you enjoyed reading my "File Reading" tutorials, and that you found them helpfull.
--harleyg