Open Side Menu Go to the Top
Register
Need some help with my C project Need some help with my C project

05-22-2013 , 06:58 AM
Hi,

I have to make a program that makes math exercises for kids. The program is written in C.

Part of the program is to read exercises from a .txt file and return a integer containing the number of exercises in the file.

the test .txt looks like this:

1 / 2 * 3 =
1 + 2 * 3 - 4 =
5 + 2 * 3 =
2 - 5 * 2 + 5 =
4 * 5 * 2 + 4 * 3 / 6 - 8 =

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?

This is the code i made:

#include<stdio.h>
#include<stdlib.h>

int countExercises(char name[]);

int main()
{
char name[9]="test.txt";

countExercises(name);

system ("pause");

return 0;
}

int countExercises(char name[]){

int opperand,i=0,count=0;

char opperator, d='=';

FILE *read = fopen(name, "r");

do{
fscanf ( read ,"%d %c ", &opperand, &opperator);

if(opperator==d){
count++;
}
i++;
}while(i<21);


printf("%d\n\n",count);
}

Thanks in advance
Need some help with my C project Quote
05-22-2013 , 07:38 AM
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.
Need some help with my C project Quote
05-22-2013 , 08:00 AM
Quote:
Originally Posted by Chips Ahoy
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?

Thank you
Need some help with my C project Quote
05-22-2013 , 08:42 AM
Quote:
Originally Posted by MilesAhead
All the files will always be...
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.
Need some help with my C project Quote
05-22-2013 , 09:04 AM
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.

If I get time later, I'll post some code...
Need some help with my C project Quote
05-22-2013 , 09:14 AM
Quote:
Originally Posted by MilesAhead
What is the return value? Can you give me a small example?
http://pubs.opengroup.org/onlinepubs...ons/scanf.html
Quote:
RETURN VALUE

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
}
Need some help with my C project Quote
05-22-2013 , 05:55 PM
Thanks for all the help i really appreciate it
Need some help with my C project Quote

      
m