The answer to your original question
How to read a char string at a time and stop when you reach the end of the string?
C ++ is very simple, namely: use getline . The link shows a simple example:
#include <iostream> #include <string> int main () { std::string name; std::cout << "Please, enter your full name: "; std::getline (std::cin,name); std::cout << "Hello, " << name << "!\n"; return 0; }
Do you really want to do this in C? I would not! The thing is, in C, you have to allocate memory in which to put the characters you read? How many characters? You do not know in advance. If you select too few characters, you will have to allocate a new buffer each time to understand how you read more characters than you have space. If you redistribute, you lose space.
C is a low level programming language. If you are new to programming and writing simple applications to read files in turn, just use C ++. It does all the memory allocation for you.
Your later questions regarding "\0" and the end of lines as a whole have been edited by others and apply to both C and C ++. But if you use C, remember that this applies not only to the end of the line, but also to memory allocation. And you have to be careful not to overload your buffer.
source share