472,119 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

reading a csv file

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
13 12142
Give the detailed information.
You are working in windows or linux??
And the fields are separated by blanks or tabs???
Sep 19 '06 #2
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
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
sorry i didnt understand the code...
i am using c language...
so if u can plz send back
Sep 19 '06 #5
Use the system() API to execute the above commands and then see the files file1, file2, file3. and then proceed.
Sep 19 '06 #6
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
Try this one........

Spoonfeeding code removed - please read Posting Gudieliens

Now see the files file1, file2 and file3.
Sep 19 '06 #8
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
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
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
Hi Can you please tell me how to do the same in
1. Windows and
2. Using File operations.
Aug 26 '08 #12
JosAH
11,448 Expert 8TB
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
arnaudk
424 256MB
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.

Similar topics

4 posts views Thread by Xah Lee | last post: by
1 post views Thread by fabrice | last post: by
19 posts views Thread by Lionel B | last post: by
4 posts views Thread by Oliver Knoll | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
7 posts views Thread by John Dann | last post: by
6 posts views Thread by efrenba | last post: by

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.