求教C++关于用ofstream打开一个文件的问题(为什么注释那里一直打不开?)
#include<iostream>#include<fstream>usingnamespacestd;intmain(){charname1[8];charname2...
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char name1[8];
char name2[8];
cout << "input src_name:" << endl;
cin.getline (name1,8,'.');
cout << "input obj_name:" << endl;
cin.getline( name2,8,'.');
ofstream of1(name1);
if (!of1)
{
cout << "creat file error\n";
system("pause");
return 1;
}
char con1[100];
cout << "inout conent of file1" << endl;
cin.getline(con1, 100, '.');
int i = 0;
for (i = 0; con1[i] != '.'; i++)
{
of1.write(reinterpret_cast<char*>(&con1[i]), sizeof(char));
}
of1.close();
ifstream in1;
in1.open(name1);
if (!in1)
{
cout << "creat file error\n";
system("pause");
return 1;
}
in1.close();
char con2[100];
for (i = 0; con1[i] != '.'; i++)
{
con2[i] = con1[i];
}
ofstream of2(name2);
if (!of2)//为什么这个打不开而上面of1那里却可以打开
{
cout << "creat file error\n";
cout << "k";
system("pause");
return 1;
}
in1.open(name1);
if (!in1)
{
cout << "creat file error\n";
cout << "m";
system("pause");
return 1;
}
in1.read(reinterpret_cast<char*>(&con1[i]), sizeof(char));
while (!in1.eof())
{
of2.write(reinterpret_cast<char *>(&con1[i]), sizeof(char));
in1.read(reinterpret_cast<char*>(&con1[i]), sizeof(char));
}
of2.close();
in1.close();
ifstream in2;
in2.open(name2);
if (!in2)
{
cout << "creat file error\n";
system("pause");
return 1;
}
while (!in2.eof())
{
cout << con2 [i]<< ' ';
in2.read(reinterpret_cast<char*>(&con2[i]), sizeof(char));
}
in2.close();
system("pause");
return 0;
} 展开
#include<fstream>
using namespace std;
int main()
{
char name1[8];
char name2[8];
cout << "input src_name:" << endl;
cin.getline (name1,8,'.');
cout << "input obj_name:" << endl;
cin.getline( name2,8,'.');
ofstream of1(name1);
if (!of1)
{
cout << "creat file error\n";
system("pause");
return 1;
}
char con1[100];
cout << "inout conent of file1" << endl;
cin.getline(con1, 100, '.');
int i = 0;
for (i = 0; con1[i] != '.'; i++)
{
of1.write(reinterpret_cast<char*>(&con1[i]), sizeof(char));
}
of1.close();
ifstream in1;
in1.open(name1);
if (!in1)
{
cout << "creat file error\n";
system("pause");
return 1;
}
in1.close();
char con2[100];
for (i = 0; con1[i] != '.'; i++)
{
con2[i] = con1[i];
}
ofstream of2(name2);
if (!of2)//为什么这个打不开而上面of1那里却可以打开
{
cout << "creat file error\n";
cout << "k";
system("pause");
return 1;
}
in1.open(name1);
if (!in1)
{
cout << "creat file error\n";
cout << "m";
system("pause");
return 1;
}
in1.read(reinterpret_cast<char*>(&con1[i]), sizeof(char));
while (!in1.eof())
{
of2.write(reinterpret_cast<char *>(&con1[i]), sizeof(char));
in1.read(reinterpret_cast<char*>(&con1[i]), sizeof(char));
}
of2.close();
in1.close();
ifstream in2;
in2.open(name2);
if (!in2)
{
cout << "creat file error\n";
system("pause");
return 1;
}
while (!in2.eof())
{
cout << con2 [i]<< ' ';
in2.read(reinterpret_cast<char*>(&con2[i]), sizeof(char));
}
in2.close();
system("pause");
return 0;
} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询