LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2004, 02:09 AM   #1
MeLassen
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45

Rep: Reputation: 15
Question Reading comma-separated data from file


Hi,

I am just starting with C++ and want to write a small program that reads data from a file and calculated some numbers from it. The second part has been done, but I got stuck with the file reading. Actually, I can read the file, but I don't manage to get the data into matrix variables. The format op the file is:

1,1020
2,5630
3,889

etc.

Can anyone help me with this?

Thanks in advance
MeL
 
Old 03-27-2004, 09:11 AM   #2
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Rep: Reputation: 15
Read lines of file into variable line.

Code:
int num, next_num;
num = atoi(strtok(line,",");
next_num = atoi(strtok(NULL,",");
 
Old 04-04-2004, 02:09 PM   #3
MeLassen
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45

Original Poster
Rep: Reputation: 15
Thanks for your help. If I understand this correctly, this command extracts the values that are separated by the colon. These values are extracted from the string 'line'. So how do I get one line from a file in the variable 'line'?

MeL
 
Old 04-04-2004, 02:27 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
File Format:

3456,
1346,
1784,
45667,

Function to read it:

int i, lines[numlines]; //array
FILE *fileHandle; //file pointer

fileHandle = fopen("filename", "rb"); //open filename as read binary
if (!fileHandle) return 0; //file didn't get opened so return

for (i=0; i < numlines; i++) fscanf(fileHandle, "%d,\n", &lines[i]);

return 1;

There! Now if you want to access line 1 you would just: line[0],
or line two: line[1]

I didn't put much error checking into it, and it isn't very dynamic, but you should be able to figure that out (although it sounds like your a new programmer, forgive me if this is an incorect assumtion).
 
Old 04-04-2004, 03:41 PM   #5
MeLassen
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45

Original Poster
Rep: Reputation: 15
Yes, I am indeed a in C++ programming. But you have to start once I want to perform some calculations on a file with the following format:

1,362
2,323
3,341
4,1339
5,1103
6,1248
7,1159
8,1160
9,1089
10,1183
11,1171
12,994
etc.

The file is generated on another system by a program that I do not control, so I have to deal with it. By reading some manuals, this is what I could do to open the file and display it (see below). I tried all the suggestions above, but they don' work with this file. I want the values to go into an array. The number before the comma is the number in the array. So A[1]=362, A[2]=323, etc

thanks in advance for all your help.
MeL

---> read and display

ifstream OpenFile("test2.txt");
char ch;
while(!OpenFile.eof())
{
OpenFile.get(ch);
cout << ch;
}
OpenFile.close();

--> an option to read a full line and separate values by comma (does not work)

ifstream infile("test2.txt");
while(!infile.eof())
{
infile.getline(line, 40, endl); /* line ends after 40 characters of 'end of line' */
std::cout << "Number " << line << std::endl;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading audio data files as an actual audio file? Erik_the_Red Linux - Software 1 06-01-2005 08:22 AM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 09:42 AM
Reading data from file (field organizzation) eiem Programming 1 03-29-2004 06:03 AM
reading data from a file afrm Programming 2 12-16-2003 08:43 PM
comma delimited file cdragon Programming 5 06-21-2002 08:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration