How can this be done - multiple variables stored

This is a discussion on How can this be done - multiple variables stored within the C Programming forums, part of the General Programming Boards category; Hi, This is what my code currently does. Reads a CSV file, parses the contents, displays a set number of ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    How can this be done - multiple variables stored

    Hi,

    This is what my code currently does. Reads a CSV file, parses the contents, displays a set number of columns and prints a specific value from a specified column and row at the end. Now ive been getting a lot of help on this code as im still trying to learn C.

    This is what i now need the project to. The values in some of the columns need to be extracted and used for the maths equation, which will then print the result at the end of the line. An example is that I would like to take the value from the 4th column along, on each line (which is a date), and then put this in a variable to be called by another part of my equation, in my case something to work out the difference between the two dates.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    
    int main() {
      const int fields_to_show = 4;				    
      const int record_to_show = 1;				   
      const int field_to_show = 8;				   
      char field_buff[256];						
      int prt_field_buff = 0;					
      
      FILE * fp;
      int c;
      int cnt_of_fields;						     
     int cnt_of_records;						    
      if((fp = fopen("file2.csv","r")) == NULL) {
        printf("Can't connect to file\n");
        return -2;
      }
    
      cnt_of_fields = 0;
      cnt_of_records = 0;
      while((c = fgetc(fp)) != EOF) {
        switch(c)
        {
        case ',' :  
          if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
            field_buff[prt_field_buff++] = '\0';
    
          cnt_of_fields++;
          if(cnt_of_fields < fields_to_show)
            printf(" - ");
          break;
    
        case '\n' :  
          if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
            field_buff[prt_field_buff++] = '\0';
    
          cnt_of_fields = 0;
          cnt_of_records++;
          printf("\n");
          break;
    
        default :  
          if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
            field_buff[prt_field_buff++] = c;
    
          if(cnt_of_fields < fields_to_show)
            printf("%c",c);
        } /* switch */
      } /* while */
    
      printf("%s\n",field_buff);
    
      fclose(fp);
      return 0;
    }
    Can someone tell me the best way of doing this please? I dont want to know the code i need, but if you can tell me how you would code it then i can look into how to do it.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    This should help explain it better.
    Attached Images Attached Images  

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    What you basically need is a grid. You have a series of rows, and operations need to be performed using certain columns in each row.

    You could look into 2d arrays, though an easier-to-understand way might be defining a struct that represents a row, and then store the data in an array of those structs.

    Regardless of which system you use - you then just need to write methods that traverse the array (be it an array of arrays or an array of structs) and perform the necessary operations to output the individual results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. register variables
    By BEN10 in forum C Programming
    Replies: 9
    Last Post: 04-17-2009, 07:20 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21