怎么把C语言的二维数组里面的数据保存到TXT文件中去?

zuiaizuqiu 2012-08-25 07:29:22
看书上的文件读取有点迷糊了,找不到该怎么办,求解~

PS:输出到文件里面的格式也是像矩阵那样子的。
...全文
2567 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
饽饽2023 2013-10-21
  • 打赏
  • 举报
回复
谢谢一楼的讲解。。
nightkids_008 2012-08-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 1 楼 的回复:

#include<stdio.h>

int main(void)
{
FILE* fp;
int str[4][3] = {{1,2,3},{4,5,6},{7,8,9},{2,3,5}};
int i, j;

/*打开文本文件以写入内容*/
fp = fopen("test.txt", "w");
if (!fp)……

又试了一次,没问……
[/Quote]
结贴,散分噢
zuiaizuqiu 2012-08-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

#include<stdio.h>

int main(void)
{
FILE* fp;
int str[4][3] = {{1,2,3},{4,5,6},{7,8,9},{2,3,5}};
int i, j;

/*打开文本文件以写入内容*/
fp = fopen("test.txt", "w");
if (!fp)……
[/Quote]
又试了一次,没问题了,谢了
zuiaizuqiu 2012-08-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

#include<stdio.h>

int main(void)
{
FILE* fp;
int str[4][3] = {{1,2,3},{4,5,6},{7,8,9},{2,3,5}};
int i, j;

/*打开文本文件以写入内容*/
fp = fopen("test.txt", "w");
if (!fp)……
[/Quote]我刚才试了一下,怎么test.txt里面是空白呢?
Gloveing 2012-08-25
  • 打赏
  • 举报
回复
自己控制格式,写入 "\r\n"就换行
hasker 2012-08-25
  • 打赏
  • 举报
回复
#include<stdio.h>

int main(void)
{
FILE* fp;
int str[4][3] = {{1,2,3},{4,5,6},{7,8,9},{2,3,5}};
int i, j;

/*打开文本文件以写入内容*/
fp = fopen("test.txt", "w");
if (!fp)
{
perror("cannot open file");
//exit(-1);
}

/*把二维数组的内容写入文件*/
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
fprintf(fp, "%d ", str[i][j]);
}
fputc('\n', fp);
}
fclose(fp);

return 0;
}

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧