C ++用条件语句读取文本文件 [英] C++ reading a text file with conditional statements

查看:200
本文介绍了C ++用条件语句读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取文本文件中的行,标记行,然后继续执行下一行的开关和中断块,但是在我的程序到达第一个中断之后,它退出循环,忽略文件的其余部分。

  ifstream in(test.txt); 
字符串行,缓冲区;
unsigned int firstLetter = 0;
// istringstream iss;

if(in.is_open())
{
istringstream iss;
while(getline(in,line))
{
iss.str(line);
char firstChar = line.at(firstLetter);
switch(firstChar)
{
case'D':
while(getline(iss,buffer,' - '))
{//打印每个令牌,每行一个
cout<<buffer:<< buffer<"<<< endl;
}
break;
case'R':
while(getline(iss,buffer,' - '))
{cout<<buffer:<<<<<<<<<<<<<<< ;< endl;}
break;





$ b

这是文本文件:

 泰坦的D-Rise-25.50-2009-12 
D-23 Jump Street-20.00 -2013-5
D-Home Alone-24.00-1995-16
D-Twin Warriors-24.00-2011-14
R-XXX-Pierce-Hawthorne-11-13
R-Forbidden Kingdom-Pierce-Hawthorne-13-31
R-23 Jump Street-Troy-Barnes-1-3
R-Home Alone-Shirley-Bennett-1-3
R-Modern Family-Abed-Nadir-10-66

但是当我运行我的代码时, :

$ $ p $ 缓冲区:D
缓冲区:Titans的上升
缓冲区:25.50
缓冲区:2009
buffer:12

这是文本文件中第1行的内容,我怎么能去到下一行,并继续这样做,直到文件的结尾?

解决方案

你正在改变你的内容你的 std :: istringstream 对象,但是sta te标志保持为先前(即,你的eof位被点亮)。一个例子,使用你的输入数据通过stdin进行修改:

  #include< iostream> 
#include< fstream>
#include< sstream>
#include< string>
使用namespace std;

int main()
{
istream& in = std :: cin;
字符串行,缓冲区;

istringstream iss;
while(getline(in,line))
{
if(!line.empty())
{
iss.str(line);
std :: cout<< 在eof的行:<< iss.eof()<< \\\
;

char firstChar = line.at(0);
switch(firstChar)
{
case'D':
while(getline(iss,buffer,' - '))
cout <<<缓冲区: <<<<<<<<<<< endl;
break;

case'R':
while(getline(iss,buffer,' - '))
cout <<<缓冲区:<<<<<<<< << endl;
break;



$ b code


<输出

 在eof的行:0 
buffer:D
buffer:Rise Titans的
缓冲区:25.50
缓冲区:2009
缓冲区:12
eof的行:1
eof的行:1
eof的行: 1
线在eof:1
线在eof:1
线在eof:1
线在eof:1
线在eof:1

要解决这个问题,要么在每次迭代之前清除流的状态,要么只是在while循环内移动字符串流,用行初始化。前者在下面完成。我将后者作为一个练习:

  #include< iostream> 
#include< fstream>
#include< sstream>
#include< string>
使用namespace std;

int main()
{
istream& in = std :: cin;
字符串行,缓冲区;

istringstream iss; $())
while(getline(in,line))
{
if(!line.empty())
{
iss.clear(); //<< ===加到这里
iss.str(line);
std :: cout<< 在eof的行:<< iss.eof()<< \\\
;

char firstChar = line.at(0);
switch(firstChar)
{
case'D':
while(getline(iss,buffer,' - '))
cout <<<缓冲区: <<<<<<<<<<< endl;
break;

case'R':
while(getline(iss,buffer,' - '))
cout <<<缓冲区:<<<<<<<< << endl;
break;



$ b code


<输出

 在eof的行:0 
buffer:D
buffer:Rise Titans
buffer:25.50
buffer:2009
buffer:12
line at eof:0
buffer:D
buffer:23 Jump Street
缓冲区:20.00
缓冲区:2013
缓冲区:5
行eof:0
缓冲区:D
缓冲区:单独存放
缓冲区: 24.00
缓冲区:1995
缓冲区:16
行eof:0
缓冲区:D
缓冲区:双人战士
缓冲区:24.00
缓冲区:2011
缓冲区:14
行eof:0
缓冲区:R
缓冲区:XXX
缓冲区:Pierce
缓冲区:Hawthorne
缓冲区:11
缓冲区:13
行eof:0
缓冲区:R
缓冲区:禁止的王国
缓冲区:Pierce
缓冲区:Hawthorne
缓冲区:13
缓冲区:31
行eof:0
缓冲区:R
缓冲区:23跳转街
缓冲区:特洛伊
缓冲区:巴恩斯
缓冲区:1
缓冲区:3
行eof:0
缓冲区:R
缓冲区:单独存放
缓冲区:Shirley
缓冲区: Bennett
缓冲区:1
缓冲区:3
行eof:0
缓冲区:R
缓冲区:现代族
缓冲区:Abed
buffer:Nadir
buffer:10
buffer:66


I am trying to read lines in a text file, tokenize the line and then go on and do the same to the next line in a switch and break block, but after my program reaches the first break, it exits the loop and ignores the rest of the file.

ifstream in("test.txt");
    string line,buffer;
    unsigned int firstLetter = 0;
    //istringstream iss;

    if( in.is_open() )
        {
            istringstream iss;
            while(getline(in,line))
            {
                iss.str(line);
                char firstChar = line.at( firstLetter );
                switch ( firstChar )
                {
                    case 'D':
                    while (getline(iss,buffer,'-'))
                    {//print each token, one per line
                        cout<<"buffer: "<<buffer<<" "<<endl;
                    }
                    break;
                    case 'R':
                    while ( getline(iss,buffer,'-') )
                    {cout<<"buffer: "<<buffer<<" "<<endl;}
                    break;
                }
            }               
        }

this is the content of the text file:

D-Rise of the Titans-25.50-2009-12
D-23 Jump Street-20.00-2013-5
D-Home Alone-24.00-1995-16
D-Twin Warriors-24.00-2011-14
R-XXX-Pierce-Hawthorne-11-13
R-Forbidden Kingdom-Pierce-Hawthorne-13-31
R-23 Jump Street-Troy-Barnes-1-3
R-Home Alone-Shirley-Bennett-1-3
R-Modern Family-Abed-Nadir-10-66

but when i run my code i get this:

buffer: D 
buffer: Rise of the Titans 
buffer: 25.50 
buffer: 2009 
buffer: 12

which is the content of line 1 in the text file, how can i go to the next line and keep doing that till the end of the file?

解决方案

You're changing your content of your std::istringstream object, but the state flags remain as prior (i.e., your eof bit is lit). An example, modified using your input data via stdin:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    istream& in = std::cin;
    string line,buffer;

    istringstream iss;
    while(getline(in,line))
    {
        if (!line.empty())
        {
            iss.str(line);
            std::cout << "line at eof: " << iss.eof() << '\n';

            char firstChar = line.at(0);
            switch ( firstChar )
            {
                case 'D':
                    while (getline(iss,buffer,'-'))
                        cout<<"buffer: "<<buffer<<" "<<endl;
                    break;

                case 'R':
                    while ( getline(iss,buffer,'-') )
                        cout<<"buffer: "<<buffer<<" "<<endl;
                    break;
            }
        }
    }
}

Output

line at eof: 0
buffer: D 
buffer: Rise of the Titans 
buffer: 25.50 
buffer: 2009 
buffer: 12 
line at eof: 1
line at eof: 1
line at eof: 1
line at eof: 1
line at eof: 1
line at eof: 1
line at eof: 1
line at eof: 1

To fix this, either clear the state of the stream before each iteration, or just move the stringstream inside the while-loop, initialized with the line. The former is done below; I leave the latter as an exercise for you:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    istream& in = std::cin;
    string line,buffer;

    istringstream iss;
    while(getline(in,line))
    {
        if (!line.empty())
        {
            iss.clear(); // <<=== ADDED HERE
            iss.str(line);
            std::cout << "line at eof: " << iss.eof() << '\n';

            char firstChar = line.at(0);
            switch ( firstChar )
            {
                case 'D':
                    while (getline(iss,buffer,'-'))
                        cout<<"buffer: "<<buffer<<" "<<endl;
                    break;

                case 'R':
                    while ( getline(iss,buffer,'-') )
                        cout<<"buffer: "<<buffer<<" "<<endl;
                    break;
            }
        }
    }
}

Output

line at eof: 0
buffer: D 
buffer: Rise of the Titans 
buffer: 25.50 
buffer: 2009 
buffer: 12 
line at eof: 0
buffer: D 
buffer: 23 Jump Street 
buffer: 20.00 
buffer: 2013 
buffer: 5 
line at eof: 0
buffer: D 
buffer: Home Alone 
buffer: 24.00 
buffer: 1995 
buffer: 16 
line at eof: 0
buffer: D 
buffer: Twin Warriors 
buffer: 24.00 
buffer: 2011 
buffer: 14 
line at eof: 0
buffer: R 
buffer: XXX 
buffer: Pierce 
buffer: Hawthorne 
buffer: 11 
buffer: 13 
line at eof: 0
buffer: R 
buffer: Forbidden Kingdom 
buffer: Pierce 
buffer: Hawthorne 
buffer: 13 
buffer: 31 
line at eof: 0
buffer: R 
buffer: 23 Jump Street 
buffer: Troy 
buffer: Barnes 
buffer: 1 
buffer: 3 
line at eof: 0
buffer: R 
buffer: Home Alone 
buffer: Shirley 
buffer: Bennett 
buffer: 1 
buffer: 3 
line at eof: 0
buffer: R 
buffer: Modern Family 
buffer: Abed 
buffer: Nadir 
buffer: 10 
buffer: 66 

这篇关于C ++用条件语句读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆