C++ Fstream 只打印一个字
这是一个很奇怪的问题。我正在尝试打印一个大文本文件,它是维基百科条目。它恰好是 Velocity 上的页面。因此,当我告诉它打印文件时,它会打印“In”,而它应该打印“在物理学中,速度是等等,等等”。 这是我用来打印的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream wiki;
wiki.open("./wiki/velocity.txt");
char* wikiRead;
wiki >> wikiRead;
cout << wikiRead << endl;
wiki.close();
}
请帮忙。
This is a very strange issue. I'm trying to print a large text file, it's a Wikipedia entry. It happens to be the page on Velocity. So, when I tell it to print the file, it prints "In", when it should print "In physics, velocity is etc, etc etc".
Here's the code I'm using to print out:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream wiki;
wiki.open("./wiki/velocity.txt");
char* wikiRead;
wiki >> wikiRead;
cout << wikiRead << endl;
wiki.close();
}
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
流的默认分隔符是空格,因此当流遇到空格时,它会停止读取,这就是为什么它只读取一个单词。
如果您希望流读取所有单词,则必须使用循环:
这将打印文件中的所有单词,每个单词都在一个新行上。请注意,如果文件中的任何单词长度超过
1024
个字符,则该程序将调用未定义的行为,并且程序可能崩溃。在这种情况下,您必须分配更大的内存块。但为什么首先要使用
char*
呢?在 C++ 中,您有更好的选择:使用std::string
。现在好多了。
如果您想逐行读取而不是逐字读取,请使用
std::getline
作为:这将读取完整的行,即使该行在单词之间包含空格,并将每行打印一个换行符。
The default delimiter for stream is space, so when the stream encounters a space, it simply stops reading, that is why it reads only one word.
If you want the stream to read all words, the you've to use a loop as:
This will print all the words in the file, each on a new line. Note if any of the word in the file is more than
1024
character long, then this program would invoke undefined behavior, and the program might crash. In that case, you've to allocate a bigger chunk of memory.But why use
char*
in the first place? In C++, you've better choice: Usestd::string
.Its better now.
If you want to read line-by-line, instead of word-by-word, then use
std::getline
as:This will read a complete line, even if the line contains spaces between the words, and will print each line a newline.
您要求流读取指针的(二进制)值(可能是 4 个字节,具体取决于您的机器架构),然后要求它打印这 4 个字节指向的文本!
You ask the stream to read the (binary) value of a pointer (probably 4 bytes, depending on your machine architecture), then you ask it to print the text pointed to by those 4 bytes!
我想知道为什么您忽略编译器警告(大多数现代编译器都会警告您有关使用未初始化的变量)。这个怎么样?
或者,我建议您将
string
对象与getline
一起使用来获取单行文本。I wonder why you ignored the compiler warning (most of the modern compiler warns you about using uninitialized variables). How about this?
Alternatively I'd suggest you to use
string
object withgetline
to get a single line of text.应用于
char *
的>>
运算符仅读取一个单词。此外,您正在读入一个未初始化的指针,这是无效的。通常使用std::string
而不是char *
在 C++ 中进行字符串处理。如果您只想打印文件的内容,则可以将文件的缓冲区直接挂接到
std::cout
:如果您想将内容放入自动分配的字符串中,请使用
std: :getline
并禁用分隔符。The
>>
operator applied to achar *
reads only one word. Moreover, you're reading into an uninitialized pointer, which is not valid. Usuallystd::string
, notchar *
, is used for string processing in C++.If you only want to print the file's contents, you can hook the file's buffer directly to
std::cout
:If you want to put the contents into an automatically-allocated string, use
std::getline
with the delimiter disabled.由于要读取大文件,逐块读取是更好的方法。
Since you want to read a large file, reading it block by block is a better way.
operator>>
被设计为一次仅读取一个单词。如果您想读取行,请使用getline
。The
operator>>
is designed to only read one word at a time. If you want to read lines, usegetline
.