473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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
Dec 18 '06 #1
9 7044
hi,

you can solve ur problem using strtok(char *s1, const char *s2) function.
Dec 18 '06 #2
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
...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
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
...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
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
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
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
thanks prakash..
it worked!
thankyou again
Dec 19 '06 #10

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

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
4
by: Ameya | last post by:
i want to read from a trace file ....but my problem is i am interested only in first 6 columns of my 10 - 12 wide text file. Each column is separated by a space (the number of columns vary on each...
9
by: paczkow | last post by:
Dear Python Community, I am an engineering and I am experiencing some trouble. Having output data from other software I want to use it. To achieve this I decided to use Python since this...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
1
by: c language | last post by:
Hello everybody, I have to do some calculations on two different files with C program. First file has 2 columns and second file has 100 columns. The program should get the columns of the first...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
2
by: rwiegel | last post by:
I'm trying to read rows from an Excel file and display them in an ASP.NET DataGridview. I am using C# for the code file. I am using OleDb to read from the Excel file. The columns that contain...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.