为什么我的程序不备份我的文件?
why won't my program back up my file?
我正在处理一个 C++ 项目,我需要在同一个程序中创建数据文件后对其进行备份。我已经创建了数据文件并成功地向其中写入了文本,但是,当我尝试使用我编写的函数备份同一个文件时,它不起作用。
这是函数后面的一些上下文:
我创建的文件名为 contactList.ext(.ext,所以它是在当前目录中创建的)。当提示输入此程序中的文件名时,我输入 contactList 并成功打开,但是,我遇到的唯一问题是它不会备份文件。我试图以这种方式备份它: newFileName = (fileName + ".bak");
我不知道还有什么其他方法可以使用 c++ 备份文件。非常感谢任何帮助!
void backupDataFile() {
string fileName;
string newFileName;
string line;
int contactListSize = 10, i = 0;
string contacts[contactListSize];
char userResponse;
fstream inFile, outFile;
cout << "\nEnter the name of the file you want to backup: ";
cin >> fileName;
inFile.open(fileName.c_str()); //attempts to open file
//file fails to open
if (inFile.fail()) {
cout << "\nThe file " << fileName << " was not opened successfully."
<< "\n Please check that the file currently exists.\n";
exit(1);
}
//read and display contents of file & assign each line to an array
cout << "\nThe following is the contents of " << fileName << ":\n\n";
while(getline(inFile, line)) {
cout << line << endl;
contacts[i] = line; //assigns each line to an array position
i++;
}
inFile.close(); //closes existing file allowing the opening of a new file
//verify user wishes to backup file
cout << "\nWould you like to backup this file? <y/n>: ";
cin >> userResponse;
if (userResponse == 'y') {
newFileName = (fileName + ".bak"); //assigns name of backup file
outFile.open(newFileName.c_str()); //attempts to open backup file
//file fails to create
if (outFile.fail()) {
cout << "\nThe file " << fileName << " did not backup successfully.";
exit(1);
}
///fix hereafter
else { //writes contents from contactList.ext to contactList.bak
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
}
outFile.close(); //closes file
cout << "\nThe file " << fileName << " has been backed-up successfully."
<< "\nThe backup file is named " << newFileName;
}//end outer-if
else
cout << "\nYou will be directed back to the Main Menu.";
}
你的问题出在这两个部分。
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
for 循环不应被注释掉,您只是在需要写入输出文件时才写入控制台(使用 cout)。
调用outFile.out()时还需要指定ios::out。
outFile.open(newFileName.c_str(),ios::out)
正如另一位发帖人所述,您实际上 运行 并不是输出到文件
的代码
//outFile << contacts[j] << endl;
然而我看到的另一个问题是只要i小于10你就循环输出。这很好,但是你在读取文件时计算行数后没有将i设置回0 !这意味着您的
while(i < 10) {
循环永远不会运行:)
我正在处理一个 C++ 项目,我需要在同一个程序中创建数据文件后对其进行备份。我已经创建了数据文件并成功地向其中写入了文本,但是,当我尝试使用我编写的函数备份同一个文件时,它不起作用。
这是函数后面的一些上下文:
我创建的文件名为 contactList.ext(.ext,所以它是在当前目录中创建的)。当提示输入此程序中的文件名时,我输入 contactList 并成功打开,但是,我遇到的唯一问题是它不会备份文件。我试图以这种方式备份它: newFileName = (fileName + ".bak");
我不知道还有什么其他方法可以使用 c++ 备份文件。非常感谢任何帮助!
void backupDataFile() {
string fileName;
string newFileName;
string line;
int contactListSize = 10, i = 0;
string contacts[contactListSize];
char userResponse;
fstream inFile, outFile;
cout << "\nEnter the name of the file you want to backup: ";
cin >> fileName;
inFile.open(fileName.c_str()); //attempts to open file
//file fails to open
if (inFile.fail()) {
cout << "\nThe file " << fileName << " was not opened successfully."
<< "\n Please check that the file currently exists.\n";
exit(1);
}
//read and display contents of file & assign each line to an array
cout << "\nThe following is the contents of " << fileName << ":\n\n";
while(getline(inFile, line)) {
cout << line << endl;
contacts[i] = line; //assigns each line to an array position
i++;
}
inFile.close(); //closes existing file allowing the opening of a new file
//verify user wishes to backup file
cout << "\nWould you like to backup this file? <y/n>: ";
cin >> userResponse;
if (userResponse == 'y') {
newFileName = (fileName + ".bak"); //assigns name of backup file
outFile.open(newFileName.c_str()); //attempts to open backup file
//file fails to create
if (outFile.fail()) {
cout << "\nThe file " << fileName << " did not backup successfully.";
exit(1);
}
///fix hereafter
else { //writes contents from contactList.ext to contactList.bak
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
}
outFile.close(); //closes file
cout << "\nThe file " << fileName << " has been backed-up successfully."
<< "\nThe backup file is named " << newFileName;
}//end outer-if
else
cout << "\nYou will be directed back to the Main Menu.";
}
你的问题出在这两个部分。
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
for 循环不应被注释掉,您只是在需要写入输出文件时才写入控制台(使用 cout)。
调用outFile.out()时还需要指定ios::out。
outFile.open(newFileName.c_str(),ios::out)
正如另一位发帖人所述,您实际上 运行 并不是输出到文件
的代码//outFile << contacts[j] << endl;
然而我看到的另一个问题是只要i小于10你就循环输出。这很好,但是你在读取文件时计算行数后没有将i设置回0 !这意味着您的
while(i < 10) {
循环永远不会运行:)