Reading Columns From A File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhankrish
    New Member
    • Dec 2006
    • 16

    Reading Columns From A File

    hi all,
    can someone give me the C code to read individual columns of files into separate arrays?
    i have a .CSV file which has 6 columns and 2000 rows...
    i have to read each of the 6 columns into a separate single dimensional array.
    my program is working for one column...
    but i dont know how to shift the control to the next column of the file to start reading the elements.
    some1 pls help me out..its urgent.
    thanks
  • prakash ravindran
    New Member
    • Dec 2006
    • 10

    #2
    hi,

    you can solve ur problem using strtok(char *s1, const char *s2) function.

    Comment

    • madhankrish
      New Member
      • Dec 2006
      • 16

      #3
      thanks for ur reply...
      can u give me the syntax of the function...
      an example would be easier to understand...
      thanks a lot

      Comment

      • madhankrish
        New Member
        • Dec 2006
        • 16

        #4
        ...and is the function applicable even for FLOAT values?
        cos those are float values which i have to read in and process...
        thanks

        Comment

        • prakash ravindran
          New Member
          • Dec 2006
          • 10

          #5
          Here there is a sample code for u....

          #include <string.h>
          #include <stdio.h>

          char string[] = "A string\tof ,,tokens\nand some more tokens";
          char seps[] = " ,\t\n";
          char *token;

          int main( void )
          {
          printf( "Tokens:\n" );

          // Establish string and get the first token:
          token = strtok( string, seps );
          // Note: strtok is deprecated; consider using strtok_s instead
          while( token != NULL )
          {
          // While there are tokens in "string"
          printf( " %s\n", token );

          // Get next token:
          token = strtok( NULL, seps );
          }
          }


          And the Output will be

          Tokens:
          A
          string
          of
          tokens
          and
          some
          more

          i belive this will help u out

          Comment

          • prakash ravindran
            New Member
            • Dec 2006
            • 10

            #6
            Originally posted by madhankrish
            ...and is the function applicable even for FLOAT values?
            cos those are float values which i have to read in and process...
            thanks

            here all the values are processed as a character string so that wont give any problem for ur FLOAT values, B'coz that FLOAT value is also considered as a character String....

            Comment

            • madhankrish
              New Member
              • Dec 2006
              • 16

              #7
              thanks for ur reply..
              but i'm not able to apply tat to my problem..
              will u b able to write the code for me?? if tats not a trouble..
              below is wat i've done so far...
              its printing the output for the first column alone...
              after that i'm getting a 'segmentation fault'..


              #include<stdio. h>
              #include<stdlib .h>
              main()
              {
              int i,j,x,y,k;
              int l,m;
              float a[2000][6];
              float temp;
              FILE *fp;
              fp=fopen("./desktop/madhan/file1.csv","r") ; /*opening the text file*/
              for(x=1;x<=2000 ;x++)
              {
              fscanf(fp,"%f ,%f ,%f ,%f ,%f ,%f ",&a[x][1],&a[x][2],&a[x][3],&a[x][4],&a[x][5],&a[x][6]); /*reading values from the file*/

              }




              for(i=1;i<=2000 ;i++)
              {
              for(j=i+1;j<=20 00;j++) /*sorting the values*/
              {
              if((a[i][1])>(a[j][1]))
              {temp=a[i][1];
              a[i][1]=a[j][1];
              a[j][1]=temp;}
              }
              }

              for(i=1;i<=2000 ;i++)
              {
              if (((a[i][1])>0.953) && ((a[i][1])<0.955))
              printf("\nThe observed value of H1 is in the location %d \n",i);
              }





              for(l=1;l<=2000 ;l++)
              {
              for(m=l+1;m<=20 00;m++) /*sorting the values*/
              {
              if((a[l][2])>(a[m][2]))
              {temp=a[l][2];
              a[l][2]=a[m][2];
              a[m][2]=temp;}
              }
              }
              for(l=1;l<=2000 ;l++)
              {
              if (((a[l][2])>0.986) && ((a[l][2])<0.988))
              printf("\nThe observed value of H2 is in the location %d \n",i);
              }




              for(i=1;i<=2000 ;i++)
              {
              for(j=i+1;j<=20 00;j++) /*sorting the values*/
              {
              if((a[i][3])>(a[j][3]))
              {temp=a[i][3];
              a[i][3]=a[j][3];
              a[j][3]=temp;}
              }
              }
              for(i=1;i<=2000 ;i++)
              {
              if (((a[i][3])>0.988) && ((a[i][3])<.990))
              printf("\nThe observed value of H3 is in the location %d \n",i);
              }





              for(i=1;i<=2000 ;i++)
              {
              for(j=i+1;j<=20 00;j++) /*sorting the values*/
              {
              if((a[i][4])>(a[j][4]))
              {temp=a[i][4];
              a[i][4]=a[j][4];
              a[j][4]=temp;}
              }
              }
              for(i=1;i<=2000 ;i++)
              {
              if (((a[i][4])>0.0791) && ((a[i][4])<0.0793))
              printf("\nThe observed value of F1,2 is in the location %d \n",i);
              }





              for(i=1;i<=2000 ;i++)
              {
              for(j=i+1;j<=20 00;j++) /*sorting the values*/
              {
              if((a[i][5])>(a[j][5]))
              {temp=a[i][5];
              a[i][5]=a[j][5];
              a[j][5]=temp;}
              }
              }

              for(i=1;i<=2000 ;i++)
              {
              if (((a[i][5])>0.112) && ((a[i][5])<0.114))
              printf("\nThe observed value of F2,3 is in the location %d \n",i);
              }




              for(i=1;i<=2000 ;i++)
              {
              for(j=i+1;j<=20 00;j++) /*sorting the values*/
              {
              if((a[i][6])>(a[j][6]))
              {temp=a[i][6];
              a[i][6]=a[j][6];
              a[j][6]=temp;}
              }
              }

              for(i=1;i<=2000 ;i++)
              {
              if (((a[i][6])>0.139) && ((a[i][6])<0.141))
              printf("\nThe observed value of F3,1 is in the location %d \n",i);
              }
              fclose(fp);
              printf("\n thankyou \n");
              }

              Comment

              • madhankrish
                New Member
                • Dec 2006
                • 16

                #8
                the program has to read in 6 columns from an excel sheet..
                each column has 2000 elements
                each column has to be sorted in the ascending order...
                then, the location of a particular value in each sorted column has to be found and printed.
                i'll be very grateful if u could help..
                thanks

                Comment

                • prakash ravindran
                  New Member
                  • Dec 2006
                  • 10

                  #9
                  I belive this sample will work 4 u


                  #include <string.h>
                  #include <stdio.h>

                  char string[] = "23.45,5645.3,4 53.2,3234.45,45 .34,56.78,345.6 7,45.2";
                  char seps[] = ",\n";
                  char *token;
                  char **list;

                  int main( void )
                  {
                  int i ;
                  int j ;
                  int k ;

                  k = 0 ;

                  list = malloc(10 * sizeof(char*));
                  token = strtok( string, seps );

                  i=0 ;
                  while( token != NULL )
                  {
                  list[i] = token;
                  i++;
                  token = strtok( NULL, seps );
                  }

                  for (j=0; j< i ;j++)
                  {
                  printf(" %s",list[j]);
                  }
                  free(list);
                  }


                  Read ur file and store the value in a variable 'string', And here i had implemented the concept of single dimention array, u need to convert this array into as per ur need.

                  As all the values are manupulated as string so while storing the value in the FLOAT variable u can type caste char* into a FLOAT variable


                  Then things should work...

                  try it and send ur acknowledgement

                  Comment

                  • madhankrish
                    New Member
                    • Dec 2006
                    • 16

                    #10
                    thanks prakash..
                    it worked!
                    thankyou again

                    Comment

                    Working...