The plan is to read the source code of another program, determine where the comments are(anything that has // before it), then replace the whole comment with spaces so that the program will ignore the comments when parsing.
Your program will read and parse a text file, assuming some syntax in the context. You want to ignore comments. Is that right?
If a line contains "//", then the "//" and the rest of the line is a comment. Is that right?
(What if a string literal contains //?. Is the // the only allowed comment syntax? It is not for C++.)
Why do you want to "replace with spaces"? Why not simply discard the rest of the line?
Why do you use plain array and C-string functions rather than std::string?
The stream::eof() is not a reliable indicator of the end of the input. Consider
#include<iostream>
#include<fstream>
usingnamespace std;
int main()
{
char line[256];
ifstream src(__FILE__);
// a comment that should disappear
char *ptr;
while (src.getline(line, sizeof(line)))
{
ptr = strstr(line, "//");
if (ptr != nullptr)
*ptr = '\0'; // this will remove anything after ptr
cout << line << "\n";
}
}