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();
}

Recommended Answers

All 2 Replies

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'

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

while(!myfile1.eof())
{
myfile1.getline(buffer,'\t');
myfile<<buffer<<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.