我想创建一个文件夹,然后将我的文件保存到该文件夹中。我编写了自动创建文件的代码,我想将文件保存到文件夹中。我尝试过很多代码,但都没有。下面是我尝试过的最后一段代码,但它仍然不起作用:
void rooms() {
Name rooms;
Name rooms2;
Name rooms3;
Name staff;
int b;
char choicedo;
string filename;
string input;
do
{
ofstream bilik2;
bilik2.open("rooms2.txt", ios::out | ios::app);
if(!bilik2)
{
cout<<"File Not Found!";
exit(1);
}
if(bilik2 == NULL)
{
cout << "Error! Rooms is empty!";
break;
}
ofstream bilik3;
bilik3.open("rooms3.txt", ios::out | ios::app);
if(!bilik3)
{
cout<<"File Not Found!";
exit(1);
}
if(bilik3 == NULL)
{
cout << "Error! Rooms is empty!";
break;
}
cout<<"1. Single Rooms "<<endl;
cout<<"2. Family Rooms "<<endl;
cout<<"3. Royal Rooms "<<endl;
cout<<endl;
cout<<"\nRooms type: ";
cin >> b;
system("cls");
cout<<"Please fill in requirement information "<<endl;
switch(b)
{
case 1:
{
cout<<"Room No: ";
cin>>filename;
input = filename;
cin.ignore();
//ofstream input("C:\\Users\\Dr. Akilah\\Desktop\\BLUE\\single room");
mkdir("singleroom");
ofstream outfile("singleroom");
//system("mkdir singleroom");
outfile.write(input.c_str(), input.size());
input += ".txt";
outfile.close();
ifstream ifile(input.c_str());
if(ifile)
{
cout<<"Room is already exist."<<endl;
exit(1);
}
ofstream out(input.c_str());
//out.open("C:/Users/Dr. Akilah/Desktop/BLUE/single room/ ", ios::out | ios::app);
ofstream bilik;
bilik.open(input.c_str(), ios::out | ios::app);
cout<<endl;
cout<<"Air Conditioner: ";
cin.getline(rooms.airc, 20);
cout<<"Date: ";
cin.getline(rooms.date, 20);
cout<<"Insert by: ";
cin.getline(staff.first, 20);
bilik<<"List of Single Rooms"<<endl;
cout<<endl;
bilik<<"------------------------------------------------------------"<<endl;
bilik<<endl;
bilik<<" Room No: "<<filename<<endl;
bilik<<endl;
bilik<<"Air Conditioner: "<<rooms.airc<<endl;
bilik<<endl;
bilik<<"Regsitration Date: "<<rooms.date<<endl;
bilik<<endl;
bilik<<"Created by: "<<staff.first<<endl;
bilik<<endl;
bilik<<endl;
bilik<<"------------------------------------------------------------"<<endl;
bilik.close();
break;
}
case 2:
{
cout<<endl;
cout<<"Room no: ";
cin>>rooms2.room;
cin.ignore();
cout<<"Air Conditioner: ";
cin.getline(rooms2.airc, 20);
cout<<"Date: ";
cin.getline(rooms2.date, 20);
cout<<"Insert by: ";
cin.getline(staff.first, 20);
break;
}
case 3:
{
cout<<endl;
cout<<"Room no: ";
cin>>rooms3.room;
cin.ignore();
cout<<"Air Conditioner: ";
cin.getline(rooms3.airc, 20);
cout<<"Date: ";
cin.getline(rooms3.date, 20);
cout<<"Insert by: ";
cin.getline(staff.first, 20);
break;
}
default:
cout<<"Invalid choice "<<endl;
cout<<endl;
Sleep(1000);
//return 0;
}
cout<<"Enter Y To input an other data or Enter N to Exit: ";
cin>>choicedo;
cout<<"_____________________________________"<<endl;
cout<<endl;
}
while (choicedo=='y' || choicedo=='Y');
}
编译此代码时:
出现此错误:
那么,我该如何解决这个错误呢?如何将文件保存到文件夹中?
答案 0 :(得分:0)
std::ostream::put
带有单个字符,而不是C字符串;为此,您应该使用std::ostream::write
:
outfile.write(input.c_str(), input.size());