ProgrammingThis 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.
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.
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:
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'?
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).
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.