基本上,我试图从一行代码中获得三种东西,即从.txt文件中读取。这将被重复,但现在我只需要知道如何得到一行。我读到的台词是这样的:
Biology $11 12所以我想要一个字符串和两个in,完全忽略$(注意,我不能更改.txt文件,所以我必须保留$ in)
所以如果我有
string subject;
int biologyscores[25];我的代码是:
int biologyscores[25]; //array to store the integer values
std::string subject; //string to store the subject the scores are for
std::ifstream infile("myScores.txt"); //declare infile to read from "myScores.txt"
infile >> subject >> biologyscores[0] >> biologyscores [1]; //read into string and int array因此,字符串将被用来检查这些分数是为了哪个主题,而分数本身将存储在一个数组生物分数中,它们彼此索引。
问题是,在执行这段代码之后,subject =“生物学”是理想的,但指数0和1的生物分数似乎都有垃圾数,而不是我想要读到的。
发布于 2021-12-03 20:29:14
您可以使用一个虚拟字符变量,它将“拾取”$字符:
char dummy_char;
infile >> subject >> dummy_char >> biologyscores[0] >> biologyscores [1]; //read into string and int array发布于 2021-12-03 21:36:24
您可以使用seekg和tellg。
std::fstream infile("myScores.txt"); //declare infile to read from "myScores.txt"
int temp;
infile >> subject; //read into string
temp=infile.tellg(); //to get the position of $
infile.seekg(temp+1);// to move one step forward
infile>> biologyscores[0] >> biologyscores [1];//now you can read the int valueshttps://stackoverflow.com/questions/70220263
复制相似问题