Reading strings, integers etc from files using fscanf

62,531

Solution 1

You are right, fscanf can give you the next integer. 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).

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.

Solution 2

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.

Solution 3

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);

}
Share:
62,531
Numerator
Author by

Numerator

Updated on July 09, 2022

Comments

  • Numerator
    Numerator almost 2 years

    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.

    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.

    In my case, where all integers separated by space, can I write 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? (I must have misunderstood it. Feels like magic).

  • Numerator
    Numerator almost 12 years
    Thank you @Shahbaz: Yes, I intended to the the second code you wrote since I need to do that until the end of the file, i.e all file contains numbers separated by space. How should I avoid the problem you mentioned?
  • Shahbaz
    Shahbaz almost 12 years
    @Numerator, by using my first example.
  • Numerator
    Numerator almost 12 years
    If it was a binary file, and each 8 bits represented a number, Can I use fscanf in this case as well in some way?
  • Shahbaz
    Shahbaz almost 12 years
    @Numerator, not in the conventional way. With binary files, fread is your friend.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 8 years
    Detail : "except when given %c %[] %n"
  • chux - Reinstate Monica
    chux - Reinstate Monica over 8 years
    fscanf(myFile, "%d", &number); if (feof(myFile)) break; does not check that a successful did indeed occur. Better to use if (fscanf(myFile, "%d", &number != 1) break;
  • Shahbaz
    Shahbaz over 8 years
    @chux, you are right. I had improved on these stuff afterwards (trying to write better answers here), but I didn't remember this answer to come back and fix it.
  • Pedro Sousa
    Pedro Sousa about 5 years
    This has an issue, if for reason the name in the string that is passed to fscanf is bigger than the size of your "char name[256]" you produce a buffer overflow.
  • Josh Greifer
    Josh Greifer about 5 years
    Well, the code as is doesn't have an issue, because it specifies the contents of the buffer, and in the code, strlen(name) is < 256. In general, of course, you're right - fscanf_s should be used instead. But the question was about fscanf usage.