Okay, so I'm pretty new at this whole file reading thing, and I'm trying to make it so that the program reads a file, attaches a line number to the beginning of each line, and sends the output to another file. The thing is, there's this error that pops up, and I'm not exactly sure why.
The code that I have is as follows:
-
#include <iostream>
-
#include <fstream>
-
using namespace std;
-
int main (void)
-
{
-
char c;
-
int line = 0;
-
ifstream IS = "hw6b.cpp";
-
ofstream OS = "hw6boutput.cpp";
-
while ((c = IS.get()) != EOF)
-
if ( c == '\n')
-
{
-
line++;
-
OS << line << '.' << '\t' << c;
-
};
-
return 0;
-
}
-
Is there something I'm doing wrong? Something I'm missing?
There are three main things that I am seeing. The first is your declaration of the files. You need to use the stream.open(), for example:
-
ifstream IS;
-
IS.open("hw6b.cpp");
-
for both your input and output streams.
Your ';' after the if statement is not 100% necessary (though it certainly still works with it there), and although it's not a big deal in a small program like this, you should consider using the IS.close() operator as well (for both streams).
The final thing is that your algorithm is incorrect. Your program (once the syntax errors are corrected), will wipe out any of the lines, and just put the numbers in them. It's not a big thing, you just need to restructure a little bit. The first part is an else statement that will print the character if it is not a '\n', which was previously the only time it was printed... There are a few more small issues, but nothing major - you should be able to get it from there.