+19

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? https://code.sololearn.com/c3iNofIQ55ok/?ref=app https://code.sololearn.com/cm1855fzWF5M/?ref=app

2/22/2019 10:02:07 PM

Geovanny Martínez Forero

14 Answers

New Answer

+12

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

+11

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

+11

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

+11

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

+10

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

+10

Rowsej Yep, I prefer that approach!

+6

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. https://code.sololearn.com/cc1f605tK9bZ/?ref=app

+6

https://code.sololearn.com/ctqT5fI3CBJQ/?ref=app 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.

+6

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.

+6

"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.

+4

Your program , modified yo print s range of lines. https://code.sololearn.com/cirOM2lr70zs/?ref=app

+4

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

+4

You're welcome!! :)

+4

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.