How could I print specific lines of a code in console? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 20

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

22nd Feb 2019, 10:02 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
13 Answers
+ 12
C++ Soldier (Babak) Thanks for your explanation!!
23rd Feb 2019, 4:42 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 11
diego Code Reviewing your code, I see that the null character as Rowsej stated is not printed. Is it because of your explanation?!
22nd Feb 2019, 10:47 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 11
diego Code Rowsej Thanks for explaining & providing nice resolutions. Now, I can do what I want to. Thanks a lot!!!
22nd Feb 2019, 10:53 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 11
~ swim ~ Thanks for expanding the former replies!!!
24th Feb 2019, 4:15 AM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 10
So, if I need different ranges, should I repeat the same process?!
22nd Feb 2019, 10:58 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 10
Rowsej Yep, I prefer that approach!
22nd Feb 2019, 11:02 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 7
"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.
23rd Feb 2019, 3:46 PM
Babak
Babak - avatar
+ 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
22nd Feb 2019, 10:33 PM
unChabon
unChabon - avatar
+ 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.
22nd Feb 2019, 10:44 PM
Rowsej
Rowsej - avatar
+ 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.
22nd Feb 2019, 10:59 PM
Rowsej
Rowsej - avatar
+ 4
Your program , modified yo print s range of lines. https://code.sololearn.com/cirOM2lr70zs/?ref=app
22nd Feb 2019, 10:46 PM
unChabon
unChabon - avatar
+ 4
Yes. I test eof before doing loop body. So It doesn't print garbage.
22nd Feb 2019, 10:50 PM
unChabon
unChabon - avatar
+ 4
You're welcome!! :)
22nd Feb 2019, 10:54 PM
unChabon
unChabon - avatar