By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,132 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,132 IT Pros & Developers. It's quick & easy.

Reading Columns From A File

P: 16
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
Dec 18 '06 #1
Share this Question
Share on Google+
9 Replies


P: 10
hi,

you can solve ur problem using strtok(char *s1, const char *s2) function.
Dec 18 '06 #2

P: 16
thanks for ur reply...
can u give me the syntax of the function...
an example would be easier to understand...
thanks a lot
Dec 18 '06 #3

P: 16
...and is the function applicable even for FLOAT values?
cos those are float values which i have to read in and process...
thanks
Dec 18 '06 #4

P: 10
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
Dec 18 '06 #5

P: 10
...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....
Dec 18 '06 #6

P: 16
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<=2000;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<=2000;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<=2000;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<=2000;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<=2000;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<=2000;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");
}
Dec 18 '06 #7

P: 16
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
Dec 18 '06 #8

P: 10
I belive this sample will work 4 u


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

char string[] = "23.45,5645.3,453.2,3234.45,45.34,56.78,345.67,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
Dec 19 '06 #9

P: 16
thanks prakash..
it worked!
thankyou again
Dec 19 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.