0

How to copy/read multiple lines using the fstream/ifstream/ofstream?

it only read the first line https://code.sololearn.com/cgE4jUs8kj7h/?ref=app

19th Mar 2018, 6:24 PM
Otumian Empire
Otumian Empire - avatar
8 odpowiedzi
+ 1
Did you place cnf<<line<<endl; inside the loop as well?
20th Mar 2018, 6:24 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
You should put the getline part in a loop to read the entire file. Also, don't close the file before you read everything. Code Example : ------------------------------------------------------------------ string line; ofstream cnf("newfile.txt"); while(!readFile.eof()) // While we are not at the end. { getline(readFile, line); cnf <<"Line read from the Good.c file: " <<line <<endl; // Read from one and write to other. } readFile.close(); cnf.close(); // Close only when done.
20th Mar 2018, 3:33 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
A loop is required here, as your file contains multiple lines and you need to have a getline for every single one of them.
20th Mar 2018, 5:27 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
I'll try
20th Mar 2018, 5:01 AM
Otumian Empire
Otumian Empire - avatar
0
will a new getline get the line? I'll try *** edit: I meant will a newline get the next line
20th Mar 2018, 5:03 AM
Otumian Empire
Otumian Empire - avatar
0
I tried it.. but it doesn't read all.. this time it reads only the last line into the new file... but reads all on the console/cmdline
20th Mar 2018, 5:49 PM
Otumian Empire
Otumian Empire - avatar
0
no.. I just did that.. thanks it worked
20th Mar 2018, 6:40 PM
Otumian Empire
Otumian Empire - avatar
20th Mar 2018, 7:04 PM
Otumian Empire
Otumian Empire - avatar