c++获取文件的一部分
c++ Getting a portion of a file
如何跳过文件的某些部分,然后 ifstream
直到到达某个字符?
例如,在一个文本文件中,这样写:
'SaveNameExample'
variableExample = 5;
我如何只获取没有 '
s 的 SaveNameExample ,将其存储为 std::string
变量,然后仅从下一行获取 5 以保存为 int
变量?
按照这些思路应该可行:
string s = "";
char c = '';
int i = 0;
while(getline(fstream_name, temp)){
for(int i = 0; i < temp.length(); i++){
if(temp[i] != "'")
s += temp[i];
if(temp[i] >= x || temp[i] <= y) //Where x and y are ascii values for 0 and 9, respectively
c = temp[i];
}
}
i = atoi(c);
这仅在某些情况下有效,但您应该能够操纵它以适应您正在尝试做的事情。
如何跳过文件的某些部分,然后 ifstream
直到到达某个字符?
例如,在一个文本文件中,这样写:
'SaveNameExample'
variableExample = 5;
我如何只获取没有 '
s 的 SaveNameExample ,将其存储为 std::string
变量,然后仅从下一行获取 5 以保存为 int
变量?
按照这些思路应该可行:
string s = "";
char c = '';
int i = 0;
while(getline(fstream_name, temp)){
for(int i = 0; i < temp.length(); i++){
if(temp[i] != "'")
s += temp[i];
if(temp[i] >= x || temp[i] <= y) //Where x and y are ascii values for 0 and 9, respectively
c = temp[i];
}
}
i = atoi(c);
这仅在某些情况下有效,但您应该能够操纵它以适应您正在尝试做的事情。