Reading source file

Hey everyone.

Is it possible to read a C++ source file using stream operators? I want to read a source file and output all the comments contained in that source file.

I have the code which works with a text file, however I would like the ability for the user to upload their source code, and my program to read the source code as if it was a text file.
Of course. Why wouldn't it be?
Telling what's a comment and what's not isn't quite as easy as it first appears.

For example
1
2
3
4
5
6
7
8
9
10
11
12
/\
\
/ I'm a comment

R"EOD( // but I'm not
EOD)";

"/* nor am i!";
// */

/\
* "not a comment"; */
Last edited on
I have the comment portion already created, but so far my program just reads text files, and I would like it to be able to read a C++ source code (and in the future, html, css, java etc.)

By using fstream, I can also read c++ source code? I didn't know that, so I can use for example,

name of cpp: "main.cpp"

std::ifstream reader("main.cpp");

and this will treat it as if it was a text file or a binary file?
closed account (E0p9LyTq)
Most, if not all, compilers require source files to be written in plain text.

I don't know of any language that requires encoded source files.

So, you will read a source file, no matter what the programming language, as a text file.
Topic archived. No new replies allowed.