如何在 C 中从文件中 fgets() 特定行?
所以,我试图找到一种方法来 fgets() C 文本文件中的特定行,将该行的内容复制到更永久的缓冲区中:
本质上,我想知道是否有办法做到这一点没有类似于以下代码的内容:
FILE *fp;
fp = fopen(filename, "r");
char line[256];
char * buffer;
int targetline = 10;
while( targetline > 0)
{
fgets(line, 256, fp)
}
buffer =(char*)malloc(sizeof(char) * strlen(line));
strcpy(buffer, line);
所以基本上我不想迭代文件 n-1 次只是为了到达第 n 行......它看起来效率不高(而且,这是家庭作业,我需要得到100%哈哈)。
任何帮助将不胜感激。
So, I'm trying to find a way to fgets() a specific line in a text file in C, to copy the contents of the line into a more permanent buffer:
Essentially, I was wondering if there was a way to do that without something similar to the following code:
FILE *fp;
fp = fopen(filename, "r");
char line[256];
char * buffer;
int targetline = 10;
while( targetline > 0)
{
fgets(line, 256, fp)
}
buffer =(char*)malloc(sizeof(char) * strlen(line));
strcpy(buffer, line);
So basically I don't want to iterate through the file n-1 times just to get to the nth line... it just doesn't seem very efficient (and, this being homework, I need to get a 100% haha).
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非您对文件有更多了解,否则您无法随机访问特定行。新行由行结束字符分隔,通常可以出现在任何地方。文本文件不附带允许您跳到第 nth 行的地图或索引。
例如,如果您知道文件中的每一行的长度相同,那么您可以使用随机访问来跳转到特定行。如果没有此类额外知识,您别无选择,只能迭代整个文件,直到到达所需的行。
Unless you know something more about the file, you can't access specific lines at random. New lines are delimited by the presence of line end characters and they can, in general, occur anywhere. Text files do not come with a map or index that would allow you to skip to the nth line.
If you knew that, say, every line in the file was the same length, then you could use random access to jump to a particular line. Without extra knowledge of this sort you simply have no choice but to iterate through the entire file until you reach your desired line.
如果您知道每行的长度,则可以使用 fseek 跳到您想要的行。
否则,您需要遍历所有行。
If you know the length of each line, you can use fseek to skip to the line you want.
Otherwise, you need to go through all lines.
首先,您的行
最好写为:
需要
+ 1
来为终止 '\0
' 字符提供空间;strlen()
没有解释这一点。在 C 中转换malloc()
的结果是不必要的,并且在某些情况下可以掩盖错误。sizeof(char)
根据定义为 1,因此不需要。并且您永远不会更改
targetline
的值,因此您的循环永远不会终止。但在回答你的问题时,如果你有一个文本文件并且你想阅读它的第 N 行,你必须阅读并跳过前 N-1 行才能到达它。 (可以设置一个单独的索引,但是创建索引无论如何都需要读取文件,并且在文件更改时保持索引最新是一个难题,可能超出了您现在正在做的范围。而且这并不是特别必要;从文件中读取 10 行的时间不会很明显。)
First off, your line
is better written as:
The
+ 1
is needed to provide room for the terminating '\0
' character;strlen()
doesn't account for that. Casting the result ofmalloc()
in C is not necessary, and in some cases can mask errors.sizeof(char)
is 1 by definition, so that's not needed.And you never change the value of
targetline
, so your loop will never terminate.But in answer to your question, if you have a text file and you want to read the Nth line of it, you have to read and skip the first N-1 lines to get to it. (It's possible to set up a separate index, but creating the index requires reading through the file anyway, and keeping the index current as the file changes is a difficult problem, probably beyond what you're doing now. And it's not particularly necessary; the time to read 10 lines from a file won't be noticeable.)
恐怕没有其他方法可以获取文件中的第 n 行。你必须经历。文件内不存在随机访问。
I'm afraid, there is no other way to get nth line in the file. You have to go through. There is no random acces within the file.
如果你想从文本文件中获取第 n 行,你必须读取它之前的 n-1 行。这就是顺序文件的本质。除非您知道所有行的长度相同,否则无法可靠地定位到特定行的开头。
If you want to get the nth line from a text file, you have to read the n-1 lines before it. That's the nature of a sequential file. Unless you know that all of your lines are the same length, there's no way to reliably position to the start of a particular line.