简单的FSCANF问题,100分

shankeke 2009-01-24 02:54:16
源程序定义如下

#define MAX_PLANET 500
int coor[MAX_PLANET][3] = {{1,2,6},{5,2,6},{3,13,23},{7,8,1},{4,5,2}};


请帮忙改成用FSCANF从TXT文件中获取此数组coor[MAX_PLANET][3]。
详细就结贴,谢谢
...全文
244 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylj103037496 2009-01-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xiaoyisnail 的回复:]
C/C++ code

#include <stdio.h>
#include <stdlib.h>

#define MAX_PLANET 500

int main()
{
int coor[MAX_PLANET][3] = {0};
FILE *fp = fopen("in.txt", "r");
if(!fp)
{
printf("error\n");
exit(1);
}
for(int i=0;i<MAX_PLANET;i++)
{
if(fscanf(fp, "%d %d %d", &coor[i][0], &coor[i][1], &coor[i][2])==EOF)
break;
}

for(int j=0;j<i;j++)…
[/Quote]


学习了。
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

#define MAX_PLANET 500

int main()
{
int coor[MAX_PLANET][3] = {0};
FILE *fp = fopen("in.txt", "r");
if(!fp)
{
printf("error\n");
exit(1);
}
for(int i=0;i<MAX_PLANET;i++)
{
if(fscanf(fp, "%d %d %d", &coor[i][0], &coor[i][1], &coor[i][2])==EOF)
break;
}

for(int j=0;j<i;j++)
printf("%d %d %d\n", coor[j][0], coor[j][1], coor[j][2]);

fclose(fp);
return 0;
}


in.txt内容:

1 2 6
5 2 6
3 13 23
7 8 1
4 5 2


运行结果:

1 2 6
5 2 6
3 13 23
7 8 1
4 5 2
Press any key to continue
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shankeke 的回复:]
1 1 1
2 2 2
3 3 3
.
.
.
这样吗?
[/Quote]
可以拿编译器去试试
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 waizqfor 的回复:]
C/C++ code
#define MAX_PLANET 500
int coor[MAX_PLANET][3] = {{1,2,6},{5,2,6},{3,13,23},{7,8,1},{4,5,2}};
int i;
for(i=0;i<=MAX_PLANET;i++)
{
fscanf("%d %d %d %d",&coor[i][0],*coor[i][1],&coor[i][2],&coor[i][3])
}
[/Quote]
FILE *p
p=fopen("123.txt","w+");
shankeke 2009-01-24
  • 打赏
  • 举报
回复
1 1 1
2 2 2
3 3 3
.
.
.
这样吗?
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shankeke 的回复:]
请问楼上,按照你写的,TXT里面的数据格式应该是什么样的?
[/Quote]
一个数中间一个空格
也可以用fseek设置位置写入
shankeke 2009-01-24
  • 打赏
  • 举报
回复
Fopen的语句也写上啊,详细一点,谢谢
waizqfor 2009-01-24
  • 打赏
  • 举报
回复

#define MAX_PLANET 500
int coor[MAX_PLANET][3] = {{1,2,6},{5,2,6},{3,13,23},{7,8,1},{4,5,2}};
int i;
for(i=0;i<=MAX_PLANET;i++)
{
fscanf("%d %d %d %d",&coor[i][0],*coor[i][1],&coor[i][2],&coor[i][3])
}
shankeke 2009-01-24
  • 打赏
  • 举报
回复
请问楼上,按照你写的,TXT里面的数据格式应该是什么样的?
xiaopoy 2009-01-24
  • 打赏
  • 举报
回复
int tmp[MAX_PLANET][3];
do
{
fscanf ("%i%i%i", tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3]);
}
while (++i<MAX_PLANET)
hityct1 2009-01-24
  • 打赏
  • 举报
回复
msdn中的例子:

#include <stdio.h>

FILE *stream;

void main( void )
{
long l;
float fp;
char s[81];
char c;

stream = fopen( "fscanf.out", "w+" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fprintf( stream, "%s %ld %f%c", "a-string",
65000, 3.14159, 'x' );

/* Set pointer to beginning of file: */
fseek( stream, 0L, SEEK_SET );

/* Read data back from file: */
fscanf( stream, "%s", s );
fscanf( stream, "%ld", &l );

fscanf( stream, "%f", &fp );
fscanf( stream, "%c", &c );

/* Output data read: */
printf( "%s\n", s );
printf( "%ld\n", l );
printf( "%f\n", fp );
printf( "%c\n", c );

fclose( stream );
}
}


Output

a-string
65000
3.141590
x


69,371

社区成员

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

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