visual c++ 6.0 读取txt文件到二维数组,二维数组写入txt文件

2024-11-25 13:27:01
推荐回答(3个)
回答1:

//从123.txt读数据,将读到的数据输入456.txt中

#include
#include

int main(void)
{
int i, j;
double Q[5][4];

FILE *fp;

if (NULL == (fp = fopen("123.txt", "r")))
{
printf("file not open!\n");
exit(0);
}

for (i=0; i<5; i++)
{
for (j=0; j<4; j++)
{
fscanf(fp, "%lf", &Q[i][j]);
}
}

fclose(fp);

if (NULL == (fp = fopen("456.txt", "w")))
{
printf("file not open!\n");
exit(0);
}

for (i=0; i<5; i++)
{
for (j=0; j<4; j++)
{
fprintf(fp, "%lf\n", Q[i][j]);
}
}

fclose(fp);

return 0;
}

回答2:

#include
#include
using namespace std;

int main()
{
double Q[5][4];
ifstream fin("a.txt");//a.txt读入de文件
ofstream fout("b.txt");//b.txt输出de文件
char ch[20];
for(int i=0;i<5;i++)
for(int j=0;j<4;j++)
{
fin>>ch;
fout< Q[i][j]=atof(ch);
cout<
}
fin.close();
fout.close();
return 0;
}

回答3:

#include

int main() {
int i,j;
double Q[5][4];
FILE *InputFP,*OutFP;
InputFP = fopen("F:\\VC\\in.txt","rt");
for(i = 0;i < 5;i++) {
for(j = 0;j < 4;j++) {
fscanf(InputFP,"%lf",&Q[i][j]);
printf("%7.2lf\n",Q[i][j]);
}
}
printf("\n");
fclose(InputFP);
OutFP = fopen("F:\\VC\\out.txt","wt");
for(i = 0;i < 5;i++) {
for(j = 0;j < 4;j++) {
fprintf(OutFP,"%7.2lf",Q[i][j]);
}
}
fclose(OutFP);
return 0;
}