简体   繁体   English

从C中的文件读取字符串的问题

[英]Problem with reading string from a file in C

I've tried to look for a similar problem but was unable to find it so I'm posting this.我试图寻找类似的问题,但找不到它,所以我发布了这个。 Here's the thing.事情是这样的。 Let's say I have a file named text.txt.假设我有一个名为 text.txt 的文件。 Now, the file consists of 3 integers and string, something like this:现在,该文件由 3 个整数和字符串组成,如下所示:

4 59 32 This is sentence 1
5 9 130 Grass is green
3 12 149 I need help

I'm still learning C so I'm sorry if this is some easy type question etc. Here's the problem.我仍在学习 C,所以如果这是一些简单类型的问题等,我很抱歉。这是问题所在。 I don't know how to read this.我不知道怎么读这个。 The same is if the string is at the beginning of the file like this同样是如果字符串在文件的开头是这样的

This is sentence 1 4 59 32
Grass is green 5 9 130 
I need help 3 12 149 

I know how to read it if I know the amount of words it will consist of (like if the file would be something like Name Surname Number Number Number) but this when I need to read entire, I have no idea.如果我知道它将包含的单词数量,我就知道如何阅读它(比如文件是否类似于 Name Surname Number Number Number),但是当我需要阅读整个时,我不知道。

Here's the code from comments.这是评论中的代码。 However, as @john pointed out, it's false right from the start since i gets first character and then I do scanf (still, I've tried out and with only numbers involved, fscanf gets correct values even though first character is read).但是,正如@john 指出的那样,它从一开始就是错误的,因为我获得了第一个字符,然后我执行了 scanf(不过,我已经尝试过并且只涉及数字,即使读取了第一个字符,fscanf 也会获得正确的值)。 I was also thinking about some while loop with isalpha() and isspace() involved but to no avail.我也在考虑一些涉及 isalpha() 和 isspace() 的 while 循环,但无济于事。

while((i = fgetc(input)) != (int)(EOF))
    {
        fscanf(inputFile, "%d %d %d", &num1, &num2, &num3);

        j=0;
        while(i != (int)('\n'))
        {
            string[j++]=(char)i;
            i = fgetc(inputFile);
        }
        string[j] = '\0';

        printf("%d %d %d %s\n",br1, br2, br3, string);

it's false right from the start since i gets first character and then I do scanf从一开始就是错误的,因为我得到了第一个字符,然后我做了 scanf

You're right, it's wrong.你是对的,这是错误的。 There's no need to consume a line's first character just to test for EOF - we can leave that to fscanf() .没有必要仅仅为了测试 EOF 而消耗一行的第一个字符——我们可以把它留给fscanf()
Also there's no need to read the string's characters one by one - fscanf() can read a string ending at \\n with the conversion specification %[^\\n] .此外,无需一个一个读取字符串的字符 - fscanf()可以读取以\\n结尾的字符串,转换规范为%[^\\n]
If the input string length is not limited, we'd have to prevent the buffer overflow when the input string would need more memory than provided by string by adding a maximum field width .如果输入字符串长度不受限制,我们必须通过添加最大字段 width来防止当输入字符串需要比string提供的更多内存时缓冲区溢出。 If you defined eg char string[100];如果你定义了例如char string[100]; , the maximum field width is 99, since the last char is needed for the terminating null character. ,宽度为99,自上次最大场char需要用于终止空字符。
So we could write eg所以我们可以写例如

    while (fscanf(input, "%d %d %d %99[^\n]", &num1, &num2, &num3, string) == 4)
        printf("%d %d %d %s\n", num1, num2, num3, string);

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

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