我正在尝试编写一个文件检查功能,但似乎有些错误(不是总是这样)。请记住,我刚开始使用C ++。
main();
的相关部分int main() {
string file, pick, first, firstFirst, second, third, fourth, note;
fstream inputFile, outputfile;
bool done = false;
void display(string second);
void copy(string second, string fourth);
void create(string second);
string getFileName(ios_base::openmode mode);
do {
stringstream choice;
//Ask for prompt
cout << "Enter one of the Following:\n\
quit - Quit the program\n\
Copy <f1> to <f2>\n\
display <f1>\n\
create <f1> <word to quit>\n\
Enter Option: ";
getline(cin, pick); //insert their response into a string
choice << pick; //copy string to a stringstream
choice >> firstFirst; //copy each word to seperate variables
//cin.clear();
first.resize(firstFirst.size());
transform(firstFirst.begin(), firstFirst.end(),
first.begin(),::tolower);
if (first == "display")
{
second = getFileName(ios::in); //file check & get second
if (second == "bad")
{
cout << "That file does not exist";
break;
}
else {
cout << "Displaying" << second;
cout << endl << endl;
display(second);
}
这是我的getFileName函数(文件检查器)
string getFileName(ios_base::openmode mode)
{
string fileName;
fstream inputFile;
cin >> fileName; //pull file name string off buffer
if (mode == ios::in)
{
while (fileName.at(0) == '"')
{
fileName.erase(
remove(fileName.begin(), fileName.end(), '\"'),
fileName.end()
);
}
inputFile.open(fileName, ios::in);
{
if (inputFile.good() != true)
{
inputFile.close();
fileName = "bad";
}
else
{
inputFile.close();
}
}
}
else if (mode == ios::out)
{
while (fileName.at(0) == '"')
{
fileName.erase(
remove(fileName.begin(), fileName.end(), '\"'),
fileName.end()
);
}
inputFile.open(fileName, ios::in);
{
if (inputFile.good() != true)
{
inputFile.close();
fileName = "bad";
}
else
{
inputFile.close();
}
}
}
else
{
fileName = "bad";
}
return fileName;
}
感谢百万提供任何帮助。