文本文件问题 (Visual C++)
Text file Issues (Visual C++)
我正在尝试让程序提示用户输入文件名,然后提示他们输入要删除的单词。
有没有一种方法可以在不在 main 之外创建任何功能的情况下这样做?
(提前致谢!)
到目前为止,这是我的代码:
ofstream outFile;
string in;
char in2[800];
char fileName = ' ';
while (in != "XXXX") {
cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
cin >> in;
for (int i = 0; i < in.length(); i++)
{
fileName += in[i];
}
outFile.open(in + ".txt");
if (outFile.fail())
{
outFile << "Can't do it.";
}
else
{
cout << "What do you want in it?\n";
cin >> in2;
cin.getline(in2, sizeof(in2));
outFile << in2;
}
outFile.close();
ifstream reader;
string oneLine;
reader.open(in + ".txt");
if (reader.fail())
{
cout << "Could not read file.\n";
return 0;
}
else
{
reader.clear();
reader.seekg(60L, ios::beg);
getline(reader, oneLine);
cout << "Is there something you'd like to remove from " << in << ".txt?\n";
cin >> in2;
ofstream write("tmp.txt");
while (getline(reader, oneLine)) {
if (oneLine != in2)
write << oneLine << endl;
}
reader.close();
write.close();
remove(fileName + ".txt");
rename("tmp.txt", fileName + ".txt");
return 0;
}
return 0;
}
while (in != "XXXX") {
cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
...
}
如果您输入 XXXX
循环将不会退出。它将尝试处理其余代码。
char fileName = ' ';
声明单个字符。你想要一个字符数组。更好的是,将其声明为 std::string fileName;
remove(fileName + ".txt");
无法编译,因为 remove
是一个 C 函数。 C 不支持 类 并且它不知道如何处理 类 例如 std::string
。你想提供一个 C 字符串,你可以使用 fileName.c_str()
以下是一些建议:
int main()
{
ofstream outFile;
string fileName;
string text;
for(;;)
{
cout << "What do you want to name the file?\n";
cout << "Enter a name or enter 999 to quit.\n";
cin >> fileName;
if(fileName == "999")
break;
fileName += ".txt";
outFile.open(fileName);
if(outFile.fail())
{
cout << "Can't do it.";
continue;
}
else
{
for(;;)
{
cout << "What do you want in it?\n";
cout << "Enter a name or enter 999 to stop.\n";
cin >> text;
if(text == "999")
break;
outFile << text << endl;
}
}
outFile.close();
ifstream reader;
reader.open(fileName);
if(reader.fail())
{
cout << "Could not read file.\n";
}
else
{
cout << "Is there something you'd like to remove from " << fileName << "?\n";
cin >> text;
string oneLine;
ofstream write("tmp.txt");
while(getline(reader, oneLine))
{
if(oneLine != text)
write << oneLine << endl;
}
reader.close();
write.close();
remove(fileName.c_str());
rename("tmp.txt", fileName.c_str());
break;
}
}
}
我正在尝试让程序提示用户输入文件名,然后提示他们输入要删除的单词。 有没有一种方法可以在不在 main 之外创建任何功能的情况下这样做? (提前致谢!) 到目前为止,这是我的代码:
ofstream outFile;
string in;
char in2[800];
char fileName = ' ';
while (in != "XXXX") {
cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
cin >> in;
for (int i = 0; i < in.length(); i++)
{
fileName += in[i];
}
outFile.open(in + ".txt");
if (outFile.fail())
{
outFile << "Can't do it.";
}
else
{
cout << "What do you want in it?\n";
cin >> in2;
cin.getline(in2, sizeof(in2));
outFile << in2;
}
outFile.close();
ifstream reader;
string oneLine;
reader.open(in + ".txt");
if (reader.fail())
{
cout << "Could not read file.\n";
return 0;
}
else
{
reader.clear();
reader.seekg(60L, ios::beg);
getline(reader, oneLine);
cout << "Is there something you'd like to remove from " << in << ".txt?\n";
cin >> in2;
ofstream write("tmp.txt");
while (getline(reader, oneLine)) {
if (oneLine != in2)
write << oneLine << endl;
}
reader.close();
write.close();
remove(fileName + ".txt");
rename("tmp.txt", fileName + ".txt");
return 0;
}
return 0;
}
while (in != "XXXX") { cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n"; ... }
如果您输入 XXXX
循环将不会退出。它将尝试处理其余代码。
char fileName = ' ';
声明单个字符。你想要一个字符数组。更好的是,将其声明为 std::string fileName;
remove(fileName + ".txt");
无法编译,因为 remove
是一个 C 函数。 C 不支持 类 并且它不知道如何处理 类 例如 std::string
。你想提供一个 C 字符串,你可以使用 fileName.c_str()
以下是一些建议:
int main()
{
ofstream outFile;
string fileName;
string text;
for(;;)
{
cout << "What do you want to name the file?\n";
cout << "Enter a name or enter 999 to quit.\n";
cin >> fileName;
if(fileName == "999")
break;
fileName += ".txt";
outFile.open(fileName);
if(outFile.fail())
{
cout << "Can't do it.";
continue;
}
else
{
for(;;)
{
cout << "What do you want in it?\n";
cout << "Enter a name or enter 999 to stop.\n";
cin >> text;
if(text == "999")
break;
outFile << text << endl;
}
}
outFile.close();
ifstream reader;
reader.open(fileName);
if(reader.fail())
{
cout << "Could not read file.\n";
}
else
{
cout << "Is there something you'd like to remove from " << fileName << "?\n";
cin >> text;
string oneLine;
ofstream write("tmp.txt");
while(getline(reader, oneLine))
{
if(oneLine != text)
write << oneLine << endl;
}
reader.close();
write.close();
remove(fileName.c_str());
rename("tmp.txt", fileName.c_str());
break;
}
}
}