使用 ifstream 读取文件

发布于 2024-08-08 08:59:01 字数 777 浏览 1 评论 0原文

我正在尝试从文件中读取: 该文件是多行的,基本上我需要检查每个“单词”。词是任何非空间的东西。

示例输入文件为:

示例文件:

测试二维
字3.5
输入
{

测试 13.5 12.3
另一个{
测试 145.4
}
}

我尝试了这样的事情:

ifstream inFile(fajl.c_str(), ifstream::in);

if(!inFile)
{
    cout << "Cannot open " << fajl << endl;
    exit(0);
}

string curr_str;
char curr_ch;
int curr_int;
float curr_float;

cout << "HERE\n";
inFile >> curr_str;

cout << "Read " << curr_str << endl;

问题是当它读取新行时它就会挂起。我在测试 13.5 之前阅读了所有内容 但一旦到达该线,它就不会做任何事情。 任何人都可以告诉我我做错了什么? 关于如何做到这一点有更好的建议吗???

我基本上需要浏览文件并当时浏览一个“单词”(非白色字符)。 我

谢谢

I am trying to read from file:
The file is multiline and basically i need to go over each "word". Word being anything non space.

Sample input file would be:

Sample file:

test 2d
word 3.5
input
{

test 13.5 12.3
another {
testing 145.4
}
}

So I tried something like this:

ifstream inFile(fajl.c_str(), ifstream::in);

if(!inFile)
{
    cout << "Cannot open " << fajl << endl;
    exit(0);
}

string curr_str;
char curr_ch;
int curr_int;
float curr_float;

cout << "HERE\n";
inFile >> curr_str;

cout << "Read " << curr_str << endl;

The problem is when it reads new line it just hangs. I read everything before test 13.5
but once it reaches that line it doesnt do anything.
Anyone can tell me what I am doing wrong?
Any better suggestion on how to do this???

I essentially need to go through file and go one "word" (non white char) at the time.
I

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

今天小雨转甜 2024-08-15 08:59:01

您打开文件“inFile”但正在从“std::cin”读取任何特殊原因?

/*
 * Open the file.
 */
std::ifstream   inFile(fajl.c_str());   // use input file stream don't.
                                        // Then you don't need explicitly specify
                                        // that input flag in second parameter
if (!inFile)   // Test for error.
{
    std::cerr << "Error opening file:\n";
    exit(1);
}

std::string   word;
while(inFile >> word)  // while reading a word succeeds. Note >> operator with string
{                      // Will read 1 space separated word.
    std::cout << "Word(" << word << ")\n";
}

You open a file 'inFile' but are reading from the 'std::cin' any particular reason?

/*
 * Open the file.
 */
std::ifstream   inFile(fajl.c_str());   // use input file stream don't.
                                        // Then you don't need explicitly specify
                                        // that input flag in second parameter
if (!inFile)   // Test for error.
{
    std::cerr << "Error opening file:\n";
    exit(1);
}

std::string   word;
while(inFile >> word)  // while reading a word succeeds. Note >> operator with string
{                      // Will read 1 space separated word.
    std::cout << "Word(" << word << ")\n";
}
早乙女 2024-08-15 08:59:01

不确定这在 iostream 库的“精神”中如何,但您可以使用未格式化的输入来做到这一点。比如:

char tempCharacter;
std::string currentWord;
while (file.get(tempCharacter))
{
    if (tempCharacter == '\t' || tempCharacter == '\n' || tempCharacter == '\r' || tempCharacter == ' ')
    {
        std::cout << "Current Word: " << currentWord << std::endl;
        currentWord.clear();
        continue;
    }
    currentWord.push_back(tempCharacter);
}

这有效吗?

Not sure how "in the spirit" of the iostream library this is, but you could do it with unformatted input. Something like:

char tempCharacter;
std::string currentWord;
while (file.get(tempCharacter))
{
    if (tempCharacter == '\t' || tempCharacter == '\n' || tempCharacter == '\r' || tempCharacter == ' ')
    {
        std::cout << "Current Word: " << currentWord << std::endl;
        currentWord.clear();
        continue;
    }
    currentWord.push_back(tempCharacter);
}

Does that work?

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