📄 7.02-文件尾添加内容.cpp
字号:
#include <fstream.h>
int main() // returns 1 on error
{
char fileName[80];
char buffer[255];
cout << "Please re-enter the file name: ";
cin >> fileName;
ifstream fin(fileName);
if (fin) // already exists?
{
cout << "Current file contents:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << "\n***End of file contents.***\n";
}
fin.close();
cout << "\nOpening " << fileName << " in append mode...\n";
ofstream fout(fileName,ios::app);
if (!fout)
{
cout << "Unable to open " << fileName << " for appending.\n";
return(1);
}
cout << "\nEnter text for the file: ";
cin.ignore(1,'\n');
cin.getline(buffer,255);
fout << buffer << "\n"; // add a return;
fout.close();
fin.open(fileName); // reassign existing fin object!
if (!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
return(1);
}
cout << "\nHere's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << "\n***End of file contents.***\n";
fin.close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -