Program that reads text from file and outputs each line to the screen from another file

I am failing to copy the file to another file preceded by Number line

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void editFile (ifstream & inStream, ofstream & outStream);

int main ()
{
ifstream fin;
ofstream fout;
char infileName[21], outfileName[21];
cout<<" Enter the file name of the source file not longer than 20 characters \n";
cin>>infileName;
cout<<" Enter the file name of the output file not longer than 20 characters \n";
cin>>outfileName;

cout<<" Begin editing files \n";

fin.open(infileName);

if (fin.fail())
{
cout<<" Input file opening failed.\n";
exit(1);
}


fout.open(outfileName);

if (fin.fail())
{
cout<<" Output file opening failed.\n";
exit(1);
}

editFile(fin,fout);

fin.close();
fout.close();


return 0;
}

void editFile (ifstream & inStream, ofstream & outStream)
{
char line[50];
int numberLine=0;
outStream.setf(ios::right);
outStream<<setw(14);

inStream.getline(line, 50);

while (!inStream.eof())
{




outStream<<"Line Number " <<numberLine<<':'<<' '<<line<<endl;
inStream.getline(line, 50);
numberLine++;


}

}

this may help.

what problems, *exactly*, are you having? You have the right idea...

while(inStream.getline(line, 50)) //the eof technique has problems, this is the accepted way.
{
outStream<<"Line Number " <<numberLine++<<':'<<' '<<line<<endl; //++condense not necessary
}
Last edited on
void editFile (ifstream & inStream, ofstream & outStream)
{
char line;
int numberLine=1;

inStream.get(line);

while (!inStream.eof())
{


outStream.setf(ios::right);
outStream<<setw(15);

cout.setf(ios::right);
cout<<setw(15);

outStream<<"Line Number " <<numberLine<<':'<<' '<<line;
cout<<"Line Number " <<numberLine++<<':'<<' '<<line;



while ((line != '\n')&& (!inStream.eof()))
{

inStream.get(line);
outStream<<line;
cout<<line;
}



inStream.get(line);


}

}



This is what i have managed
ok. now put it in code tags <> on the side bar editor, and explain what isnt working in enough detail that I don't have to copy it, paste it, download it, try to guess the problem, and fix it. Help us help you. You appear to have ignored my first post, I still see that EOF logic which has problems.
Last edited on
Topic archived. No new replies allowed.