简体   繁体   English

我想逐行传递C文件并使用scanf进行读取,但跳过第一行

[英]I want to pass in a file in C line by line and read using scanf but skip the first line

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

int 
main(void){

 //Declare variables
  //Want to read the first line and then ignore it.  From the second line
  //I scan in the input values and then store them in their individual arrays.

     int yy, mm, dd, loc;
     double mx, mm;
         /*Read and discard the first line of the file*/
          scanf("%*[^\n]");

         /*Read from the second line*/
         while(scanf("%d,%d,%d,%d,%lf,%lf\n",
                    &loc, &yy, &mm, &dd, &mx, &mn) == 6){

            //Storing each input in its own array.
  }

}

scanf("%*[^\\n]"); will indeed read and discard up to the first newline, but won't discard the newline itself. 确实会读取并丢弃第一个换行符,但不会丢弃换行符本身。 I suggest pairing that with getchar(); 我建议将其与getchar();配对getchar(); .

\\n within a format string doesn't do what you think it does; \\n格式字符串中的\\n不会执行您认为的操作; as scanf isn't line-oriented (but instead field-oriented), you may find it'll discard all whitespace characters , not just newlines . 由于scanf不是面向行的(而是面向字段的),您可能会发现它会丢弃所有空白字符 ,而不仅仅是换行符 I wouldn't see that as a problem. 我认为这不是问题。 If you want to literally discard the remainder of the line, you could do so using the same code suggested to discard the first line: 如果您想逐字地丢弃该行的其余部分,则可以使用建议的相同的代码来丢弃第一行:

scanf("%*[^\n]");
getchar();

There are two declarations of mm in your code, though you've not mentioned an error message so I guess your testcase isn't accurate and you intended one of them (the double ) to be mn instead. 您的代码中有两个mm声明,尽管您没有提到错误消息,所以我想您的测试用例不准确,您打算将其中一个( double )改为mn

With those issues covered, as my testcase (included below) demonstrates I'd expect that your program is capable of reading and discarding the first line, and then reading four fields of int s as decimal digits followed by two fields of double s as decimal digits, separated by commas. 涵盖了这些问题后,正如我的测试用例 (包括在下面)演示的那样,我希望您的程序能够读取和丢弃第一行,然后读取四个int s字段作为十进制数字,然后读取两个double s字段作为十进制数字数字,用逗号分隔。

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

int main(void) {
    int yy, mm, dd, loc;
    double mx, mn;

    scanf("%*[^\n]");
    getchar();

    while (scanf("%d,%d,%d,%d,%lf,%lf\n", &loc, &yy, &mm, &dd, &mx, &mn) == 6){
        printf("<%d> <%d> <%d> <%d> <%f> <%f>\n", loc, yy, mm, dd, mx, mn);
    }
}

UPDATE : By the looks of one of your comments, this doesn't actually meet your assignment requirements. 更新 :从您的评论之一的外观来看,这实际上不符合您的作业要求。 "You should use a seperate while(getchar()) loop to consume the first line" , and so scanf("%*[^\\n]"); getchar(); “您应该使用单独的while(getchar())循环来消耗第一行” ,因此scanf("%*[^\\n]"); getchar(); scanf("%*[^\\n]"); getchar(); should be replaced with your loop. 应该用你的循环代替。

If you end up adding an attempt at that loop to your question, feel free to ping me so I can provide feedback. 如果您最终尝试在该循环中添加问题,请随时给我ping通,以便我提供反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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