编写一个程序,将 “粤, 港, 澳, 大, 湾, 区” 这6个字存入一个2行3列的二维数组,再按下图的格式输出,最后将该数组的内容存入txt文件。
下图的格式,汉字之间多少空格啊
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str[2][3] = {"粤","港","澳","大","湾","区"};
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
cout<<str[i][j]<<" ";
cout<<endl;
}
fstream out;
out.open("D:\\yga.txt", ios::out); // write,清空再写入
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
out.write(str[i][j].c_str(),str[i][j].length());
}
cout<<"数组存盘成功!"<<endl;
out.close();
return 0;
}
#include <stdio.h>
int main()
{
char buf[][4] = {"粤","港","澳","大","湾","区"};
//2行3列输出
for(int i=0; i<6; i++) {
printf("%s", buf[i]);
if((i + 1) % 3 == 0) //一行3个元素
printf("\n");
else
printf("\t");
}
//存入txt文件
FILE *fp = fopen("D:\\1.txt", "w");
if(fp == NULL) {
perror("open failed");
return -1;
}
for(int i=0; i<6; i++) {
fprintf(fp, "%s", buf[i]);
if((i + 1) % 3 == 0) //一行3个元素
fprintf(fp, "\n");
else
fprintf(fp, "\t");
}
fclose(fp);
printf("数组存盘成功\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main() {
setlocale(LC_ALL,"chs");
wchar_t a[2][3]={{L'粤',L'港',L'澳'},{{L'大',L'湾',L'区'}};
for (int y=0;y<2;y++) {
for (int x=0;x<3;x++) {
wprintf(L"%c ",a[y][x]);
}
wprintf(L"\n");
}
FILE *f=fopen("a.txt","wb");
fwrite((const void *)&a[0][0],sizeof(wchar_t),6,f);
fclose(f);
wprintf(L"数组存盘成功!\n");
system("pause");
return 0;
}