如何从一行中读取特定数量的字符? (C++)

How can I read in a specific number of characters from a line? (C++)

我正在做家庭作业,我需要对 .txt 文件的第一行进行错误检查。在这种情况下,文本文件的第一行应为 "Number of samples: ",后跟一个未指定的数字。如果该行不以 "Number of samples: " 开头,我需要在显示错误消息后终止程序。假设该行确实以正确的字符串开头,我需要将数字作为无符号整数读入。

总而言之,我需要从 .txt 文件行中读入 19 个字符。我该怎么做,同时仍然能够读取该行的其余部分以将其存储为变量?

我使用的库是 iostream、string、ofstream 和 ifstream(如果重要的话)。

ifstream x;
char addToString;
string check = "Number of samples: ";
string temp = "";
int num;
x.open("file.txt");

for (int i = 0; i < 19; i++){
     x.get(addToString);
     temp += addToString;
}

if (check == temp){
     x >> num;
     // other stuff
}

else {
     cout << "error message" << endl;
}

x.close();

return 0;