Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++_String_Inputstream_Seek - Fatal编程技术网

C++ 从包含特定字符串的行开始读取

C++ 从包含特定字符串的行开始读取,c++,string,inputstream,seek,C++,String,Inputstream,Seek,我正试图从输入文件中读取一些xyz坐标。 这是我的输入文件: input.inp POSITIONS 1.5 2.5 1.5 C 3.2 1.5 4.5 C 1.4 4.2 3.2 C 我想编写一个函数,在输入文件中搜索包含“C”的字符串,然后开始从该行读取坐标。如何在C++中实现这一点?(我不想搜索单词位置,因为输入文件的该部分稍后可能会更改)。您应该读取字符串变量的所有行。分析它,如果你想阅读这些数字,你可以使用stringstrea

我正试图从输入文件中读取一些xyz坐标。 这是我的输入文件:

input.inp

POSITIONS
1.5    2.5    1.5    C
3.2    1.5    4.5    C
1.4    4.2    3.2    C

我想编写一个函数,在输入文件中搜索包含
“C”
的字符串,然后开始从该行读取坐标。如何在C++中实现这一点?(我不想搜索单词
位置
,因为输入文件的该部分稍后可能会更改)。

您应该读取
字符串
变量的所有行。分析它,如果你想阅读这些数字,你可以使用
stringstream

//检查线路末端是否有C或其他测试,然后

stringstream b(a, stringstream::in);

double c1=0, c2=0, c3=0;
b >> c1 >> c2 >> c3;

将读取该行中的数字。

getline、seekg、ignore等的多种组合。
ifstream file("in");
string line;
while (getline(file, line)) { 
    if (line.find(" C") == string::npos)
        continue;
    istringstream ss(line);
    double x, y, z;
    ss >> x >> y >> z;
    if (!ss)
        continue; // error
    // ok
}
ifstream file("in");
string line;
while (getline(file, line)) { 
    if (line.find(" C") == string::npos)
        continue;
    istringstream ss(line);
    double x, y, z;
    ss >> x >> y >> z;
    if (!ss)
        continue; // error
    // ok
}