2

I am trying to write a program that reads lines from a .txt and inputs them to 2 different arrays.

So far I have this:

    #include <stdio.h>

    int main() {
        FILE * ifp = fopen("input.txt","r"); 
        FILE * ofp = fopen ("output.txt", "w"); 
        int participants = 0, i;  
        char name [10];
        float grade [10];
        float perc [10];

        fscanf(ifp, "%d", &participants);

        for (i=1; i<participants; i++) {
             fscanf(ifp, "%s", &name);

             fscanf(ifp, "%f", &grade);

        }

        printf( "%d\n", participants);
        printf( "%s\n", name);
        printf( "%f\n", grade);

        fclose(ifp);
        fclose(ofp);

        return 0;
    }

The txt I'm trying to read is:

    2 
    Optimus 
    45 90 
    30 60 
    25 30 
    50 70 
    Megatron 
    5 6 
    7 9 
    3 4 
    8 10 

My problem is that it picks up the first 2 lines but stops when it gets to the numbers. I'm trying to get the names into an array and all the numbers, in pairs in a different array. Right now I'm just trying to check to see if I am picking up the numbers in the array but its not picking them all up.

This is the output that I get:

    2
    Optimus
    0.000000

Any ideas?

EDIT

This is my new code after some changes:

    #include <stdio.h>

    int main() {
        FILE * ifp = fopen("input.txt","r");
        FILE * ofp = fopen ("output.txt", "w");
        int participants = 0, i , j; 
       char name [10];
        int grade [26];
        float perc [26];

        fscanf(ifp, "%d", &participants);

        for (i=1; i<participants; i++) {
             fscanf(ifp, " %s", name);
             fscanf(ifp, " %d", grade);

        }

        printf( "%d\n", participants);
        printf( "%s\n", name);
        printf( "%d\n", grade[0]);

        fclose(ifp);
        fclose(ofp);

      return 0;
    }

And my new output is :

    2
    Optimus
    45

EDIT 2

What I need to do with those numbers later is divide the first number in a line with the second number in that same line, multiply it by 10, and then have it display "*" according to the number. So it would print out like this:

    Optimus
    +:  *****
    -:  *****
    *:  ********
    /:  *******
    Megatron
    +:  ********
    -:  *******
    *:  *******
    /:  ********

"+" is the first line under a name. "-" is the second line under that same name. "*" for the third. "/" for the fourth.

2 Answers 2

1

This is your main problem:

    printf( "%f\n", grade);

You're trying to print a pointer as a float. You want grade[0].

In this line:

    fscanf(ifp, "%f", &grade);

This will work, but it's not correct. It should be &grade[0] or just plain grade.

Of course in your final version, you'll need to adjust the array subscripts. You'll also need to allocate more arrays for the name - you currently can only store one.

To read in the data you will need to change the program a lot. You could declare grade as grade[10][8] (assuming a maximum of 10 participants), and store each of the 8 integers in the array. A loop like this would process it:

   for (i = 0; i < participants; i++) {
      fscanf(ifp, "%s", name);             // read the name (you need to fix this)
      for (j = 0; j < 8; j++) {
        fscanf(ifp, "%d", &grade[i][j]);   // store each number
      }
   }
Sign up to request clarification or add additional context in comments.

7 Comments

fscanf(ifp, "%f", &grade); is technically correct as well. As &grade == grade.
@Armin, it's not. &grade is a pointer to an array, not to an element.
Go try it and check the addresses of &grade and grade, it is the same.
Ok, so I changed it and now it picks up the first number but stops there. How can I make it so that it continues to pick up the other numbers and when it gets to the name, it adds that to the "name" array.
@Armin, I'm quite aware of that, but they're not the same type.
|
0

name and grade are arrays - the address of the variable is implicit for an array. So the address operator should not be used for these variables.

fscanf(ifp, "%s", name);
fscanf(ifp, "%f", grade);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.