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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-27-2004, 02:09 AM
|
#1
|
Member
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45
Rep:
|
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
|
|
|
03-27-2004, 09:11 AM
|
#2
|
Member
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94
Rep:
|
Read lines of file into variable line.
Code:
int num, next_num;
num = atoi(strtok(line,",");
next_num = atoi(strtok(NULL,",");
|
|
|
04-04-2004, 02:09 PM
|
#3
|
Member
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45
Original Poster
Rep:
|
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
|
|
|
04-04-2004, 02:27 PM
|
#4
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
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).
|
|
|
04-04-2004, 03:41 PM
|
#5
|
Member
Registered: Sep 2001
Location: The Netherlands
Distribution: Ubuntu 4.10
Posts: 45
Original Poster
Rep:
|
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;
}
|
|
|
All times are GMT -5. The time now is 02:25 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|