如何打开并写入一个名称取自C++中字符串的文件

How do I open and write to a file with the name taken from a string in C++?

本文关键字:C++ 文件 字符串 一个 何打开      更新时间:2023-10-16

在制作项目时,我试图完成这个简单的任务1.根据if情况打开文件2.让用户输入.txt文件的名称3.将用户给出的名称与.txt文件合并为一个简单的文件名示例:"测试"&";。txt";4.让用户传入信息并将其写入文件

如果你对感到困惑,这是代码

if (wordChoose=="ofile")
{
cout <<("      File Name?")<< endl;
string fileNaming;
cin >> fileNaming;

FILE *f=fopen((fileNaming+".txt").c_str());

const char *text;
cin >> *text;
fprintf(text);

`by the way, this is for a school project`
system ("pause");
return 0;
}

编辑:我会给出完整的代码,祝好运

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string inputComm;
int PCOS()
{
string accountSettings;
cin >> inputComm;

if (inputComm=="comp.start")
{
cout <<("               Pear .P.C.O.S. Computer System n")<< endl;

cout <<("     Main Menu:")<< endl;
cout <<("     1<        SETTINGS")<< endl;
cout <<("     2<        APPLICATIONS")<< endl;
cout <<("     3<        RADIO NETWORKS")<< endl;
cout <<("     4<        CREDITS")<< endl;

string menuChoose;

cin >> menuChoose;

if(menuChoose=="pea.app")
{
cout <<("     Applications:")<< endl;
cout <<("     1<           CALCULATOR")<< endl;
cout <<("     2<           FMAKE DOCUMENT APPLICATION")<< endl;
cout <<("     3<           HUCKY")<< endl;

string appChoose;

cin >> appChoose;

if (appChoose=="app.fmake")
{
cout <<("      Fmake Document Application:")<< endl;
cout <<("      1<                        MAKE FILE")<< endl;
cout <<("      2<                        OPEN FILE")<< endl;

string wordChoose;
cin >> wordChoose;

if (wordChoose=="fmake.ofile");
{
cout <<("      File Name?")<< endl;
string fileNaming;
cin >> fileNaming;

FILE *f=fopen((fileNaming+".txt").c_str());

const char *text;
cin >> *text;
fprintf(text);


system ("pause");
return 0;
}

if (wordChoose=="fmake.mfile")
{
cout <<("      What is the name of the file?")<< endl;
string fileName;
cin >> fileName;

fstream ((fileName+".txt").c_str());

cout <<("      To continue, restart the os.")<< endl;
system ("pause");
return 0;
}       
}

if(appChoose=="app.calc")
{
cout <<("     Calculator:")<< endl;
cout <<("     1<         ADDITION")<< endl;
cout <<("     2<         SUBTRACTION")<< endl;
cout <<("     3<         MULTIPLICATION")<< endl;
cout <<("     4<         DIVISION")<< endl;

string equationType;

cin >> equationType;

if (equationType=="calc.div")
{
cout <<("      Enter the first number")<< endl;

double firstDIVnumber;
cin >> firstDIVnumber;

cout <<("      Go on")<< endl;

double secondDIVnumber;
cin >> secondDIVnumber;

double sumDIV=firstDIVnumber / secondDIVnumber;
cout << sumDIV;
cout <<("      To continue, restart the os")<< endl;
system("pause");
return 0;
}

if (equationType=="calc.mtt")
{
cout <<("      Enter the first number")<< endl;

double firstMTTnumber;
cin >> firstMTTnumber;

cout <<("      Go on")<< endl;

double secondMTTnumber;
cin >> secondMTTnumber;

double sumMTT=firstMTTnumber*secondMTTnumber;
cout << sumMTT;
cout <<("      To continue, restart the os")<< endl;
system ("pause");
return 0;
}

if (equationType=="calc.stt")
{
cout <<("      Enter the first number")<< endl;

double firstSTTnumber;
cin >> firstSTTnumber;

cout <<("      Go on")<< endl;

double secondSTTnumber;
cin >> secondSTTnumber;

double sumSTT=firstSTTnumber-secondSTTnumber;
cout << sumSTT;
cout <<("      To continue, restart the os")<< endl;
system ("pause");
return 0;
}

if(equationType=="calc.add")
{
cout <<("      Enter the first number")<< endl;

double firstADDnumber;
cin >> firstADDnumber;

double secondADDnumber;
cin >> secondADDnumber;

double sumADD=firstADDnumber+secondADDnumber;
cout << sumADD;
cout <<("     To continue, restart the os")<< endl;
system ("pause");
return 0;   
}
} 
}

if(menuChoose=="pea.stt")
{
cout <<("     Settings:")<< endl;
cout <<("     1<       ACCOUNTS")<< endl;
cout <<("     2<       SHUT DOWN")<< endl;

string settingOptions;
cin >> settingOptions;

if (settingOptions=="stt.acc")
{
cout <<("     Accounts:")<< endl;
cout <<("     1<       CREATE ACCOUNT")<< endl;
cout <<("     2<       DELETE ACCOUNT")<< endl;

string accountSettings;

cin >> accountSettings;
if(accountSettings=="acc.cac")
{
cout <<("Create your name")<< endl;
string accname;

cin >> accname;

cout <<("Type the password")<< endl;
string accpassword;

cin >> accpassword;

cout <<("Settings Saved, restart the os to continue")<< endl;
}

if(accountSettings=="acc.del")
{
cout <<("          Acount deleted")<< endl;
}
}
if(settingOptions=="stt.shutd")
{
cout <<("          Shutting down")<< endl;
return 0;
}
}
}
else
{
char errM[]="Unable to read written text";
cerr <<("ERROR_MESSAGE:")<< errM << endl;
cout <<("Please restart the device and write the right command")<< endl;
}
}

int main()
{
PCOS();
}
cout << "      File Name?" << endl;
string fileNaming;
cin >> fileNaming;
  1. 将用户名称与.txt组合
fileNaming +=".txt";
std::fstream fileio(fileNaming);
if (fileio.is_open())
{
//do stuff
}

4:用户写入文件:代替//do stuff

std::string userStr;
cin >> userStr;
fileio << userStr;

使用代码编辑问题后,看起来您想要根据用户输入调用函数。我建议使用函数映射:使用您的代码:

cout <<("     Applications:")<< endl;
cout <<("     1<           CALCULATOR")<< endl;
cout <<("     2<           FMAKE DOCUMENT APPLICATION")<< endl;
cout <<("     3<           HUCKY")<< endl;

string appChoose;        
cin >> appChoose; 

你创建一个映射,给出一个字符串,这个映射就会调用一个函数。

void Calculator () {
//do stuff
}
typedef void (*pfunc)();
map<string, pfunc> funcMap; 
//"app.calc" is whatever string the user will later type
funcMap["app.calc"] = Calculator; //assign functions to the map this way
//assigne other functions here

调用函数:

pfunc foo = funcMap[appChoose];
(*foo)(); //call function