Thread: How to simply input a CSV file into an universal variable 2D Array

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    How to simply input a CSV file into an universal variable 2D Array

    Hello Everyone,

    I am working in an assignment and part of this assignment is to input a CSV file with floats such as this:

    8.5, 10.5, 90.5
    49.5, 99 ,97
    88, 70, 100
    78, 2, 10
    into an universal variable array.I just want to figure out a way to simply input the CSV file into a 2D variable array.The values in the array will be later use in functions that I'm trying to figure out but I can't do that until I store these values in the array. I think is just a matter of figuring out how to tell fscanf to ommit spaces and commas, but I don't know how. I am a beginner and I have try everything. If any of you could help me with this problem, I would appreaciated a lot.

    Thank you very much

    This is what I have done

    [======================================

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    float grades[1001][101];
    
    int main(int argc, char* argv[]){ 
      int i,j; 
      int num_student,num_assig; 
      FILE *input_file; 
    
      num_student = atoi(argv[1]);
      num_assig = atoi(argv[2]); 
    
       if( argc > 3 || argc < 3){
     printf("usage: ./a.out num_students      num_assignments\n");    return(0); 
       } 
    
               input_file = fopen("grades.txt","r");
    
            for( i = 1; i < num_student; i++){
       for( j= 1; j < num_assig ; j++){
       fscanf(input_file,"%f, ",&grades[i][j]); 
       } 
       }    
           printf("Assignment 1 of student 1 = %.1f\n",grades[1][1]);
        printf("Assignment 2 of student 1 = %.1f\n",grades[1][2]);
        printf("Assignment 3 of student 1 = %.1f\n",grades[1][3]);
        printf("Assignment 1 of student 2 = %.1f\n",grades[2][1]); 
    
              fclose(input_file);
    }
    


    ================================================== ========]

    The output is this

    Assignment 1 of student 1 = 8.5
    Assignment 2 of student 1 = 10.5
    Assignment 3 of student 1 = 0.0
    Assignment 1 of student 2 = 90.5

    I think I'm close to solve it but something is missing the output should look like this

    Assignment 1 of student 1 = 8.5
    Assignment 2 of student 1 = 10.5
    Assignment 3 of student 1 = 90.5
    Assignment 1 of student 2 = 49.5


  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    What you want to look at is the fgets() / sscanf() combination to read a line at a time and parse the line into your array.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User input variable to global array help
    By Bob_the_Builder in forum C Programming
    Replies: 5
    Last Post: 10-11-2013, 12:51 PM
  2. Copying variable to array or splitting input
    By BeNNs in forum C Programming
    Replies: 4
    Last Post: 01-03-2012, 08:49 PM
  3. Taking a variable and input each value of it into an array
    By jammysammy in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:46 AM
  4. how to input joystick simply?
    By denyilc in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2006, 02:35 AM
  5. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM