Read string using char to end of line C / C ++

How to read a char string at a time and stop when you reach the end of the string? I use the fgetc function to read from a file and put the characters in an array (the latter changes the array to malloc), but cannot figure out how to stop when the end of the line is reached

Tried this (c is the variable with char from the file):

if(c=="\0") 

But it gives an error that I cannot compare a pointer to an integer

The file looks (word length unknown):

 one two three 

So, here are the questions: 1) Is it possible to compare c with \ 0 as \ 0 - two characters (\ and 0) or is it considered one (the same question with \ n) 2) Maybe I should use \ n? 3) If the above sentences are incorrect, what would you suggest (note that I have to read the string one char at a time)

(Note. I am new to C ++ (and I program myself))

+6
source share
4 answers

You want to use single quotes:

 if(c=='\0') 

Double quotes (") are for strings that are sequences of characters. Single quotes (') are for single characters.

However, the end of the line is represented by the newline character, which is "\ n".

Note that in both cases, the backslash is not part of the character, but simply a way of representing special characters. Using a backslash, you can represent various non-printable characters, as well as characters that would otherwise confuse the compiler.

+15
source

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.

+3
source

If you use the C function fgetc , then you should check the next character to see if it matches a newline or EOF. for instance

 unsigned int count = 0; while ( 1 ) { int c = fgetc( FileStream ); if ( c == EOF || c == '\n' ) { printF( "The length of the line is %u\n", count ); count = 0; if ( c == EOF ) break; } else { ++count; } } 

or it might be better to rewrite the code using a do-while loop. for instance

 unsigned int count = 0; do { int c = fgetc( FileStream ); if ( c == EOF || c == '\n' ) { printF( "The length of the line is %u\n", count ); count = 0; } else { ++count; } } while ( c != EOF ); 

Of course, you need to insert your own processing of read xgaracters. This is just an example of how you can use the fgetc function to read lines of a file.

But if the program is written in C ++, it would be much better if you used the std::ifstream and std::string classes and the std::getline function to read an entire line.

+2
source

The text file does not have \ 0 at the end of lines. It has a \ n. \ N character, not a string, so it must be enclosed in single quotes

if (c == '\ n')

0
source

Source: https://habr.com/ru/post/969476/


All Articles