| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
infile...reading one line at a time
I have to write a program that opens a file. There are about 10 lines of information...1 string and 5 integers. What i need to do is read in the string then go to a function where the program reads in the 5 integers and then calculates an average and sends it back to the main function. Outfile the information and then go to the next line of the infile.
I think I need to use a loop but im not sure how to just read one line in at a time and then go to the next. Any thoughts or help would be much appreciated. Thanks, Echo |
|
#2
|
|||
|
|||
|
with the ifstream you have to open the file, there is a function called getline, which you could use to obviously get the current line. You could the function strtok parse this line to get the individual elements. So if your line was:
firstline 1 2 3 4 5 the first call to strtok would look like this: char* token = strtok(line, " "); where line the char*/char[] you used to store the result from getline, and the second parameter " " is just to let strtok know what you used to separate the token on the line. Had we used commas to separate the elements on a line: firstline,1,2,3,4,5 it would look like strtok(line, ","); after the first call to strtok, you have the string part, now to get the integers, you loop until you've read on the integers, calling strtok like so: strtok(NULL, " "); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > infile...reading one line at a time |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|