从文件问题中读取 [英] read from file question

查看:52
本文介绍了从文件问题中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个关于我想要做的文件操作的问题。我有一个数据

文件让我说X字节。我想读取文件并每隔2个字节删除一个字节

。我有点喜欢,我的方法是这样的。


main()

{

char buf [1024];

FILE * fp;

fp = fopen(" data"," rb");


while(! feof(fp)){

fread(buf,1,1,fp);

}


fclose(fp) ;

}


ok,有了这个我一次可以读取1个字节直到结束。现在我想要的是什么

do is ....我想在buf上有1个字节,第3个字节..第5个..等等

并丢弃第2个......第4个......等等。我以为是采取这个立场

文件指针,

例如:position = ftell(fp)如果位置%2 == 0那么请不要读取
。 。


但是我不知道我是在正确的方式还是正确的方式来做我想要的b $ b。我会欣赏有关如何接近的评论它。谢谢

提前。

Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .

推荐答案

Arquitecto写道:
Arquitecto wrote:




我有一个关于我想要做的文件操作的问题。我有一个数据

文件让我说X字节。我想读取文件并每隔2个字节删除一个字节

。我有点喜欢,我的方法是这样的。


main()

{

char buf [1024];

FILE * fp;

fp = fopen(" data"," rb");


while(! feof(fp)){

fread(buf,1,1,fp);

}


fclose(fp) ;

}
Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}



这不符合您的想法;你反复阅读
a单字节到buf []的第一个元素。


1.更喜欢检查fread的返回值

测试feof。

到达文件结尾之前,fread可能会遇到错误。


2.要使用fread一次读取一个字节,你需要

a指向缓冲区中当前位置的指针。


This doesn''t do what you think; you repeatedly read
a single byte into the first element of buf[].

1. Prefer to check the return value of fread instead
of testing feof. fread may encounter errors before
getting to end of file.

2. To use fread to read one byte at a time you''ll need
a pointer to the current position in the buffer.


ok,有了这个我可以一次读取1个字节直到结束。现在我想要什么

do是....我想在buf上有1个字节,第3个字节..第5个..等等

并丢弃第2个......第4个......等。我的想法是取位置

文件指针,

例如:position = ftell(fp)如果位置%2 == 0则不要< br $> b $ b读..


但是我不知道我是在正确的方式还是正确的方式做我想要的b $ b想要的。我将很感激如何处理它的评论。谢谢

advance。
ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .



所以你可以尝试一次阅读整个文件,然后复制

每个其他字符到你的结果缓冲区,或者一次读取一个文件
字节并且只将奇数索引字节放入你的
缓冲区(记住你需要保留跟踪当前

缓冲位置)。有几种方法你可以做到这一点,但

你可以不用使用ftell就可以做到。


-

imalone

So you can try to read the whole file at once and then copy
every other char to your result buffer, or read the file one
byte at a time and put only the odd indexed bytes into your
buffer (remembering that you need to keep track of the current
buffer position). There are a few ways you can do that, but
you can do it without using ftell.

--
imalone


" Arquitecto"写道:
"Arquitecto" wrote:

我有一个关于我想要做的文件操作的问题。我有一个数据

文件让我说X字节。我想读取文件并每隔2个字节删除一个字节

。我有点喜欢,我的方法是这样的。


main()

{

char buf [1024];

FILE * fp;

fp = fopen(" data"," rb");


while(! feof(fp)){

fread(buf,1,1,fp);

}


fclose(fp) ;

}


ok,有了这个我一次可以读取1个字节直到结束。现在我想要的是什么

do is ....我想在buf上有1个字节,第3个字节..第5个..等等

并丢弃第2个......第4个......等等。我以为是采取这个立场

文件指针,

例如:position = ftell(fp)如果位置%2 == 0那么请不要读取
。 。


但是我不知道我是在正确的方式还是正确的方式来做我想要的b $ b。我会欣赏有关如何接近的评论它。谢谢

提前。
I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .



我会这样做。创建一个输出缓冲区,并使用索引操作将所需数据

放入其中。然后将输出缓冲区写入新的

文件。完成后,删除原始文件并重命名新文件

以获取旧文件的名称。

I would do it like this. Create an output buffer and put the desired data
in it using the indexing operations. Then write the output buffer to a new
file. When you are done, delete the original file and rename the new file
to have the name of the old file.


" osmium" < r1 ******** @ comcast.netwrites:
"osmium" <r1********@comcast.netwrites:

" Arquitecto"写道:
"Arquitecto" wrote:

>我有一个关于我想要做的文件操作的问题。我有一个数据
文件让我说X字节。我想读取文件并删除每个第2个字节的字节。
>I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte .



[...]

[...]


我会这样做。创建一个输出缓冲区,并使用索引操作将

所需数据放入其中。然后将

输出缓冲区写入新文件。完成后,删除原始的

文件,并将新文件重命名为旧文件的名称。
I would do it like this. Create an output buffer and put the
desired data in it using the indexing operations. Then write the
output buffer to a new file. When you are done, delete the original
file and rename the new file to have the name of the old file.



这意味着将输入文件的一半内容存储在内存中,而不知道输入文件有多大。
。为这个

分配内存可能很棘手; realloc应该做*如果*它是可能的。


但它没有必要。当您正在读取输入文件时,将每个

其他字符写入输出文件。完成后,关闭输入

并输出文件并重命名输出文件。 (并进行错误检查。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://用户.sdsc.edu / ~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

That implies storing half the contents of the input file in memory,
without knowing how big the input file is. Allocating memory for this
can be tricky; realloc should do it *if* it''s possible at all.

But it''s not necessary. As you''re reading the input file, write every
other character to the output file. When you''re done, close the input
and output files and rename the output file. (And do error checking.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


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

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