文件输入截断第一个字
File Input cuts off first word
我正在编写一个程序,它应该能够接收输入、创建文件并存储该信息。我已经成功地完成了那部分。该程序还应该能够将以前的文件打印到屏幕上。我遇到的问题是它截断了文件调用第一行的第一个单词。例如,我会单击 n
并输入一个先前的文本文件,例如 test.txt
,它会打印以下内容:
Kennedy
CSC 201
Computer Science
Mondays and Wednesdays
Scott Davis
何时打印:
John Kennedy
CSC 201
Computer Science
Mondays and Wednesdays
Scott Davis
我打印文件做错了什么?
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
ifstream inData;
ofstream outData;
string inFile, outFile, fullName, fileName;
string courseCode, courseName, courseProfessor, courseHours;
char answer;
int counter = 0;
int courses;
cout << "Are you creating a new file? y/n" << endl;
cin >> answer;
if(answer == 'n')
{
cout << "Enter the name of your file." << endl;
cin >> inFile;
cin.ignore (200, '\n');
inData.open(inFile.c_str());
inData >> fileName;
cout << inData.rdbuf();
}
else if(answer == 'y')
{
cout << "Enter the name of your file." << endl;
cin >> outFile;
cin.ignore (200, '\n');
outData.open(outFile.c_str());
cout << "What is your full name?" << endl;
getline(cin, fullName);
outData << fullName << endl;
outData << endl;
cout << "How many courses are you taking?" << endl;
cin >> courses;
while(courses > counter)
{
cin.ignore (200, '\n');
cout << "What is the code for your class?" << endl;
getline(cin, courseCode);
outData << courseCode << endl;
cout << "What is the name of the course?" << endl;
getline(cin, courseName);
outData << courseName << endl;
cout << "What days and time periods do you take this course?" << endl;
getline(cin, courseHours);
outData << courseHours << endl;
cout << "What is the name of your professor?" << endl;
getline(cin, courseProfessor);
outData << courseProfessor << endl;
outData << endl;
counter++;
}
}
inData.close();
outData.close();
return 0;
}
你的问题是 inData >> fileName;
在 inData.open(inFile.c_str());
之后
它将第一个单词读入文件名并移动当前文件位置。
我正在编写一个程序,它应该能够接收输入、创建文件并存储该信息。我已经成功地完成了那部分。该程序还应该能够将以前的文件打印到屏幕上。我遇到的问题是它截断了文件调用第一行的第一个单词。例如,我会单击 n
并输入一个先前的文本文件,例如 test.txt
,它会打印以下内容:
Kennedy
CSC 201
Computer Science
Mondays and Wednesdays
Scott Davis
何时打印:
John Kennedy
CSC 201
Computer Science
Mondays and Wednesdays
Scott Davis
我打印文件做错了什么?
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
ifstream inData;
ofstream outData;
string inFile, outFile, fullName, fileName;
string courseCode, courseName, courseProfessor, courseHours;
char answer;
int counter = 0;
int courses;
cout << "Are you creating a new file? y/n" << endl;
cin >> answer;
if(answer == 'n')
{
cout << "Enter the name of your file." << endl;
cin >> inFile;
cin.ignore (200, '\n');
inData.open(inFile.c_str());
inData >> fileName;
cout << inData.rdbuf();
}
else if(answer == 'y')
{
cout << "Enter the name of your file." << endl;
cin >> outFile;
cin.ignore (200, '\n');
outData.open(outFile.c_str());
cout << "What is your full name?" << endl;
getline(cin, fullName);
outData << fullName << endl;
outData << endl;
cout << "How many courses are you taking?" << endl;
cin >> courses;
while(courses > counter)
{
cin.ignore (200, '\n');
cout << "What is the code for your class?" << endl;
getline(cin, courseCode);
outData << courseCode << endl;
cout << "What is the name of the course?" << endl;
getline(cin, courseName);
outData << courseName << endl;
cout << "What days and time periods do you take this course?" << endl;
getline(cin, courseHours);
outData << courseHours << endl;
cout << "What is the name of your professor?" << endl;
getline(cin, courseProfessor);
outData << courseProfessor << endl;
outData << endl;
counter++;
}
}
inData.close();
outData.close();
return 0;
}
你的问题是 inData >> fileName;
在 inData.open(inFile.c_str());
它将第一个单词读入文件名并移动当前文件位置。