使用istringstream丢失随机字符

时间:2013-05-03 21:49:14

标签: c++ c++11

我正在将文本文件的行读入变量myline,然后我尝试使用istringstream对这些行进行标记。但是,我似乎丢失了原始文本文件字符串中的随机字符。

cout<< myline << buff << flush; //print original text file line
istringstream iss(myline);
string sub;
while (iss >> sub) {
cout << "[" << sub << "]" << endl;
} 

如果查看我的输出,你可以看到我从文本文件中找到了正确的字符串,但是当我使用istringstream然后打印单个标记(在[]括号中看到)时,一些标记会过早地被截断

#include <iostream>
[#include]
[<iostream]
#include <sstream>
[#include]
[<sstream>]
using namespace std;
[using]
[namespace]
[st]

int main()
[int]
[main(]
{
    string str("   SOME  LONG    STRING\twith\nSPACES    ");
[string]
[str("]
[SOME]
[LONG]
[STRING\twith\nSPACES]

    istringstream iss(str);
[istringstream]
[iss(str);]

    string s;
[strin]
    while (iss >> s) {
[while]
[(iss]
[>>]
        cout << "[" << s << "]" << endl;
[cout]
[<<]
["["]
[<<]
[s]
[<<]
["]"]
[<<]
[e]
    }
    return 0;
[retur]
}

有人有任何想法我做错了吗?提前谢谢!

编辑:这是一个完全编译的代码版本。您可以使用任何文本文件

运行它
#include <cstring>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

class MyFileReader {

public:
    //constructor
    MyFileReader(const char* p);

    //destructor
    ~MyFileReader();

    //getLine()
    int getLine(char *buffer, int size);

    //getCurrentLineNumber()
    int getCurrentLineNumber();

    void tokenizeLine(vector<string>& vec);

    FILE * pFile;

};

    //constructor
    MyFileReader::MyFileReader(const char* p) {
        pFile = fopen(p, "r");
    }

    //destructor
    MyFileReader::~MyFileReader() {
        fclose(pFile);
    }

    //getLine()
    int MyFileReader::getLine(char *buffer, int size){
        char *out = fgets(buffer, size, pFile);
        if (out==NULL) {
            return -1;
        }
        char *pch = strpbrk(out,"\n");
        if (pch != NULL) {
            return 1;
        }
        else {
            return 0;
        }

    }

    int MyFileReader::getCurrentLineNumber() {
        static int mynumber=2;
        return mynumber++;
    }

    //tokenizeLine
    void MyFileReader::tokenizeLine(vector<string>& vec) {
        string myline("");
        char buff[10];
        while (1) {
            int result = getLine(buff, sizeof(buff));
            if (result == -1 ) {
                if (myline.length() > 0) 
                    cout << myline << flush;
            break;
            }
            else if (result == 0) {
                myline += buff;
            }
            else if (result == 1) {
                cout<< myline << buff << flush;
                istringstream iss(myline);
                string sub;
                while (iss >> sub) {
                    cout << "[" << sub << "]" << endl;
                } 
                myline = "";
            }
            else {
                printf("PANIC");
            }
            }
            return;
        }

    int main(int argc, char **argv) {
    vector<string> v;

    const char *filename = argv[1];
    MyFileReader f(filename);
    f.tokenizeLine(v);
    return 0;

    }

要生成上面的输出,我运行它:

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

int main()
{
    string str("   SOME  LONG    STRING\twith\nSPACES    ");

    istringstream iss(str);

    string s;
    while (iss >> s) {
        cout << "[" << s << "]" << endl;
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

错误在于:

else if (result == 1) {
            cout<< myline << buff << flush;
            istringstream iss(myline);
            string sub;
            while (iss >> sub) {
                cout << "[" << sub << "]" << endl;
            } 
            myline = "";
        }

如果result == 1,则表示buff 包含 \n,并不意味着包含{{1} }}。即如果缓冲区包含\n,则删除缓冲区。因此,如果该行恰好具有n * 10(sizeof buffer)字符,则代码可以正常工作,否则,行的最后一个字符不会复制到\n但会丢弃。

快速解决方案是:

myline

虽然你可能想考虑从缓冲区中删除 else if (result == 1) { myline += buff; // copy the rest of the line into `myline` cout<< myline << flush; // buff now is part of myline istringstream iss(myline); string sub; while (iss >> sub) { cout << "[" << sub << "]" << endl; } myline = ""; } ,例如:

\n

但是,您必须将int MyFileReader::getLine(char *buffer, int size){ char *out = fgets(buffer, size, pFile); if (out==NULL) { return -1; } //char *pch = strpbrk(out,"\n"); char *pch = strchr(out,'\n'); // no need to search for a string if (pch != NULL) { *pch = '\0'; // drop the '\n' return 1; } else { return 0; } } 更改为cout<< myline << flush;


除了这个错误,请考虑使用cout<< myline << endl;

ifstream

答案 1 :(得分:1)

你的行缓冲区只有10个字节长。这不足以满足整条生产线。

void MyFileReader::tokenizeLine(vector<string>& vec) {
        string myline("");
        char buff[10];// this is too short
....

修改

当您在输入文件中检测到\ n时,Dyp正确指出您的附加逻辑不正确。