按字符读取字符串直到行 C/C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23726724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading string by char till end of line C/C++
提问by user3102621
How to read a string one char at the time, and stop when you reach end of line? I'am using fgetc function to read from file and put chars to array (latter will change array to malloc), but can't figure out how to stop when the end of line is reached
如何一次读取一个字符的字符串,并在到达行尾时停止?我正在使用 fgetc 函数从文件中读取并将字符放入数组(后者会将数组更改为 malloc),但无法弄清楚如何在到达行尾时停止
Tried this (c is the variable with char from file):
试过这个(c 是文件中带有字符的变量):
if(c=="one
two
three
")
But it gives error that I cant compare pointer to integer
但它给出了我无法将指针与整数进行比较的错误
File looks like (the length of the words are unknown):
文件看起来像(字长未知):
if(c=='#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;
}
')
So here comes the questions: 1) Can I compare c with \0 as \0 is two symbols (\ and 0) or is it counted as one (same question with \n) 2) Maybe I should use \n ? 3) If suggestions above are wrong what would you suggest (note I must read string one char at the time)
所以问题来了:1)我可以将 c 与 \0 进行比较,因为 \0 是两个符号(\ 和 0)还是算作一个(与 \n 相同的问题)2)也许我应该使用 \n ?3)如果上面的建议是错误的,你有什么建议(注意我当时必须读一个字符的字符串)
(Note I am pretty new to C++(and programming it self))
(注意我对 C++ 很陌生(并且自己编程))
回答by Vaughn Cato
You want to use single quotes:
你想使用单引号:
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;
}
}
Double quotes (") are for strings, which are sequences of characters. Single quotes (') are for individual characters.
双引号 (") 用于字符串,字符串是字符序列。单引号 (') 用于单个字符。
However, the end-of-line is represented by the newline character, which is '\n'.
但是,行尾由换行符表示,即 '\n'。
Note that in both cases, the backslash is not part of the character, but just a way you represent special characters. Using backslashes you can represent various unprintable characters and also characters which would otherwise confuse the compiler.
请注意,在这两种情况下,反斜杠都不是字符的一部分,而只是表示特殊字符的一种方式。使用反斜杠,您可以表示各种不可打印的字符以及可能会混淆编译器的字符。
回答by Ray Toal
The answer to your original question
你原来问题的答案
How to read a string one char at the time, and stop when you reach end of line?
如何一次读取一个字符的字符串,并在到达行尾时停止?
is, in C++, very simply, namely: use getline. The link shows a simple example:
在 C++ 中,非常简单,即:使用getline。该链接显示了一个简单的示例:
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 );
Do you really want to do this in C? I wouldn't! The thing is, in C, you have to allocate the memory in which to place the characters you read in? How many characters? You don't know ahead of time. If you allocate too few characters, you will have to allocate a new buffer every time to realize you reading more characters than you made room for. If you over-allocate, you are wasting space.
你真的想用 C 来做这件事吗?我不会!问题是,在 C 中,您必须分配内存来放置您读入的字符?多少个字符?你不知道提前。如果分配的字符太少,则每次都必须分配一个新缓冲区,以实现读取的字符数超出了空间。如果你过度分配,你就是在浪费空间。
C is a language for low-level programming. If you are new to programming and writing simple applications for reading files line-by-line, just use C++. It does all that memory allocation for you.
C 是一种用于低级编程的语言。如果您不熟悉编程和编写用于逐行读取文件的简单应用程序,请使用 C++。它为您完成所有内存分配。
Your later questions regarding "\0"
and end-of-lines in general were answered by others and do apply to C as well as C++. But if you are using C, please remember that it's not just the end-of-line that matters, but memory allocation as well. And you will have to be careful not to overrun your buffer.
您后来关于"\0"
行尾的一般问题已由其他人回答,并且确实适用于 C 和 C++。但是,如果您使用 C,请记住,重要的不仅仅是行尾,还有内存分配。你必须小心不要超出你的缓冲区。
回答by Vlad from Moscow
If you are using C function fgetc
then you should check a next character whether it is equal to the new line character or to EOF. For example
如果您正在使用 C 函数,fgetc
那么您应该检查下一个字符是否等于换行符或 EOF。例如
or maybe it would be better to rewrite the code using do-while loop. For example
或者使用 do-while 循环重写代码可能会更好。例如
##代码##Of course you need to insert your own processing of read xgaracters. It is only an example how you could use function fgetc
to read lines of a file.
当然需要插入自己的read xgaracters处理。这只是如何使用函数fgetc
读取文件行的示例。
But if the program is written in C++ then it would be much better if you would use std::ifstream
and std::string
classes and function std::getline
to read a whole line.
但是,如果程序是用C ++编写,然后它会更好,如果你会使用std::ifstream
和 std::string
类和功能std::getline
来读取一整行。
回答by ScottMcP-MVP
A text file does not have \0 at the end of lines. It has \n. \n is a character, not a string, so it must be enclosed in single quotes
文本文件在行尾没有 \0。它有\n。\n 是字符,不是字符串,所以必须用单引号括起来
if (c == '\n')
如果 (c == '\n')