关于ftell和fopen的问题 [英] questions on ftell and fopen

查看:150
本文介绍了关于ftell和fopen的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下计划:


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * argv [])

{

if(argc!= 2)

{

printf(" Usage:< program-name< text-file> \ n");

退出(EXIT_FAILURE);

}


FILE * fp;


if((fp = fopen(argv [1]," r"))= = NULL)

{

printf(无法打开输入文件:%s:\ n",argv [1]);

退出(EXIT_FAILURE);

}


if(fseek(fp,0L,SEEK_END)== 0)

printf(" fseek successful\\\
);

else

printf(" fseek failed\\\
);


long int size = ftell(fp);


printf(" filesize =%ld \ n",size);


fclose(fp);


char a [size + 1];

a [size] =''\ 0'';


if((fp = fope n(argv [1]," rb"))== NULL)

{

printf(&无法打开输入文件:%s:\ n ;,argv [1]);

退出(EXIT_FAILURE);

}


fread(a,size,1, fp);


if(ferror(fp))

{

printf(fread failed\\\
) ;

fclose(fp);

退出(EXIT_FAILURE);

}


fclose( fp);


printf("%s",a);


返回0;

}


假设文件名是tmp.c


我用Redhat Enterprise Linux下的gcc编译了这个程序,带

命令


gcc -std = c99 tmp.c


汇编得很好。我用命令运行它


$。/ a.out tmp.c


它成功运行。

我的问题:

1)ftell的用法是否正确?

2)在第二次fopen中,我使用了rb。对于模式,即我打开

二进制模式下的文本文件,然后使用fread。这总是工作吗?

解决方案

./ a.out tmp.c


它运行成功。


我的问题:

1)ftell的用法是否正确?

2)在第二个fopen中,我使用了rb。对于模式,即我打开

二进制模式下的文本文件,然后使用fread。这总是

工作吗?


很抱歉给您带来不便。


第一个问题应该是

1)我可以使用fseek作为本程序中使用的文本流(虽然它好像b $ b似乎有效)


" su ************** @ yahoo.com,India" < su ************** @ yahoo.com>

写道:


1)我可以使用fseek作为此程序中使用的文本流(尽管它可以使用



你对fseek的使用就像这个:

if(fseek(fp,0L,SEEK_END)== 0)

这适用于文本流(当你使用它时)。 />
-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q = " kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}


Consider the following program:

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

int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: <program-name<text-file>\n");
exit(EXIT_FAILURE);
}

FILE *fp;

if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Could not open input file :%s:\n", argv[1]);
exit(EXIT_FAILURE);
}

if (fseek(fp, 0L, SEEK_END) == 0)
printf("fseek successful\n");
else
printf("fseek failed\n");

long int size = ftell(fp);

printf("filesize = %ld\n", size);

fclose(fp);

char a[size+1];
a[size] = ''\0'';

if ((fp = fopen(argv[1], "rb")) == NULL)
{
printf("Could not open input file :%s:\n", argv[1]);
exit(EXIT_FAILURE);
}

fread(a, size, 1, fp);

if (ferror(fp))
{
printf("fread failed\n");
fclose(fp);
exit(EXIT_FAILURE);
}

fclose(fp);

printf("%s", a);

return 0;
}

Suppose the name of the file is tmp.c

I compiled this program with gcc under Redhat Enterprise Linux with
the command

gcc -std=c99 tmp.c

It compiles well. I ran it with the command

$./a.out tmp.c

It runs successfully.

My questions:
1) Is the usage of ftell above correct ?
2) In the second fopen, I have used "rb" for the mode ie I am opening
the text file in binary mode and then using fread. Will this always
work ?

解决方案

./a.out tmp.c

It runs successfully.

My questions:
1) Is the usage of ftell above correct ?
2) In the second fopen, I have used "rb" for the mode ie I am opening
the text file in binary mode and then using fread. Will this always
work ?


Sorry for the inconvenience.

The first question should be
1)Can I use fseek for a text stream as used in this program(though it
seems to work)


"su**************@yahoo.com, India" <su**************@yahoo.com>
writes:

1)Can I use fseek for a text stream as used in this program(though it
seems to work)

Your use of fseek is like this:
if (fseek(fp, 0L, SEEK_END) == 0)
This is fine for use with a text stream (as you use it).
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


这篇关于关于ftell和fopen的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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