Skip remainder of line with fscanf in C

前端 未结 3 1736
暖寄归人
暖寄归人 2021-01-06 13:31

I\'m reading in a file and after reading in a number, I want to skip to remaining part of that line. An example of a file is this

2 This part should be skipp         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 14:19

    #include 
    
    int main(){
        FILE *f = fopen("data.txt", "r");
        int n, stat;
        do{
            if(1==(stat=fscanf(f, "%d", &n))){
                printf("n=%d\n", n);
            }
        }while(EOF!=fscanf(f, "%*[^\n]"));
        fclose(f);
    
        return 0;
    }
    

提交回复
热议问题