0

I wrote a quick C++ program that asks the user for a input text file and an output text file. The program is then supposed to number the lines in the file on the left margin. However, I cannot seem to get it working properly, it compiles fine but does not number the lines like it is supposed to. I believe it is a logical error on my part. I am also not too familiar with file i/o in C++ as I am just learning it now using old school textbooks.

Here is the file:

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>

using namespace std;

int main(void)
{int i = 0 , num = 1;
 string inputFileName;
 string outputFileName;
 string s;
 ifstream fileIn;
 ofstream fileOut;
 char ch;

 cout<<"Enter name of input file: ";
 cin>>inputFileName;
 cout<<"Enter name of output file: ";
 cin>>outputFileName;

 fileIn.open(inputFileName.data());
 fileOut.open(outputFileName.data());

 assert(fileIn.is_open() );
 assert(fileOut.is_open() );

 while (!(fileIn.eof()))
       {ch=fileIn.get();
        if (ch=='\n') num++;
           fileOut << num << "\n";
        s.insert(i,1,ch);     //insert character at position i
        i++;
       }
 fileOut << s;
 fileIn.close();
 fileOut.close();
 return 0;
}

If anyone could point me in thr right direction or give me some tips I would be eternally grateful.

NinjaZ
  • 25
  • 7
  • Don't assert things which could legitimately fail in a non-debug environment, such as a file not opening. Checking eof in a while loop is wrong. – Neil Kirk Oct 08 '14 at 01:08
  • Can you please explain to me why it is wrong or provide a link so that I may learn? – NinjaZ Oct 08 '14 at 01:10
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Neil Kirk Oct 08 '14 at 01:11

1 Answers1

4
int i = 0;
string line;
while (getline(infile, line))
{
    outfile << (i++) << " " << line << "\n";
}
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • Ok so should i modify my while loop to something like this? – NinjaZ Oct 08 '14 at 01:11
  • Yes this is how I would implement it. Much easier than getting the characters one at a time. – Neil Kirk Oct 08 '14 at 01:12
  • @RemyLebeau why did you edit my answer to make it worse? endl in a file stream makes it slower. – Neil Kirk Oct 08 '14 at 01:16
  • I agree now that I have played around with it, your method works a lot better than the one I was using. Thank you so much! Any other tips you could give me would be really helpful. – NinjaZ Oct 08 '14 at 01:19
  • @NeilKirk: I did not "make it worse". `std::endl` is the preferred way to end a line in C++, not outputting a `"\n"` character manually (that is the C way of doing it). `std::endl` is not slower, it handles outputting a line break in a platform-appropriate manner, an it also allows streams, like `ofstream`, to perform buffering that is flushed when `std::endl` is encountered. – Remy Lebeau Oct 08 '14 at 01:29
  • @RemyLebeau You are wrong. endl forces the file to flush at the end of every line. This is good if you think the program might terminate before the stream closes naturally. But more frequent flushing worsens the performance of the file stream. The harddisk doesn't know or care about a new line. Why should it flush every time this arbitrary character is encountered? endl is not required for buffering to occur. – Neil Kirk Oct 08 '14 at 01:31