我如何从文件中读取奇数行 [英] how do i read odd numbered lines from a file

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

问题描述

你好,

有人能告诉我如何从奇数编号的文件中划线,即

第1,第3,第5 ......行。


i尝试将文件指针递增2(fp = fp + 2)

但它确实无法工作

有人可以给我代码请。

解决方案

ru * *****@gmail.com 写道:

你好,
任何人都可以告诉我如何从奇数编号的文件中划线,即
1st ,3,5 ......行。

我尝试将文件指针递增2(fp = fp + 2)
但它确实无法工作
有人可以给我吗代码请。




你可以读两行到同一个字符串数组,因此

丢弃你不应该的字符串我想要。并且filepointer每次增加

正确的字节数。


因此fp = fp + 2的问题是线长度可能会有所不同而你不要
事先知道每个线路...

最好的问候

Martin J?rgensen


-

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

马丁之家J?rgensen - http://www.martinjoergensen.dk


Martin J?rgensen说:

ru *** ***@gmail.com 写道:

你好,
任何人都可以告诉我如何从奇数编号的文件中划线,即第1,第3 ,5 ......行。

我尝试将文件指针递增2(fp = fp + 2)
但它确实无法工作
有人可以给我代码拜托。
你可以读两行到同一个char数组,从而丢弃你不想要的那个。并且filepointer每次递增
正确的字节数。




不,它不是!


文件指针不递增,因为文件指针没有指向文件的
!我知道它听起来确实如此,但它真的,真的不是。

它指的是一些内部doodad包含有关

文件的信息。内部doodad存储的信息之一是

文件位置指示符。 - 和/ /递增。

所以fp = fp + 2的问题是线路长度可能会有所不同而且你事先并不知道每个线路长度...




不,fp = fp + 2的问题在于它将有效指针转换成

无效的指针。除非你是绝对的,否则请不要给出建议

100%确定它是正确的 - 即使这样,请检查K& R或标准

到确保你是对的。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


ru ****** @ gmail.com 写道:

任何人都可以告诉我如何从一个奇数编号的文件行即
第1,第3,第5 ......行。

我尝试将文件指针递增2(fp = fp + 2)
但它确实无法正常工作有人可以给我代码。




如果通过递增文件指针你的意思是你这样做了:


#include< stdio.h>


...

FILE * fp = fopen(...);

...

fp = fp + 2;

...


那不符合你的意愿;令人惊讶的是,它增加了文件

指针,它不会从文件中读取,或以任何方式修改文件流中当前的

位置。

增量后fp将有一个值2 * sizeof(FILE)字节大于
比以前更大,指向一个很可能不是文件的内存区域

结构并且最终不是您打开的FILE结构。

尝试通过fp读取将导致未定义的行为。 (因为它

会修改随机存储位置)


提示:如果你面前有一份文件(打印单面)和

你想只读取奇数页面。你会怎么做?


hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does''nt work
Can someone give me the code please.

解决方案

ru******@gmail.com wrote:

hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does''nt work
Can someone give me the code please.



You could just read in two lines to the same char-array, thereby
discarding the one you don''t want. And the filepointer is incremented
the correct number of bytes each time.

So the problem with fp=fp+2 is that line lengths can vary and you don''t
know each linelength on beforehand...
Best regards
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk


Martin J?rgensen said:

ru******@gmail.com wrote:

hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does''nt work
Can someone give me the code please.
You could just read in two lines to the same char-array, thereby
discarding the one you don''t want. And the filepointer is incremented
the correct number of bytes each time.



No, it isn''t!

The file pointer is NOT incremented, because the file pointer does not point
to the file! I know it sounds like it does, but it really, really doesn''t.
What it points to is some internal doodad that contains information about
the file. One of the items of information that internal doodad stores is a
"file position indicator" - and /that/ is incremented.
So the problem with fp=fp+2 is that line lengths can vary and you don''t
know each linelength on beforehand...



No, the problem with fp=fp+2 is that it turns a valid pointer into an
invalid one for no gain. Please don''t give advice unless you are absolutely
100% sure it''s correct - and even then, please check in K&R or the Standard
to ensure that you are right to be sure.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


ru******@gmail.com wrote:

can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does''nt work
Can someone give me the code please.



If by "incrementing the file pointer" you mean you did this:

#include <stdio.h>

...
FILE *fp = fopen(...);
...
fp = fp + 2;
...

That does not do what you want; Surprisingly, it increments the file
pointer, it does not read from the file, or modify the current
position in the file stream in any way.
After the increment fp will have a value 2*sizeof(FILE) bytes larger
than before, pointing to a memory area that most likely is not a FILE
structure and definitively is not the FILE structure you opened.
Attempting to read via fp will cause undefined behavior. (Because it
will modify random memory locations)

Hint: If you had in front of you a document (printed single side) and
you wanted to read only the odd numbered pages. How would you do it?


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

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