ANSI C - Filsize? [英] ANSI C - Filsize ?

查看:62
本文介绍了ANSI C - Filsize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我退出这个语言的新手让我裸露:


如何将文件文件的内容读入缓冲区?

我该怎么办?得到一个文件的大小?


我试过这个,但它似乎不起作用:


FILE * file1;

FILE * file2;

file1 = fopen(" inputfil.txt"," r +");

file2 = fopen(inputfil.txt,r +);


while((c = getc(file1))!= EOF)

{

filesize ++;

}


unsigned char buffer [filesize];


while((c = getc(file2))!= EOF)

{

buffer [i] = c;

for (i = 0; i< filesize-1; i ++)

{

buffer [i] = getc(file2);

}

}

Im quit new to this language so bare with me:

how do read the contens of a textfile in to a buffer?
how do i get the size of a file?

I''ve tried this, but it dosn''t seem to work:

FILE *file1;
FILE *file2;
file1 = fopen("inputfil.txt", "r+");
file2 = fopen("inputfil.txt", "r+");

while((c = getc(file1)) != EOF)
{
filesize++;
}

unsigned char buffer[filesize];

while((c = getc(file2)) != EOF)
{
buffer[i] = c;
for(i = 0; i< filesize-1; i++)
{
buffer[i] = getc(file2);
}
}

推荐答案

2004年1月31日星期六19:04:20 +0100,Joe< am *** ********@hotmail.com>

在comp.lang.c中写道:
On Sat, 31 Jan 2004 19:04:20 +0100, Joe <am***********@hotmail.com>
wrote in comp.lang.c:
我退出了我对这种语言如此陌生:

如何将文本文件的内容读入缓冲区?
如何获取文件的大小?


永远不要只说它似乎无法正常工作,因为那不是

为任何人提供足够的信息来帮助解决你的问题。

至少,你需要提供一些不起作用的解释

手段。如果您的编译器生成错误,您需要复制文本

并将其粘贴到您的帖子中。如果它编译但运行不正确,你需要提到你认为它应该做什么,以及它做了什么。

文件*文件1;
文件* file2;
file1 = fopen(" inputfil.txt"," r +");
file2 = fopen(" inputfil.txt"," r +");
while((c = getc(file1))!= EOF)


c的类型是什么?如果它是一个字符,那可能是个问题。

{
filesize ++;


fileize的类型是什么?

}

unsigned char buffer [filesize];


除非你有一个符合C99标准的编译器,并且没有那么多的b / b $ b,你不能用变量定义一个数组作为尺寸。如果

您需要访问大小未知的存储量,直到运行时间为
,您需要使用动态内存分配,即malloc()或

calloc()。
while((c = getc(file2))!= EOF)
{
buffer [i] = c;
for(i = 0; i< filesize-1; i ++)
{
buffer [i] = getc(file2);
}
}
Im quit new to this language so bare with me:

how do read the contens of a textfile in to a buffer?
how do i get the size of a file?

I''ve tried this, but it dosn''t seem to work:
Never just say "it doesn''t seem to work", because that does not
provide enough information for anyone to help you with your problem.
At a minimum, you need to provide an explanation of what not working
means. If your compiler generates an error, you need to copy the text
and paste it into your post. If it compiles but runs incorrectly, you
need to mention what you think it should do, and what it does instead.
FILE *file1;
FILE *file2;
file1 = fopen("inputfil.txt", "r+");
file2 = fopen("inputfil.txt", "r+");

while((c = getc(file1)) != EOF)
What is the type of c? If it''s a char, that could be a problem.
{
filesize++;
What is the type of filesize?
}

unsigned char buffer[filesize];
Unless you have a C99 conforming compiler, and there are not very many
of those, you can''t define an array using a variable as the size. If
you need to access an amount of storage with a size not known until
run time, you need to use dynamic memory allocation, i.e., malloc() or
calloc().

while((c = getc(file2)) != EOF)
{
buffer[i] = c;
for(i = 0; i< filesize-1; i++)
{
buffer[i] = getc(file2);
}
}




请参阅comp.lang.c的常见问题解答,在我的签名块中链接,特别是

第7节,其中详细介绍了内存分配。
http://www.eskimo.com/~scs/ C-faq / s7.html


您在发布代码时表现得非常好,您只需要一点点

更具体一些未来的问题。


-

杰克克莱恩

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



See the FAQ for comp.lang.c, link in my signature block, particularly
section 7 which covers memory allocation in great detail.
http://www.eskimo.com/~scs/C-faq/s7.html

You did quite well at posting your code, you just need to be a little
more specific about the problem in the future.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


On星期六,2004年1月31日19:04:20 +0100

Joe< am *********** @ hotmail.com>写道:
On Sat, 31 Jan 2004 19:04:20 +0100
Joe <am***********@hotmail.com> wrote:
我退出这个语言的新手与我一起裸露:

如何读取缓冲区中文本文件的内容?


int buffer [WAY_LARGE_NUM];

int i;

i = 0;

while( (buffer [i] = getchar()!= EOF)|| i< WAY_LARGE_NUM)

i ++;

我如何获得文件的大小?


严格地说,如果不使用平台特定的

技术,你就不可能。

我试过这个,但它似乎无法工作:


这可以使用C99编译器,只需一个小修复。

FILE * file1;
FILE * file2;
file1 = fopen(" inputfil.txt"," r +");
file2 = fopen(" inputfil.txt"," r +" );

while((c = getc(file1))!= EOF)
{
filesize ++;
}

unsigned char缓冲液[文件大小];


C99允许您使用文件大小来表示阵列的大小。 C89

没有。同样对于这个应用程序,我建议输入int

作为缓冲区。

while((c = getc(file2))!= EOF)
{
buffer [i] = c;


我的价值是什么?

for(i = 0; i< filesize-1; i ++)
{
buffer [i] = getc(file2);
}
}
Im quit new to this language so bare with me:

how do read the contens of a textfile in to a buffer?
int buffer[WAY_LARGE_NUM];
int i;
i = 0;
while ((buffer[i] = getchar() != EOF) || i < WAY_LARGE_NUM)
i++;
how do i get the size of a file?
Strictly speaking, you can''t, without using a platform specific
technique.
I''ve tried this, but it dosn''t seem to work:
This can work with a C99 compiler, with a small fix.
FILE *file1;
FILE *file2;
file1 = fopen("inputfil.txt", "r+");
file2 = fopen("inputfil.txt", "r+");

while((c = getc(file1)) != EOF)
{
filesize++;
}

unsigned char buffer[filesize];
C99 allows you to use filesize for the size of your array. C89
does not. Also for this application, I would recommend type int
for buffer.
while((c = getc(file2)) != EOF)
{
buffer[i] = c;
What''s the value of i?
for(i = 0; i< filesize-1; i++)
{
buffer[i] = getc(file2);
}
}




再次使用C99缓冲区声明,您可以更改

以上循环到此;


i = 0;

while((c = getc(file2))!= EOF ){

buffer [i] = c;

i ++; }


您也可以放弃中期。


C99或其他,你真的_need_知道文件大小

提前?除了将数据放入缓冲区(无疑也很方便),

需要解决的更大问题是什么?


-

donLouis



Again with the C99 declaration of buffer, you could change the
above while loop to this;

i = 0;
while ((c = getc(file2)) != EOF) {
buffer[i] = c;
i++; }

You could also drop the middle term, as well.

C99 or otherwise, do you really _need_ to know the file size in
advance? Besides getting the data into a buffer (no doubt handy),
what is the larger problem to be solved?

--
donLouis


>>我如何获得文件的大小?
>> how do i get the size of a file?

严格来说,如果不使用平台特定的技术,你就不可能。

Strictly speaking, you can''t, without using a platform specific
technique.




假设ftell()属于stdio属于ANSI C,

一个/可能/可以找出文件大小。不起作用

包含非普通文件。


长文件大小(FILE * fp){

long save = ftell( fp);

fseek(fp,0,SEEK_END);

长尺寸= ftell(fp);

fseek(fp,save, SEEK_END);

返回尺寸;

}


Jan Engelhardt

- -



Assuming that ftell() belongs to stdio belongs to ANSI C,
one /might/ be able to find out the file size. Does not work
with un-ordinary files.

long filesize(FILE *fp) {
long save = ftell(fp);
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, save, SEEK_END);
return size;
}


Jan Engelhardt
--


这篇关于ANSI C - Filsize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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