473,396 Members | 2,014 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,396 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 12300
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

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.