0

Hi,
I'm reading from a text file and writing to another one.My problem is that the data is being written to the new file as one line..i.e., when the end of a line is reached it does not start writing a new line but continues on the same line.. Can someone please tell me how to make it write data line by line? this is my code..Any improvements and suggestions will be appreciated. Thanks

#include<iostream.h>
void main()
{ 
      char *buffer;
ofstream myfile;
ifstream myfile1;
myfile.open("book1.txt");
myfile1.open("book2.txt");
while(!myfile1.eof())
{
myfile1.getline(buffer,'\t');
myfile<<buffer;
}
delete [] buffer;
myfile.close();
myfile1.close();
}

Edited by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by lotrsimp12345
0

Please use code tags in your next post. They are easy to use and help everyone read your code (and therefore help you) more effectively
Simple as:

[code]

//code goes here //more code

[/code]

main() returns an int (always).

Don't use .eof when reading in files(see this). So use your myfile1.getline to drive the while loop.

You don't need to use delete for buffer unless you use new.

In terms of your actual question, you can send a newline to your output stream just like you would send it to a cout, using endl or '\n'

Edited by jonsca: n/a

0

if you want to put each line separately use endl like follows:

while(!myfile1.eof())
{
myfile1.getline(buffer,'\t');
myfile<<buffer<<endl;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.