简体   繁体   English

使用fscanf在C中读取多行

[英]Reading multiple lines in C using fscanf

i'm currently doing an uni project which has to read a multiple lines sequence of inputs given in a .txt format. 我目前正在做一个uni项目,该项目必须读取以.txt格式给出的多行输入序列。 This is my first experience with C, so i don't know much about reading files with fscanf and then processing them. 这是我第一次使用C,因此我对使用fscanf读取文件然后进行处理并不了解。 The code i wrote goes like this: 我写的代码是这样的:

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

int main() {
    char tipo [1];
    float n1, n2, n3, n4;
    int i;
    FILE *stream;
    stream=fopen("init.txt", "r");
    if ((stream=fopen("init.txt", "r"))==NULL) {
        printf("Error");
    } else {
        i=0;
        while (i<4) {
            i++;
//i know i could use a for instead of a while
            fscanf(stream, "%s %f %f %f %f%", &tipo, &n1, &n2, &n3, &n4);
            printf("%s %f %f %f %f", tipo, n1, n2, n3, n4);
        }
    }
    return 0;
}

My "init" file is formatted like this: 我的“ init”文件的格式如下:

L 150.50 165.18 182.16 200.50
G 768.12 876.27 976.56 958.12
A 1250.15 1252.55 1260.60 1265.15
L 200.50 245.30 260.10 275.00
A 1450.15 1523.54 1245.17 1278.23
G 958.12 1000.65 1040.78 1068.12

I don't know how to tell the program to skip a line after the first one is read. 我不知道如何在读取第一个后告诉程序跳过一行。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

Use fscanf(stream, "%*[^\\n]\\n") to skip line. 使用fscanf(stream, "%*[^\\n]\\n")跳过行。 Just add one if statement to check line number to skip. 只需添加一个if语句来检查要跳过的行号。 if (i == 2) to skip second line. if (i == 2)跳过第二行。 Also change char tipo[1] to char tipo and change "%s" to "%c" in printf and fscanf 还要将char tipo[1]更改为char tipo并在printffscanf中将“%s”更改为“%c”

while (i++ < 4) 
{
    if (i == 2) // checks line number. Skip 2-nd line
    {
        fscanf(stream, "%*[^\n]\n");
    }
    fscanf(stream, "%c %f %f %f %f\n", &tipo, &n1, &n2, &n3, &n4);
    printf("%c %f %f %f %f\n", tipo, n1, n2, n3, n4);
}

Also you are opening file twice. 另外,您要打开文件两次。 if(streem = fopen("init.txt", "r") == NULL) will be true because you have already opened file. if(streem = fopen("init.txt", "r") == NULL)将为true,因为您已经打开了文件。

In response to " I don't know how to tell the program to skip a line after the first one is read ."Just do this! 响应“ 我不知道如何在读取第一个代码后告诉程序跳过一行 。”请执行此操作!

while (i<4) 
{
    i++;
    //i know i could use a for instead of a while
    fscanf(stream, "%s %f %f %f %f%", &tipo, &n1, &n2, &n3, &n4);
    if(i != 2) //skipping second line
        printf("%s %f %f %f %f", tipo, n1, n2, n3, n4);

}

Also there is no point in using an 1-element array. 同样,使用1元素数组毫无意义。 If you wish to just use a char element change it from char tipo [1]; 如果您只想使用char元素,请从char tipo [1];更改它char tipo [1]; to char tipo; char tipo; and your respective "%s" to "%c" . 以及您各自的"%s""%c" But if you wish it to be a string element : change it from char tipo [1]; 但是,如果您希望它是一个string元素,请从char tipo [1];更改char tipo [1]; to char *tipo; char *tipo; or char tipo [n]; char tipo [n]; and keep your "%s" . 并保留您的"%s"

There's no reason to use a char array (string) when you're only going to read one character. 当您只打算读取一个字符时,没有理由使用char数组(字符串)。

Do this: 做这个:

char tipo;

and

fscanf(stream, "%c %f %f %f %f%", &tipo, &n1, &n2, &n3, &n4);

and you're code should work. 并且您的代码应该可以工作。 Note the c instead of s. 注意c而不是s。

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

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