如何在 C++ 中保留文本文件的内存?
How to keep memory of text file in C++?
最近,我写了一个形状生成器项目,用C++显示三角形和矩形的形状。输出将显示在屏幕和文本文件中。但是我有一个问题,我如何让文本时间记住用户选择的所有形状?例如,形状总是存储在文本文件中。如果第一次 运行 我选择了 3 次 ..当我下次选择 2 次时退出 program.the 时,这 3 个选定的对象应该保留在文件中…然后退出程序。文本文件总共应该有 5 个对象。
这是我的代码:
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
void printTriangle(int n)
{
cout << "\t~~Triangle Generator~~" << endl;
cout << "\n\tEnter Height of Triangle: ";
cin >> n;
int k = 2 * n - 2;
cout << "\n";
// open a file in write mode.
ofstream outfile;
outfile.open("shape_generator.txt");
// character to be printed
char ch = 'O';
// outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++) {
cout << " ";
outfile << " ";
}
// decrementing k after each loop
k = k - 1;
// inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// printing stars
cout << ch << " ";
outfile << ch << " ";
}
// ending line after each row
cout << endl;
outfile << "\n";
}
cout << "\n";
}
void printRectangle(int height, int width)
{
cout << "\t~~Rectangle Generator~~" << endl;
int i, j;
/* Input number of rows from user */
cout << "\n\tEnter Height of Rectangle: ";
cin >> height;
cout << "\n\tEnter Width of Rectangle : ";
cin >> width;
cout << "\n";
// open a file in write mode.
ofstream outfile;
outfile.open("shape_generator.txt");
/*Iterate through N rows*/
for (i = 1; i <= height; i++) {
/*Iterate over columns*/
for (j = 1; j <= width; j++) {
/*Print star for each column*/
cout << "O";
outfile << "O";
}
/* Move to the next line/row */
cout << "\n";
outfile << "\n";
}
cout << "\n";
// close the opened file.
outfile.close();
}
void exit()
{
cout << "\n\tThank You For Using Our Services. Have A Nice Day!" << endl;
}
int main()
{
int n;
int height;
int width;
int option;
do {
system("color ED");
cout << "\t" << string(35, '_') << "\t\n";
cout << "\n\tWelcome to Shape Generator Service!\n";
cout << "\t" << string(35, '_') << "\t\n\n";
cout << "\t* * * * * * * * * * * * *" << endl;
cout << "\t* *" << endl;
cout << "\t*\t1.Triangle \t*" << endl;
cout << "\t* *" << endl;
cout << "\t*\t2.Rectangle\t*" << endl;
cout << "\t* *" << endl;
cout << "\t*\t0.Exit \t*" << endl;
cout << "\t* *" << endl;
cout << "\t* * * * * * * * * * * * *" << endl;
cout << "\n\tEnter Option: ";
if (cin >> option) {
switch (option) {
case 1:
system("cls");
system("color D7");
printTriangle(n);
break;
case 2:
system("cls");
system("color CF");
printRectangle(height, width);
break;
case 0:
exit();
break;
default:
cout << "\nThe Option is Not Available! Please Try Again.\n" << endl;
break;
}
}
else {
cout << "Only Digits are Allowed! Press any key to continue..." << endl;
getch();
cin.clear();
cin.ignore();
option = 15;
}
} while (option != 0);
}
因为你正在写一个文本文件并且有 #include <fstream
那么我认为你的意思是用附加打开文件,比如:
(例子)
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream file; //open the file
file.open("shapes.txt", fstream::out|fstream::app); //set mode as output and append
string phrase = "Example!\n\n"; //example phrase
file << phrase;
}
因为:
-fstream::app
是附加的,基本上会在文件末尾添加新内容而不删除其他内容
-fstream::out
是输出,基本上意味着您要放置一个输出,追加是一个输出
-file <<
基本上会在文件中添加以下参数
最近,我写了一个形状生成器项目,用C++显示三角形和矩形的形状。输出将显示在屏幕和文本文件中。但是我有一个问题,我如何让文本时间记住用户选择的所有形状?例如,形状总是存储在文本文件中。如果第一次 运行 我选择了 3 次 ..当我下次选择 2 次时退出 program.the 时,这 3 个选定的对象应该保留在文件中…然后退出程序。文本文件总共应该有 5 个对象。
这是我的代码:
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
void printTriangle(int n)
{
cout << "\t~~Triangle Generator~~" << endl;
cout << "\n\tEnter Height of Triangle: ";
cin >> n;
int k = 2 * n - 2;
cout << "\n";
// open a file in write mode.
ofstream outfile;
outfile.open("shape_generator.txt");
// character to be printed
char ch = 'O';
// outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++) {
cout << " ";
outfile << " ";
}
// decrementing k after each loop
k = k - 1;
// inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// printing stars
cout << ch << " ";
outfile << ch << " ";
}
// ending line after each row
cout << endl;
outfile << "\n";
}
cout << "\n";
}
void printRectangle(int height, int width)
{
cout << "\t~~Rectangle Generator~~" << endl;
int i, j;
/* Input number of rows from user */
cout << "\n\tEnter Height of Rectangle: ";
cin >> height;
cout << "\n\tEnter Width of Rectangle : ";
cin >> width;
cout << "\n";
// open a file in write mode.
ofstream outfile;
outfile.open("shape_generator.txt");
/*Iterate through N rows*/
for (i = 1; i <= height; i++) {
/*Iterate over columns*/
for (j = 1; j <= width; j++) {
/*Print star for each column*/
cout << "O";
outfile << "O";
}
/* Move to the next line/row */
cout << "\n";
outfile << "\n";
}
cout << "\n";
// close the opened file.
outfile.close();
}
void exit()
{
cout << "\n\tThank You For Using Our Services. Have A Nice Day!" << endl;
}
int main()
{
int n;
int height;
int width;
int option;
do {
system("color ED");
cout << "\t" << string(35, '_') << "\t\n";
cout << "\n\tWelcome to Shape Generator Service!\n";
cout << "\t" << string(35, '_') << "\t\n\n";
cout << "\t* * * * * * * * * * * * *" << endl;
cout << "\t* *" << endl;
cout << "\t*\t1.Triangle \t*" << endl;
cout << "\t* *" << endl;
cout << "\t*\t2.Rectangle\t*" << endl;
cout << "\t* *" << endl;
cout << "\t*\t0.Exit \t*" << endl;
cout << "\t* *" << endl;
cout << "\t* * * * * * * * * * * * *" << endl;
cout << "\n\tEnter Option: ";
if (cin >> option) {
switch (option) {
case 1:
system("cls");
system("color D7");
printTriangle(n);
break;
case 2:
system("cls");
system("color CF");
printRectangle(height, width);
break;
case 0:
exit();
break;
default:
cout << "\nThe Option is Not Available! Please Try Again.\n" << endl;
break;
}
}
else {
cout << "Only Digits are Allowed! Press any key to continue..." << endl;
getch();
cin.clear();
cin.ignore();
option = 15;
}
} while (option != 0);
}
因为你正在写一个文本文件并且有 #include <fstream
那么我认为你的意思是用附加打开文件,比如:
(例子)
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream file; //open the file
file.open("shapes.txt", fstream::out|fstream::app); //set mode as output and append
string phrase = "Example!\n\n"; //example phrase
file << phrase;
}
因为:
-fstream::app
是附加的,基本上会在文件末尾添加新内容而不删除其他内容
-fstream::out
是输出,基本上意味着您要放置一个输出,追加是一个输出
-file <<
基本上会在文件中添加以下参数