C++ Fstream 只打印一个字

发布于 2024-11-28 01:39:17 字数 457 浏览 1 评论 0原文

这是一个很奇怪的问题。我正在尝试打印一个大文本文件,它是维基百科条目。它恰好是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流年里的时光 2024-12-05 01:39:17
wiki >> wikiRead;

流的默认分隔符是空格,因此当流遇到空格时,它会停止读取,这就是为什么它只读取一个单词。

如果您希望流读取所有单词,则必须使用循环:

char* wikiRead = new char[1024]; //must allocate some memory!
while(wiki >> wikiRead)
{
   cout << wikiRead << endl;
}
wiki.close();
delete []wikiRead; //must deallocate the memory

这将打印文件中的所有单词,每个单词都在一个新行上。请注意,如果文件中的任何单词长度超过 1024 个字符,则该程序将调用未定义的行为,并且程序可能崩溃。在这种情况下,您必须分配更大的内存块。

但为什么首先要使用 char* 呢?在 C++ 中,您有更好的选择:使用 std::string

#include<string>

std::string word;
while(wiki >> word)
{
   cout << word << endl;
}
wiki.close();

现在好多了。

如果您想逐行读取而不是逐字读取,请使用 std::getline 作为:

std::string line;
while(std::getline(wiki, line))
{
   cout << line << endl;
}
wiki.close();

这将读取完整的行,即使该行在单词之间包含空格,并将每行打印一个换行符。

wiki >> wikiRead;

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:

char* wikiRead = new char[1024]; //must allocate some memory!
while(wiki >> wikiRead)
{
   cout << wikiRead << endl;
}
wiki.close();
delete []wikiRead; //must deallocate the memory

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: Use std::string.

#include<string>

std::string word;
while(wiki >> word)
{
   cout << word << endl;
}
wiki.close();

Its better now.

If you want to read line-by-line, instead of word-by-word, then use std::getline as:

std::string line;
while(std::getline(wiki, line))
{
   cout << line << endl;
}
wiki.close();

This will read a complete line, even if the line contains spaces between the words, and will print each line a newline.

毁梦 2024-12-05 01:39:17

您要求流读取指针的(二进制)值(可能是 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!

愁杀 2024-12-05 01:39:17

我想知道为什么您忽略编译器警告(大多数现代编译器都会警告您有关使用未初始化的变量)。这个怎么样?

    ifstream wiki;
    wiki.open("./wiki/velocity.txt");
    char wikiRead[255];
    wiki >> wikiRead;
    cout << wikiRead << endl;
    wiki.close();

或者,我建议您将 string 对象与 getline 一起使用来获取单行文本。

string str;
getline(wiki, str);

I wonder why you ignored the compiler warning (most of the modern compiler warns you about using uninitialized variables). How about this?

    ifstream wiki;
    wiki.open("./wiki/velocity.txt");
    char wikiRead[255];
    wiki >> wikiRead;
    cout << wikiRead << endl;
    wiki.close();

Alternatively I'd suggest you to use string object with getline to get a single line of text.

string str;
getline(wiki, str);
中二柚 2024-12-05 01:39:17

应用于 char *>> 运算符仅读取一个单词。此外,您正在读入一个未初始化的指针,这是无效的。通常使用 std::string 而不是 char * 在 C++ 中进行字符串处理。

如果您只想打印文件的内容,则可以将文件的缓冲区直接挂接到 std::cout

int main() {
    std::ifstream wiki("./wiki/velocity.txt");
    std::cout << wiki.rdbuf() << '\n';
}

如果您想将内容放入自动分配的字符串中,请使用 std: :getline 并禁用分隔符。

int main() {
    std::ifstream wiki("./wiki/velocity.txt");
    std::string wiki_contents;
    getline( wiki, wiki_contents, '\0' /* do not stop at newline */ );

    std::cout << wiki_contents << '\n'; // do something with the string
}

The >> operator applied to a char * reads only one word. Moreover, you're reading into an uninitialized pointer, which is not valid. Usually std::string, not char *, 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:

int main() {
    std::ifstream wiki("./wiki/velocity.txt");
    std::cout << wiki.rdbuf() << '\n';
}

If you want to put the contents into an automatically-allocated string, use std::getline with the delimiter disabled.

int main() {
    std::ifstream wiki("./wiki/velocity.txt");
    std::string wiki_contents;
    getline( wiki, wiki_contents, '\0' /* do not stop at newline */ );

    std::cout << wiki_contents << '\n'; // do something with the string
}
笑,眼淚并存 2024-12-05 01:39:17

由于要读取大文件,逐块读取是更好的方法。

ifstream wiki;
wiki.open("./wiki/velocity.txt");
const int buf_size = 1024;
char* wikiRead = 0;
int cnt = 1;
do
{
   wikiRead = realloc( wikiRead, bufsize*cnt );
   wiki.Read( wikiRead + (bufSize*(cnt-1)), buf_size ); //appends to reallocated memory
   cnt++;
}while( !wiki.eof())
wikiRead[(bufSize*(cnt-2)) + wiki.gcount() + 1] = '\0'; // null termination.
wiki.Close();
cout << wikiRead;
delete[] wikiRead;

Since you want to read a large file, reading it block by block is a better way.

ifstream wiki;
wiki.open("./wiki/velocity.txt");
const int buf_size = 1024;
char* wikiRead = 0;
int cnt = 1;
do
{
   wikiRead = realloc( wikiRead, bufsize*cnt );
   wiki.Read( wikiRead + (bufSize*(cnt-1)), buf_size ); //appends to reallocated memory
   cnt++;
}while( !wiki.eof())
wikiRead[(bufSize*(cnt-2)) + wiki.gcount() + 1] = '\0'; // null termination.
wiki.Close();
cout << wikiRead;
delete[] wikiRead;
離殇 2024-12-05 01:39:17

operator>> 被设计为一次仅读取一个单词。如果您想读取行,请使用getline

#include <iostream>
#include <fstream>
#include<string>

using namespace std;

int main()
{
    ifstream wiki;
    wiki.open("./wiki/velocity.txt");

    string   wikiRead;

    while (getline(wiki, wikiRead))
    {
        cout << wikiRead << endl;
    }

    wiki.close();
}

The operator>> is designed to only read one word at a time. If you want to read lines, use getline.

#include <iostream>
#include <fstream>
#include<string>

using namespace std;

int main()
{
    ifstream wiki;
    wiki.open("./wiki/velocity.txt");

    string   wikiRead;

    while (getline(wiki, wikiRead))
    {
        cout << wikiRead << endl;
    }

    wiki.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文