C++中Char字符指针数组如何输入和使用?
需要编写一个小程序:
将2个txt文件 合并到新的txt文件中。
下面为修改好的代码
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main(int argc,char**argv)
{
char file[3][80];
if(argc<4)
{
cout<<"命令行参数过少.\n"
<<"你需要输入三个参数(参数1:txt1文件名 参数2:txt2文件名 参数3:新txt文件名)\n?";
cin>>file[0]>>file[1]>>file[2];
}
else
{
strcpy(file[0],argv[1]);
strcpy(file[1],argv[2]);
strcpy(file[2],argv[3]);
}
ifstream read1(file[0],ios::in);
ifstream read2(file[1],ios::in);
ofstream write(file[2],ios::app);
write<<read1.rdbuf()<<"\n"<<read2.rdbuf();
read1.close();
read2.close();
write.close();
cout<<file[0]<<"、"<<file[1]<<"已合并到"<<file[2]<<endl;
return 0;
}
问题1:
char file[3][80];
若换成char指针数组
char* file[3];
该如何使用输入语句:
cin>>file[0]>>file[1]>>file[2];
和 赋值语句
strcpy(file[0],argv[1]);
strcpy(file[1],argv[2]);
strcpy(file[2],argv[3]);
直接使用会运行出错 求大神指点
#C/C++#