简体   繁体   中英

fscanf issue reading in from file

I'm basically using the C function, fscanf() to read in data from a file. using fopen and checking it I know the file is being opened successfully by checking the return value.

The file is formatted with Char, Int, Int. The issue I have is I can only read in one line at a time and the loop exits. Would appreciate if anyone could see where I am going wrong

char c;     
char nl;    
int t1, t2; 
int dataTest; 
do
{
    dataTest = fscanf(fp, "%c %d %d", &c, &t1, &t2);
    fscanf(fp, "%c", &nl);
    printf("%c %d %d \n", c, t1, t2);
    if (dataTest = -1)
    {
        break;
    }
}while(1);

You want

if(datatest==-1)

rather than

if(datatest = -1)

One tests equality, while the other is an assignment expression. Any "assignment expression" in C will return the value to which the variable is assigned. So for example, datatest= -1 will return -1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM