-4

I have a text file that looks like this:

4
3
Samantha Octavia Ivan Henry
100 90 65 70
99 50 70 88
88 90 98 100

I am wanting to just read the first 2 lines individually and print them out, but as it stands now it is giving me a huge number.

inputFile = fopen ("input.txt", "r");

//input
    if( inputFile == NULL)
    {
        printf ("Unable to open file input.txt");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf ("How many students?\n ");
        fscanf (inputFile, "%d", &students);
        printf ("%d", &students);
        printf ("\nHow many assignments?\n ");
        fscanf (inputFile, "%d", &assignments);
        printf ("%d", &assignments);
        printf ("\n");
    }

What am I missing here?

Deadweight
  • 11
  • 1
  • 1
    Possible duplicate of [Read int values from a text file in C](http://stackoverflow.com/questions/4600797/read-int-values-from-a-text-file-in-c) – abhishek_naik Jun 11 '16 at 02:55
  • Try adding white-space following %d in your fscanf() calls...(e.g fscanf(inputFile, "%d ", &students); ) – TonyB Jun 11 '16 at 02:58
  • *What am I missing here?* Do you mean *other than a clear, meaningful problem description*? – Ken White Jun 11 '16 at 03:00

1 Answers1

1

Simple error!

Printing the value of &students or &assignments is not correct. That would print value of the pointer to that variable. You need the following code:

    printf ("How many students?\n ");
    fscanf (inputFile, "%d", &students);
    printf ("%d", students); // not &students
    printf ("\nHow many assignments?\n ");
    fscanf (inputFile, "%d", &assignments);
    printf ("%d", assignments); // not &assignments
    printf ("\n");
sg7
  • 6,108
  • 2
  • 32
  • 40