Hello I need some help, I would like to scan integers from a text file that has both characters and integers.
This is the text file:
Peter Koffman 57 82 97
Caryn Limonson 83 87 92
George Siacoura 75 87 32
Shirl Urys 95 72 88
When I scan for integers, it scans the characters as well, and converts them into int >_________>. I try to do a caclulation (add the integers up) and end up with a number like this:
45645267798
Here's the statement I was using (note all variables have been defined):
Code:
FILE *in_fpt
FILE *out_fpt
fscanf(in_fpt, "%d%d%d", &scr1, &scr2, &scr3);
Ovsiously that would not work, so I tried to do a while statement, here's the whole code.
Code:
#include <stdio.h>
#include <stdlib.h>
FILE *in_fpt, *out_fpt; //fpt = file pointer//
void copy_cat(FILE*, FILE*); //will copy the information via strings//
float avg_calc(FILE*); //will calculate averages//
main()
{
in_fpt = fopen("tests.txt", "r");
out_fpt = fopen("copy_cat.txt", "w+");
fprintf(out_fpt, "Names Scr 1 Scr 2 Scr 3 Average\n\n"); //heading//
fprintf(out_fpt, "%d", avg_calc(in_fpt));
fclose(in_fpt);
fclose(out_fpt);
return EXIT_SUCCESS;
}
/*
void copy_cat(FILE *in_fpt, FILE *out_fpt)
{
char storage[99];
}
*/
float avg_calc(FILE *in)
{
int scr1, scr2, scr3; //scores on tests//
int tot1, tot2, tot3; //total score for test 1, 2, 3.//
float scr_avg, tot_avg; //average for each student, average for each test//
tot1=tot2=tot3=0;
char test; //tests for new line character and/or EOF//
while( 0 < (test = fgetc(in)) < 9 ){
/*check to see that the scanned character is 0<x<9 to make sure it is an int*/
fscanf(in, "%d %d %d", &scr1, &scr2, &scr2);
scr_avg = (scr1 + scr2 + scr3);
/*tot1 +=scr1;
tot2 +=scr2;
tot3 +=scr3;*/
if( (test = fgetc(in)) == '\n'){ //exit statement when the line has ended//
break;
else( 0 > (test = fgetc(in)) > 9){//force statment to run even if char is scanned//
continue;
} //closes the else statment//
} //closes the if statment//
} //closes the while statement//
return scr_avg;
}
Im having problems compiling the above so anyaway, let me ask a more concrete question;
Is there a way to test what data type has been aquired via scanf, as in that I scanned in an integer and not a character?
That way I can created a condition while(scanf() = integer) do this and that.
Thank you for the help.