Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

C - How to read certain lines from a file? ( Not all lines or entire file but just certain lines)

I tried looking for similar questions that could find an answer to my question but i couldn't seem to find it. Could someone please tell me is there any way to read certain lines from the .txt file. For example i would like to read lines 2,3,5 from the text file.

Example of text file:

1255466889436     //Line 1 - I want to read this line
5489784642165     //Line 2
4984651425165     //Line 3 - I want to read this line
4968164816514     //Line 4
4161654168468     //Line 5 - I want to read this line

It would be nice if somone could show example code cause im really stumped at the moment.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read all lines, discard the ones you don't need.

You can seek to any offset in a file, but which line is offset 5000? That depends on how many newline characters there were in the previous 5000 characters. And unless you check all characters, you don't know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...