如何打开并写入一个名称取自C++中字符串的文件
How do I open and write to a file with the name taken from a string in C++?
在制作项目时,我试图完成这个简单的任务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;
- 将用户名称与.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
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到