我正面临fread()的问题? [英] im facing problem with fread()??

查看:65
本文介绍了我正面临fread()的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i正在编写一个简单的prgm来读取.txt文件,然后将内容

存储到数组中...

程序如下:

--------------------------

#include< stdio .h>


int main()

{

FILE * fp1;

int buf [5];

int num,i;


fp1 = fopen(" triangle.txt"," r");

if(fp1 == NULL)

{

printf(文件无法打开);

退出(0 );

}


num = fread(buf,sizeof(int),5,fp1);

printf(" ;读取的元素数量=%d \ n",num);


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

printf( "%d \ nn",buf [i]);

返回0;

}


--- ---------------

i将no.of元素读为0,但文件可以打开...

元素作为垃圾号码...


任何人都可以告诉我,扫管笏是这样做的问题.. ???? />
是我错误使用fread还是行为异常???


TIA

问候,

Rajshekhar

解决方案

文件triangle.txt必须为空,这就是为什么它返回0号

元素和那里的垃圾价值

Rajshekhar写道:


我正在写一个简单的prgm来读取.txt文件然后存储内容进入阵列......
程序如下:
--------------------------
#include< stdio.h>


{> FILE * fp1;
int buf [5];
int num,i;

fp1 = fopen(" triangle.txt"," r");
if(fp1 == NULL)
{
printf (文件无法打开);
退出(0);
}

num = fread(buf,sizeof(int),5,fp1);
printf(" num of elements read =%d \ n",num);

r(i = 0; i< 5; i ++)
printf("%d \ nn",buf [i]);
返回0;
}
< br ----> ------------------
我将no.of元素读为0,但文件是可以打开的...
元素为垃圾号码......

任何人都可以告诉我,扫管笏是这样做的问题.. ????
是我错误地使用fread还是行为异常???? br />
TIA
问候,
Rajshekhar




没有它不空......! !

i使用,,,,, getc&读取它的内容使用putc打印它们


~Rajshekhar




Rajshekhar写道:

我正在编写一个简单的prgm来读取.txt文件然后将内容存储到数组中...
请发布triangle.txt的内容然后有人可以帮助你。

如果triangle.txt是二进制文件,那么在fopen中尝试rb而不是r。

程序如下:
- ------------------------
#include< stdio.h>

int main()
{
FILE * fp1;
int buf [5];
int num,i;

fp1 = fopen(" triangle.txt"," r");
if(fp1 == NULL)
{
printf(文件无法打开);
退出(0);
} < br => num = fread(buf,sizeof(int),5,fp1);
printf(" num of elements read =%d \ n",num);

for(i = 0; i< 5; i ++)
printf("%d \ nn",buf [i]);
返回0;
}

------------------
我正在读取no.of元素为0,但文件是可以打开的...
作为垃圾数字的元素......

任何人都可以告诉我这是做问题的问题.. ????
是我错误地使用fread还是表现得非常糟糕?

TIA
问候,
Rajshekhar




Hi ,
i am writing a simple prgm to read a .txt file then store the contents
into the array...
program as follows:
--------------------------
#include<stdio.h>

int main()
{
FILE *fp1;
int buf[5];
int num,i;

fp1=fopen("triangle.txt","r");
if(fp1 == NULL)
{
printf("file cant be opend");
exit(0);
}

num=fread(buf,sizeof(int),5,fp1);
printf("num of elements read =%d\n",num);

for(i=0;i<5;i++)
printf("%d\n",buf[i]);
return 0;
}

------------------
i am getting no.of elements read as 0,but file is openable...
the elements as junk numbers...

can anybody tell me wat is the problem in doing this ..????
is it that i m using fread wrongly or it behaves abnoramlly???

TIA
Regards,
Rajshekhar

解决方案

The file triangle.txt must be empty that''s why it is returning 0 number
of elements and junk value there on
Rajshekhar wrote:

Hi ,
i am writing a simple prgm to read a .txt file then store the contents
into the array...
program as follows:
--------------------------
#include<stdio.h>

int main()
{
FILE *fp1;
int buf[5];
int num,i;

fp1=fopen("triangle.txt","r");
if(fp1 == NULL)
{
printf("file cant be opend");
exit(0);
}

num=fread(buf,sizeof(int),5,fp1);
printf("num of elements read =%d\n",num);

for(i=0;i<5;i++)
printf("%d\n",buf[i]);
return 0;
}

------------------
i am getting no.of elements read as 0,but file is openable...
the elements as junk numbers...

can anybody tell me wat is the problem in doing this ..????
is it that i m using fread wrongly or it behaves abnoramlly???

TIA
Regards,
Rajshekhar




no its not empty ....!!
i read the contents of it using,,,,,getc & printed them using putc

~Rajshekhar



Rajshekhar wrote:

Hi ,
i am writing a simple prgm to read a .txt file then store the contents
into the array... Please post the contents of triangle.txt.then someone can help you.
If triangle.txt is a binary file then try rb instead of r in fopen.
program as follows:
--------------------------
#include<stdio.h>

int main()
{
FILE *fp1;
int buf[5];
int num,i;

fp1=fopen("triangle.txt","r");
if(fp1 == NULL)
{
printf("file cant be opend");
exit(0);
}

num=fread(buf,sizeof(int),5,fp1);
printf("num of elements read =%d\n",num);

for(i=0;i<5;i++)
printf("%d\n",buf[i]);
return 0;
}

------------------
i am getting no.of elements read as 0,but file is openable...
the elements as junk numbers...

can anybody tell me wat is the problem in doing this ..????
is it that i m using fread wrongly or it behaves abnoramlly???

TIA
Regards,
Rajshekhar




这篇关于我正面临fread()的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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