I have a data.txt
file and need to read different numbers of integers into every line for further processing, and some maybe a blank line.
//data.txt
3 7 2 1 9
8
234 0 2 -1
And I use a program to read it.
int main() {
FILE *fp=fopen("data.txt","r");
char input;
int temp;
if(fp==NULL){
perror("Cannot open file!\n");
exit(1);
}
while(!feof(fp)){
while(fscanf(fp,"%c",&input)==1){
if(input==' '){
printf(" ");
continue;
}
else if(input=='\n') {
printf("This line finished.\n");
continue;
}
else if(input=='-'){
fscanf(fp,"%c",&input);
temp=-(int)(input-'0');
printf("%d",temp);
continue;
}
temp=(int)(input-'0');
printf("|%d|",temp);
}
}
fclose(fp);
return 0;
}
And I get some weird results with -35 on every line.
|3| |7| |2| |1| |9||-35|This line finished.
|8||-35|This line finished.
|-35|This line finished.
|2||3||4| |0| |2| -1
Process finished with exit code 0
Does anyone know what's wrong with my program?