文件搜索 [英] File seek

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

问题描述



我最近在一次采访中被问到这个问题。 你如何

移动到文件中的第6个字节? ...我的想法是在文件中找到

数据类型,设置一个基本指针并将其推进6.我

mean,ptr + 6。另一种提出相同问题的方法是如何将

移动到文件中的第3条记录(考虑到该文件由

记录组成)。

我告诉采访者我们可以寻求转移到

特定字节。当时,面试官点点头表示赞同我的回答,但我知道他认为有更好的方法可以做到这一点。有人可以提出一些指示

有效的方式是什么?


问候,

Arvind。

解决方案



要移动到第6个字节,您不需要在文件中找到数据类型。一个

字节是一个字节。也许他指的是那个。


Mauricio


" Arvind Varma Kalidindi" <斧***** @ lycos.com>在消息中写道

news:26 ************************** @ posting.google.c om ...


我最近在一次采访中被问到这个问题。 你如何移动到文件中的第6个字节? ...我的想法是在文件中找到
数据类型,设置一个基本指针并将其推进6.我的意思是,ptr + 6。另一种提出相同问题的方法是如何将
移动到文件中的第3条记录(考虑到文件由
记录组成)。
我告诉采访者我们可以寻求转移到特定字节。当时,面试官点点头表示他对我的回答表示赞同,但我才知道他认为有更好的方法可以做到这一点。任何人都可以提出一些有效的方法吗?

问候,
Arvind。



< blockquote>

2004年8月10日星期二,Arvind Varma Kalidindi在comp.lang.c中写道:


我最近在一次采访中被问到这个问题。 你如何移动到文件中的第6个字节? ...我的想法是在文件中找到
数据类型,设置一个基本指针并将其提前6.


这不是连贯的英语。文件没有数据类型,并且

你不能设置指向/进入/文件的C或C ++指针。你必须先用b $ b来读出数据。

我的意思是,ptr + 6。另一种提出相同问题的方法是如何将
移动到文件中的第3条记录(考虑到文件由
记录组成)。


并且假设文件中的前两个记录之间总共有两个
6个字节,是的。 :)

我告诉采访者我们可以寻求转移到
特定字节。当时,面试官点点头表示他对我的回答表示赞同,但我才知道他认为有更好的方法可以做到这一点。任何人都可以提出一些指示
有效的方式是什么?




我怀疑他认为有更多/更有效/方式。不过,你可以在你的答案中更多/更详细/ b $ b。他可能有一个预计C中的面试候选人能够写下来的话。


fseek(fp,6,SEEK_SET);
(虽然我个人每次都要查看

这些参数的顺序),也许可以解释为什么


if(fseek(fp,6,SEEK_SET)){

put(fseek期间的I / O错误);

}


会更合适。他本可以期待你回忆说'fseek''在文字上没有明确定义的行为

溪流,除非在非常具体的情况下 - 考虑

第6字节的定义关于文本文件


apple

banana

EOF


是第6字节 ASCII换行符,回车符,字母b,

或什么?一个完整的答案必须至少解释这个问题;并且

然后面试官可能会跟进,你认为什么是最好的

解释,以及你将如何实现它?

导致


for(i = 0; i< 6; ++ i)getc(fp);


面试官甚至可能想知道你是否意识到

''fseek(fp,6,SEEK_SET)''是/否/检索

a二进制中的第六个字节文件根本就是/ seven / byte,因为

''fseek''从零开始计算。 (我花了一段时间。)


当然,在C ++中还有很多其他答案。

不要交叉像这样的不同语言的团体;

它只会让人感到困惑,你得到的答案有一半将是

无法使用。


-Arthur


ax*****@lycos.com (Arvind Varma Kalidindi)写道:



#我最近在一次采访中被问到这个问题。 你如何

#移动到文件中的第6个字节? ...我的想法是在文件中找到

#数据类型,设置一个基本指针并将其提前6.我

#mean,ptr + 6 。另一种提出相同问题的方法是如何将

#移动到文件中的第3条记录(考虑到该文件由

#记录组成)。 br />

这里有些东西是乱码或丢失的。

编码记录中的记录有多种不同的方式,以及索引(或不索引)文件的

记录。没有关于文件和记录格式和索引的信息,

没有真正的答案。


-

SM Ryan http://www.rawbw.com/~wyrmwif/

这个镇上有很多野蛮人。


Hi,
I was asked this question in an interview recently. "How do you
move to the 6th byte in a file?" ... My thinking would be to find the
data types in the file, set a base pointer and advance it by 6. I
mean, ptr+6. Another way to ask the same question is how do you move
to the 3rd record in a file (considering that the file is made up of
records).
I told the interviewer that we could do a seek to move to the
specific byte. At that time, the interviewer nodded his head to show
his approval of my answer, but I came to know that he thought there is
a more effective way of doing this. Can anyone throw some pointers on
what that effective way could be?

Regards,
Arvind.

解决方案

Hi,
To move to the 6th byte you dont need to find the data types in the file. A
byte is a byte. Maybe he refered to that.

Mauricio

"Arvind Varma Kalidindi" <ax*****@lycos.com> wrote in message
news:26**************************@posting.google.c om...

Hi,
I was asked this question in an interview recently. "How do you
move to the 6th byte in a file?" ... My thinking would be to find the
data types in the file, set a base pointer and advance it by 6. I
mean, ptr+6. Another way to ask the same question is how do you move
to the 3rd record in a file (considering that the file is made up of
records).
I told the interviewer that we could do a seek to move to the
specific byte. At that time, the interviewer nodded his head to show
his approval of my answer, but I came to know that he thought there is
a more effective way of doing this. Can anyone throw some pointers on
what that effective way could be?

Regards,
Arvind.




On Tue, 10 Aug 2004, Arvind Varma Kalidindi wrote in comp.lang.c:


I was asked this question in an interview recently. "How do you
move to the 6th byte in a file?" ... My thinking would be to find the
data types in the file, set a base pointer and advance it by 6.
This is not coherent English. Files don''t have "data types," and
you can''t set C or C++ pointers to point /into/ files. You have to
read out the data first.
I mean, ptr+6. Another way to ask the same question is how do you move
to the 3rd record in a file (considering that the file is made up of
records).
And assuming that the first two records in the file have a total of
6 bytes between them, yes. :)
I told the interviewer that we could do a seek to move to the
specific byte. At that time, the interviewer nodded his head to show
his approval of my answer, but I came to know that he thought there is
a more effective way of doing this. Can anyone throw some pointers on
what that effective way could be?



I doubt he thought there was a more /effective/ way. You could
have been more /detailed/ in your answer, though; he might have
expected an interview candidate in C to be able to write down

fseek(fp, 6, SEEK_SET);

from memory (although I personally have to look up the order of
those parameters every time), and perhaps to explain why

if (fseek(fp, 6, SEEK_SET)) {
puts("I/O error during fseek");
}

would be more appropriate. He might have expected you to
recall that ''fseek'' does not have well-defined behavior on text
streams except in very specific circumstances---consider the
definition of "the 6th byte" with respect to the text file

apple
banana
EOF

Is "the 6th byte" an ASCII newline, a carriage return, the letter ''b'',
or what? A complete answer must at least explain the problem; and
the interviewer might then follow up with, "What is the best
interpretation in your opinion, and how would you implement it?"
leading to

for (i=0; i < 6; ++i) getc(fp);

The interviewer might even have wondered if you''d realize that
''fseek(fp, 6, SEEK_SET)'' does /not/ retrieve the sixth byte in
a binary file at all, but rather the /seventh/ byte, because
''fseek'' counts from zero. (It took me a while, too.)

And of course there are a whole slew of other answers in C++.
Don''t crosspost between groups for different languages like this;
it just confuses people, and half the answers you get will be
unusable.

-Arthur


ax*****@lycos.com (Arvind Varma Kalidindi) wrote:
# Hi,
# I was asked this question in an interview recently. "How do you
# move to the 6th byte in a file?" ... My thinking would be to find the
# data types in the file, set a base pointer and advance it by 6. I
# mean, ptr+6. Another way to ask the same question is how do you move
# to the 3rd record in a file (considering that the file is made up of
# records).

Something here is garbled or missing. There are many different ways of
encoding records in a file, and of indexing (or not indexing) a file''s
record. Without information about file and record formats and indexing,
there''s no real answer.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.


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

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