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

reading a csv file

P: 25
hey,
i have to do the following in c:
i want to read data from a csv file.
i need to read the first, second and fifth column of the file.
i am using fread but i cant get it to read the desired data.
Sep 19 '06 #1
Share this Question
Share on Google+
13 Replies


P: 61
Give the detailed information.
You are working in windows or linux??
And the fields are separated by blanks or tabs???
Sep 19 '06 #2

P: 25
i am working in linux.

the data in the file are as the following:

name of the data, "day/month/year",values,values,values...
name of the data,"day/month/year",values,values,values...

and so on...
a comma is used for seperation
Sep 19 '06 #3

P: 61
You can solve this problem in two ways......

Use the following command......

Spoonfeeding code removed, please read posting guidelines

Now read line by line from file2 and parse the fields.

Secondly you can do like this.....

Spoonfeeding code removed, please read posting guidelines

Now read all the three files at a time and get each word from each file and that will give you each field of a record.
Sep 19 '06 #4

P: 25
sorry i didnt understand the code...
i am using c language...
so if u can plz send back
Sep 19 '06 #5

P: 61
Use the system() API to execute the above commands and then see the files file1, file2, file3. and then proceed.
Sep 19 '06 #6

P: 25
still dont get what u saying.


i need u to tell me how to do that in the
int main(void)
{}
format..



sorry for that
Sep 19 '06 #7

P: 61
Try this one........

Spoonfeeding code removed - please read Posting Gudieliens

Now see the files file1, file2 and file3.
Sep 19 '06 #8

P: 25
what this mean??
system("cut -d',' f1 filename > file1");
system("cut -d',' f2 filename > file2");
system("cut -d',' f5 filename > file3");
Sep 19 '06 #9

P: 61
See cut command will cut the required columns of the file and we are redirecting the output to a file. And system API will help you run any system command from the C code.

See the man page for cut.
Sep 19 '06 #10

risby
P: 30
hey,
i have to do the following in c:
i want to read data from a csv file.
i need to read the first, second and fifth column of the file.
i am using fread but i cant get it to read the desired data.
If you are reading a text file, wherein each line can be of different length and is delimited by the newline character, then fread is inappropriate.

fread() will read a specific number of bytes and using it will unnecessarily complicate your solution. Use fgets() to read a line at a time and then use strchr() to find a comma (also look at strtok() which you may or may not find easier to use).
Sep 19 '06 #11

P: 1
Hi Can you please tell me how to do the same in
1. Windows and
2. Using File operations.
Aug 26 '08 #12

Expert 10K+
P: 11,448
Hi Can you please tell me how to do the same in
1. Windows and
2. Using File operations.
Please don't hijack someone else's thread; it is considered rude. Either read and
optionally reply in this thread or if you really want to you can start your own thread.

kind regards,

Jos (moderator)

ps. See the 'Help' link near the top of this page for more information.
Aug 26 '08 #13

100+
P: 424
hey,
i have to do the following in c:
i want to read data from a csv file.
i need to read the first, second and fifth column of the file.
i am using fread but i cant get it to read the desired data.
What pukur123 is suggesting amounts to using linux commands to split the columns into separate files. But that won't help you if you want to read the csv data into variables of your C program, you'll need to use C commands and there is one ideally suited for what you want to do (that will work in linux and windows): fscanf

Use fgets() to read a line at a time and then use strchr() to find a comma (also look at strtok() which you may or may not find easier to use).
fscanf() is even simpler than fgets() when you know in advance what format to expect; you won't need to search for commas and date slashes, etc.

[EDIT:] Whoops, I didn't notice this thread was started in 2006...[/EDIT]
Aug 26 '08 #14

Post your reply

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