使用istringstream丢失随机字符
Losing random characters with istringstream
我正在将文本文件的行读取为变量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 STRINGtwithnSPACES ");
[string]
[str("]
[SOME]
[LONG]
[STRINGtwithnSPACES]
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 STRINGtwithnSPACES ");
istringstream iss(str);
string s;
while (iss >> s) {
cout << "[" << s << "]" << endl;
}
return 0;
}
错误在这里:
else if (result == 1) {
cout<< myline << buff << flush;
istringstream iss(myline);
string sub;
while (iss >> sub) {
cout << "[" << sub << "]" << endl;
}
myline = "";
}
如果result == 1
,意味着buff
包含 n
,并不意味着只包含 n
。例如,如果包含n
,则删除缓冲区。因此,如果该行恰好有n*10 (sizeof buffer)个字符,您的代码可以工作,否则,该行的最后一个字符不会复制到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 = ' '; // drop the 'n'
return 1;
}
else {
return 0;
}
}
您必须将cout<< myline << flush;
更改为cout<< myline << endl;
。
除了这个错误,请考虑使用ifstream
:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream file("test.txt");
if(!file)
{
/* error */
}else
{
string line;
while(getline(file, line))
{
istringstream iss(line);
string s;
while (iss >> s) {
cout << "[" << s << "]" << endl;
}
}
}
}
您的行缓冲区只有10字节长。
void MyFileReader::tokenizeLine(vector<string>& vec) {
string myline("");
char buff[10];// this is too short
....
编辑
正如Dyp正确指出的那样,当您在输入文件中检测到n时,追加逻辑是不正确的。
相关文章:
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 在 c++ 的 10x10 数组中生成特定数量的多个随机字符
- 使用指针编译代码后,.cpp文件将变为随机字符
- 生成随机字符/数字
- 随机字符,从强制转换 Arduino 字符串到 Excel BSTR 通过 c++ DLL
- 字符数组中的十六进制导致写入套接字时出现随机字符
- 如何在网格中放置随机字符
- 解压缩 TIFF 数据会打印随机字符
- 尝试以随机字符(长读取)打印字符串返回结果
- UNIX API 调用:使用 read() 函数打开文件并将其打印到屏幕上会增加额外的随机字符
- 如何修复C++中的随机字符输出?
- 为什么从函数返回c弦乐会导致随机字符
- 生成唯一的随机字符并将其保存在数组中
- 编译器将随机字符添加到我的 char 数组中
- 字符串更改程序输出随机字符
- 挑选无重复C 的随机字符
- 随机字符作为在样本中使用 libaws 时返回
- C/C++ - 生成一个随机字符串会覆盖以前的字符串(请参阅下面的代码)