我的文件处理程序有问题。看到这个 [英] I've problem with my file handling program. See this

查看:48
本文介绍了我的文件处理程序有问题。看到这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这个程序时。

#include< stdio.h>


int main()

{

FILE * fp;


int i;

浮动f;

char str [20] ;


if((fp = fopen(" stuff"," w"))== NULL)

{

printf(抱歉!无法创建/阅读内容);

退出(-1);

}


for(i = 0; i< = 10; i ++)

fprintf(fp,"%d:%s:%。2f \ n",i," Hello", 12.3f);


for(i = 0; i< = 5; i ++)

{

fscanf(fp, %d:%s:%。2f\ n,& i,& str,& f);

printf(" Hi%s%d%f \ n",str,i,f);

}


fclose(fp);

返回0;

}


这是输出:

$。/ a.out

嗨|à?ù ?-F | Ya 0 0.000000

Hi |à?ù?-F | Ya 1 0.000000

嗨|à?ù?-F | Ya 2 0.000000

Hi |à?ù?-F | Ya 3 0.00000 0

Hi |à?ù?-F | Ya 4 0.000000

嗨|à?ù?-F | Ya 5 0.000000


文件的内容是:

$ cat stuff

0:你好:12.30

1:你好:12.30

2:你好:12.30

3:你好:12.30

4:你好:12.30

5:你好:12.30

6:你好:12.30

7:你好:12.30

8:你好:12.30

9:您好: 12.30

10:你好:12.30

当我读取字符串和浮动值时,为什么会发生这种情况。帮帮我

我哪里错了

提前致谢

解决方案

< blockquote> ./ a.out

Hi |à?ù?-F | Ya 0 0.000000

嗨|à?ù?-F | Ya 1 0.000000

Hi |à?ù?-F | Ya 2 0.000000

嗨|à?ù?-F | Ya 3 0.000000

嗨|à?ù ?-F | Ya 4 0.000000

Hi |à?ù?-F | Ya 5 0.000000


文件内容如下:


猫的东西

0:你好:12.30

1:你好:12.30

2:你好:12.30

3:你好:12.30

4:你好:12.30

5:你好:12.30

6 :你好:12.30

7:你好:12.30

8:你好:12.30

9:你好:12.30

10:你好:12.30

当我读取字符串和浮点值时,为什么会发生这种情况。帮帮我

我哪里错了

提前致谢




Rajen写道:


当我运行这个程序时。

#include< stdio.h>


int main()

{

FILE * fp;


int i;

float f;

char str [20];


if((fp = fopen(" stuff"," w") )== NULL)



应该是''fopen(" stuff,rw)''或

''fopen(" stuff"," r +")''


{

printf("对不起) !无法创建/阅读东西);

退出(-1);

}


for(i = 0 ; i< = 10; i ++)

fprintf(fp,"%d:%s:%。2f \ n",i," Hello",12.3f);



这里文件指针必须带到

文件的开头,或者你想要读取

内容的位置文件。像''rewind(fp)''这样的东西会这样做。


for(i = 0; i< = 5; i ++)

{

fscanf(fp,"%d:%s:%。2f \ n",& i,& str,& f);

printf(" Hi%s%d%f \ n",str,i,f);

}


fclose(fp);

返回0;

}


这是输出:


When I run this program.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}

fclose(fp);
return 0;
}

This is the output:
$./a.out
Hi |à?ù?-F|Ya 0 0.000000
Hi |à?ù?-F|Ya 1 0.000000
Hi |à?ù?-F|Ya 2 0.000000
Hi |à?ù?-F|Ya 3 0.000000
Hi |à?ù?-F|Ya 4 0.000000
Hi |à?ù?-F|Ya 5 0.000000

The contents of the file are:
$cat stuff
0:Hello:12.30
1:Hello:12.30
2:Hello:12.30
3:Hello:12.30
4:Hello:12.30
5:Hello:12.30
6:Hello:12.30
7:Hello:12.30
8:Hello:12.30
9:Hello:12.30
10:Hello:12.30

Why is this happening when I''m reading string and float values. Help me
where I''m wrong
Thanks in advance

解决方案

./a.out
Hi |à?ù?-F|Ya 0 0.000000
Hi |à?ù?-F|Ya 1 0.000000
Hi |à?ù?-F|Ya 2 0.000000
Hi |à?ù?-F|Ya 3 0.000000
Hi |à?ù?-F|Ya 4 0.000000
Hi |à?ù?-F|Ya 5 0.000000

The contents of the file are:


cat stuff
0:Hello:12.30
1:Hello:12.30
2:Hello:12.30
3:Hello:12.30
4:Hello:12.30
5:Hello:12.30
6:Hello:12.30
7:Hello:12.30
8:Hello:12.30
9:Hello:12.30
10:Hello:12.30

Why is this happening when I''m reading string and float values. Help me
where I''m wrong
Thanks in advance



Rajen wrote:

When I run this program.
#include<stdio.h>

int main()
{
FILE *fp;

int i;
float f;
char str[20];

if( ( fp=fopen("stuff", "w")) == NULL )

Should have been either ''fopen("stuff",,"rw")'' or
''fopen("stuff",,"r+")''

{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}

for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);

And here the file pointer has to be brought to the beginning of the
file, or to the position from which you wanted to read the contents of
the file. Something like, ''rewind(fp)'' would do that.

for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}

fclose(fp);
return 0;
}

This is the output:


这篇关于我的文件处理程序有问题。看到这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆