获取文件的一部分

c++ Getting a portion of a file

本文关键字:一部分 文件 获取      更新时间:2023-10-16

如何跳过文件的某些部分,然后跳过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);

这只会在某些情况下起作用,但你应该能够操纵它来适应你想要做的。