i thought i do this by counting the number of equal signs in the text and scan the file in pairs (integer/char) until i hit a equal sign which i then count.
But the problem is that i need to tell the program before hand how many pairs there are in the file. Is there a way i scan the entire file?
Use the return value from fscanf to detect the end of file.
Your program doesn't handle the case where the file isn't formatted like you expect. I would use a function that reads unconditionally into a buffer first, and then parse the buffer. Then I would loop through the buffer a character at a time and count '=' as I go.
Use the return value from fscanf to detect the end of file.
Your program doesn't handle the case where the file isn't formatted like you expect. I would use a function that reads unconditionally into a buffer first, and then parse the buffer. Then I would loop through the buffer a character at a time and count '=' as I go.
All the files will always be operator operand operator operand etc. so dont think i need to worry about that.
What is the return value? Can you give me a small example?
It will serve you well for your academic journey, as well as any "real-world" experience, to code this the way the person suggested. Think beyond just this project's specifics.
Hey mate - I don't have much time, so I'm going to leave you to look up the details here, but the most efficient way to do this is to use low-level (or unbuffered) file IO routines (look in io.h) to open the file (returning a file handle), get the size (the routine is 'file_length()', I think - depends on your compiler), malloc a block big enough to hold the entire file, make ONE call to read() to get the whole thing, then loop through the buffer counting '=' as you suggest.
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, [CX] and errno shall be set to indicate the error.
Code:
matchCount = fscanf ( read ,"%d %c ", &opperand, &opperator);
if(EOF == matchCount) {
return count;
}
if(2 == matchCount) {
if('=' == opperator){
count++;
}
}
else {
assert(0); // the file is not what you expected
}