简体   繁体   English

使用fscanf从文件中读取字符串,整数等

[英]Reading strings, integers etc from files using fscanf

I'd like your help with understanding how should I do the following: 我希望您能帮助我理解如何执行以下操作:

I have a file that contains integers separated by spaces ' '. 我有一个包含由空格''分隔的整数的文件。 I need to read all integers, sort them and write them as a strings to another file. 我需要读取所有整数,对它们进行排序并将它们作为字符串写入另一个文件。 I wrote a code, but I read char by char, put the word in an char sub_arr [Max_Int] and when I met ' ', I put these chars, now one string, after atoi-ing it into another Main int array,until reaching the end of the file, string by string, and then I sorted and itoa-ing them and wrote them in another file. 我写了一个代码,但是我通过char读取char,把这个单词放在char sub_arr [Max_Int]中,当我遇到''时,我将这些字符放入另一个Main int数组后,现在放入一个字符串,直到到达文件的末尾,逐个字符串,然后我对它们进行排序并将它们写在另一个文件中。

But then I remembered that there's a fscanf function:I read about it and still I didn't understand completely what does it do and how to use it. 但后来我记得有一个fscanf函数:我读到了它,但我仍然不完全理解它做了什么以及如何使用它。

In my case, where all integers separated by space, can I write fscanf(myFile,"%s",word) ? 在我的情况下,所有整数由空格分隔,我可以写fscanf(myFile,"%s",word)吗? would it know not to consider ' ' and stop at the end of the specific string?! 它会不会考虑''并停在特定字符串的末尾?! How? 怎么样?

More than that, Can I write fscanf(myFile,"%d",number) and it would give me the next number itself? 更重要的是,我可以写fscanf(myFile,"%d",number) ,它会给我下一个号码吗? (I must have misunderstood it. Feels like magic). (我一定是误会了。感觉​​像魔术一样)。

You are right, fscanf can give you the next integer. 你是对的, fscanf可以给你下一个整数。 However, you need to provide it with a pointer. 但是,您需要为其提供指针。 Therefore, you need an & behind number: 因此,您需要一个&后面的数字:

fscanf(myFile, "%d", &number);

*scanf family of functions also automatically skip whitespace (except when given %c , %[ or %n ). *scanf系列函数也会自动跳过空格(除非给定%c%[%n )。

Your loop with reading file will eventually look like this: 您的阅读文件循环最终将如下所示:

while (you_have_space_in_your_array_or_whatever)
{
    int number;
    if (fscanf(myFile, "%d", &number) != 1)
        break;        // file finished or there was an error
    add_to_your_array(number);
}

Side note: you may think of writing like this: 旁注:您可能会想到这样写:

while (!feof(myFile))
{
    int number;
    fscanf(myFile, "%d", &number);
    add_to_your_array(number);
}

This, although looks nice, has a problem . 这虽然看起来不错, 却有问题 If you are indeed reaching the end of file, you will have read a garbage number and added to your data before testing the end of file. 如果您确实到达文件末尾,则在测试文件结尾之前,您将读取垃圾编号并添加到数据中。 That is why you should use the while loop I mentioned first. 这就是你应该首先使用我提到的while循环的原因。

following lines will do your work, following lines will read single integer. 以下行将完成您的工作,以下行将读取单个整数。

int number;
fscanf(myFile, " %d", &number);

Put it in a loop until end of file, and place the number in array. 将它放在循环中直到文件结尾,并将数字放在数组中。

Try this: 尝试这个:

#include <stdio.h>


int main(int argc, char* argv[])
{
    char name[256];
    int age;
    /* create a text file */
    FILE *f = fopen("test.txt", "w");
    fprintf(f, "Josh 25 years old\n");
    fclose(f);

    /* now open it and read it */
    f = fopen("test.txt", "r");

    if (fscanf(f, "%s %d", name, &age) !=2)
        ; /* Couln't read name and age */
    printf("Name: %s, Age %d\n", name, age);

}

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

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