[try Beta version]
Not logged in

 
Reading source file

Jul 6, 2018 at 3:00am
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.
Jul 6, 2018 at 3:43am
Of course. Why wouldn't it be?
Jul 6, 2018 at 3:45am
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 Jul 6, 2018 at 3:51am
Jul 6, 2018 at 4:15am
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?
Jul 6, 2018 at 4:18am
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.