How could I print specific lines of a code in console? | Sololearn: Learn to code for FREE!


How could I print specific lines of a code in console?

The following codes print "Your Source Code as Output". And, I know that instead of the macro FILE, you enter the path of your file. Let's say...I write a C/C++ code & wanna print from Line "x" to Line "y" or different lines of the same code in the output. How could I achieve that?! One final question: Why in the C code is it printed this character <�> 'question mark inside diamond' at the end?

2/22/2019 10:02:07 PM

Geovanny Martínez Forero

14 Answers

New Answer


C++ Soldier (Babak) Thanks for your explanation!!


diego Code Reviewing your code, I see that the null character as Rowsej stated is not printed. Is it because of your explanation?!


diego Code Rowsej Thanks for explaining & providing nice resolutions. Now, I can do what I want to. Thanks a lot!!!


~ swim ~ Thanks for expanding the former replies!!!


So, if I need different ranges, should I repeat the same process?!


Rowsej Yep, I prefer that approach!


"Why it doesn’t show in C++ is a mystery to me though" Simple! `std::string`, unlike C-string, is not a null-terminated object. As already pointed out, the `\0` character in a C-string object signals the end of the string because, in the implementation, there's no such thing as `str.length()` [it holds `content + '\0'`]. The '\0' is there to provide such effect, for example, when you call `strlen(str)` which reads the string until '\0' and returns the "visible length" of the string. The STL string container, however, holds `content + length` pretty much the same way that the `std::vector` does. Note: Though, the traditional null-terminated buffer effect is achievable by a `c_str()` call.


In the C code you are testing eof after printf. In the C++ you sre testing before eof, so if eof is reached, exits loop before printing.

+6 The question mark thing is the null character (U+0000), and it signals the end of a string. Why it doesn’t show in C++ is a mystery to me though.


Yes. Instead of using #define you could put them into variables, and put the whole thing into a function that could be called again and again.


Geovanny Martínez Forero Diamond at the end in the output is the eof symbol. In the C version you are printing the eof symbol first and then checking if the char is indeed eof symbol and if yes you come out of do-while loop. In C++ version you are first checking if the retrieved char is eof char before entering the loop, hence eof char is not printed. If you want to print correctly with do-while loop then you can do like this char ch = getc(fp); do { putchar(ch); ch = getc(fp); } while (ch != EOF); C/C++ string discussion although correct has got nothing to do in the current context. There are no strings(C or C++) involved here. You are reading data char by char.


Your program , modified yo print s range of lines.


Yes. I test eof before doing loop body. So It doesn't print garbage.


You're welcome!! :)