流不规则模式
ifstream irregular pattern
我很难准确地指出我的文件输入出错的地方。下面是代码:
char tempFirst[20], tempLast[20], tempCourse[7];
char c; // For peeking
// Find out how many students are in the file
inFile >> numStudents;
for (int i = 0; i < numStudents; i++)
{
// Get last name from file
inFile.getline(tempLast, 20, ',');
// Gets rid of any spaces inbetween last and first
while (isspace(inFile.peek()))
c = inFile.get();
// Get first name from file
inFile.getline(tempFirst, 20, 'n');
// New line, get course
inFile >> tempCourse;
// PRINT
cout << tempFirst << "n" << tempLast << "n"
<< tempCourse << "n";
list[i]->SetGrades(inFile);
}
SetGrade将导致以下三个继承函数之一:
void EnglishStudent::SetGrades(ifstream& inFile)
{
inFile >> attendance >> project >> midterm >> final;
cout << attendance << " " << project << " " << midterm << " " << final << "nn";
}
void HistoryStudent::SetGrades(ifstream& inFile)
{
inFile >> paper >> midterm >> final;
cout << paper << " " << midterm << " " << final << "nn";
}
void MathStudent::SetGrades(ifstream& inFile)
{
inFile >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5
>> test1 >> test2 >> final;
cout << quiz1 << " "<< quiz2 << " " << quiz3 << " " << quiz4 << " " << quiz5
<< " " << test1 << " " << test2 << " " << final << "nn";
}
这是我加载信息的文件:
6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
History 88 75 90
Dipwart, Marvin
English 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
English 40 100 68 88
Lewinsky, Monica
History 60 72 78
然后输出如下:
Bugs
Bunny
Math
90 86 80 95 100 99 96 93
Joe
History
88 75 90
Marvin
English
95 76 72 88
Jimmy
Crack Corn
Math
44 58 23 76 50 59 77 68
James T.
English
40 100 68 88
Monica
History
60 72 78
我遗漏了大多数姓氏,对于第一个学生,firstname有一个尾线。我该如何解决这个问题?
不是名字的末尾有一个换行符,而是姓氏的开头有一个换行符。当从输入中读取int
s时,遇到的任何标记int
结束的空白都将留在输入流中。
要解决这个问题,在SetGrades
方法中或在循环结束时,在读取姓氏之前删除空白。后两者也需要在阅读numStudents
后删除空白。删除空格最简单的方法是使用ws
流操纵符。只需要:
inFile >> ws;
你也可以用这个代替你的peek
循环。
将字符数组替换为字符串以获得更真实的c++体验。这也需要用getline
free函数替换ifstream::getline
。作为奖励,您的代码将适用于长度超过19个字符的名称。
std::string tempFirst, tempLast, tempCourse;
...
for (int i=0; i < numStudents; ++i) {
inFile >> std::ws;
getline(inFile, last, ',');
inFile >> std::ws;
getline(inFile, first, 'n');
...
在移动到下一行之前跳过当前行的其余部分
inFile >> numStudents;
std::string line;
std::getline(inFile, line);//read the rest of the first line, you should do this before you start to read next line
for (int i = 0; i < numStudents; i++)
{
std::getline(inFile, line); //line contains first name and last name
size_t pos = line.find(",");
std::cout << line.substr(0, pos) << std::endl //first name
<< line.substr(pos + 2) << std::endl; //last name,skip ", "
std::getline(inFile, line); // course name and grades
//you could split the course name and grades now with line
std::cout << line << std::endl;
}
相关文章:
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么在保护模式下继承升级不起作用
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 此模式的C++RegEx
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 使用可变模板的Broadcaster/Listener模式
- c++方法参数只能在linux的发布模式下自行更改
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 资源管理设计模式
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 小字符串优化(调试与发布模式)
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- 如何设计具有不同类型的通知和观察器的观察者模式?
- 在C++的一系列数字中查找重复模式
- 是否允许使用带有"w+"模式的 freopen 进行标准设置?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 流不规则模式