Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 文本文件解析C++;|用空格分隔。_C++_Parsing_Text - Fatal编程技术网

C++ 文本文件解析C++;|用空格分隔。

C++ 文本文件解析C++;|用空格分隔。,c++,parsing,text,C++,Parsing,Text,我有一个这样的文本文件 Path 827 196 847 195 868 194 889 193 909 191 929 191 951 189 971 186 991 185 1012 185 Path 918 221 927 241 931 261 931 281 930 301 931 321 927 341 923 361 921 382 我正在使用getline函数读取文本文件中的每一行,我想将一行中的2个数字解析为两个不同的整数变量 int main() { vector&

我有一个这样的文本文件

Path 
827 196
847 195
868 194
889 193
909 191
929 191
951 189
971 186
991 185
1012 185
Path
918 221
927 241
931 261
931 281
930 301
931 321
927 341
923 361
921 382
我正在使用getline函数读取文本文件中的每一行,我想将一行中的2个数字解析为两个不同的整数变量

int main()
{

  vector<string> text_file;

  int no_of_paths=0;

  ifstream ifs( "ges_t.txt" );
  string temp;

  while( getline( ifs, temp ) )
  {
          if (temp2.compare("path") != 0)
          {
//Strip temp string and separate the values into integers here.
          }

  }


}
intmain()
{
矢量文本文件;
int no_of_路径=0;
ifstream ifs(“ges_t.txt”);
字符串温度;
while(getline(ifs,temp))
{
if(temp2.比较(“路径”)!=0)
{
//剥离临时字符串,并在此处将值分隔为整数。
}
}
}
类似这样的内容:

#include <string>
#include <sstream>
#include <fstream>

std::ifstream ifs("ges_t.txt");

for (std::string line; std::getline(ifs, line); )
{
    if (line == "Path") { continue; }

    std::istringstream iss(line);
    int a, b;

    if (!(iss >> a >> b) || iss.get() != EOF) { /* error! die? */ }

    std::cout << "You said, " << a << ", " << b << ".\n";
}
#包括
#包括
#包括
std::ifstream ifs(“ges_t.txt”);
for(std::string-line;std::getline(ifs,line);)
{
如果(行=“路径”){continue;}
标准::istringstream iss(线);
INTA,b;
如果(!(iss>>a>>b)| iss.get()!=EOF){/*错误!死亡?*/}
std::cout类似这样的内容:

#include <string>
#include <sstream>
#include <fstream>

std::ifstream ifs("ges_t.txt");

for (std::string line; std::getline(ifs, line); )
{
    if (line == "Path") { continue; }

    std::istringstream iss(line);
    int a, b;

    if (!(iss >> a >> b) || iss.get() != EOF) { /* error! die? */ }

    std::cout << "You said, " << a << ", " << b << ".\n";
}
#包括
#包括
#包括
std::ifstream ifs(“ges_t.txt”);
for(std::string-line;std::getline(ifs,line);)
{
如果(行=“路径”){continue;}
标准::istringstream iss(线);
INTA,b;
如果(!(iss>>a>>b)| iss.get()!=EOF){/*错误!死亡?*/}

std::cout给定一个包含两个整数的字符串:

std::istringstream src( temp );
src >> int1 >> int2 >> std::ws;
if ( ! src || src.get() != EOF ) {
    //  Format error...
}
请注意,您可能希望在比较之前删除空白
“路径”
(尾随空格可能特别有害,
因为在普通编辑器中看不到它。)

给定一个包含两个整数的字符串:

std::istringstream src( temp );
src >> int1 >> int2 >> std::ws;
if ( ! src || src.get() != EOF ) {
    //  Format error...
}
请注意,您可能希望在比较之前删除空白
“路径”
(尾随空格可能特别有害,

既然在普通编辑器中看不到它。)

为什么不用调用atoi()就可以将字符串从stringstream中分离出来并保存为整数函数.<代码> ATOI 是C的做事方式。使用IOFSUBES和C++是做事情的方式。@ Raja:投票是堆栈溢出的方式,说“好”谢谢!):我在完成所有的事情之后,我会去做。你怎么能把字符串从String流中分离出来,并把它们保存成整数,而不用调用O。f原子()函数.<代码> ATOI 是C的做事方式。使用IOFSUBES和C++是做事情的方式。@ Raja:投票是堆栈溢出的方式,说“OK,谢谢!”:在我完成所有的事情之后,我会去做这个。这给了一个错误:变量“STD::ISTIGISSWISS ISS”有初始化器但不完整类型@拉贾特:你是否包含了标题?请注意,尽管进行了错误检查,这仍然会接受像
123 456 abc
@JamesKanze这样的行:OP没有请求对其输入进行格式验证,是吗?(如果你愿意,当然可以附加
iss.getc()&&&!iss
)@KerrekSB OP不必这样做。任何时候从外部源读取时,都要验证格式是否正确。这会给出一个错误:变量“std::istringstream iss”具有初始值设定项,但不完整type@rajat:是否包含标题?请注意,尽管进行了错误检查,但仍会接受像
123 456 abc
@JamesK这样的行anze:OP没有要求对他的输入进行格式验证,是吗?(如果你愿意,你当然可以附加一个
iss.getc()&&&!iss
)@KerrekSB OP不必这样做。任何时候你从外部源读取,你都可以验证格式是否正确。