简体   繁体   English

如何fgets()C中文件的特定行?

[英]How to fgets() a specific line from a file in C?

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: 所以,我试图找到一种方法来fgets()C中的文本文件中的特定行,以将该行的内容复制到更永久的缓冲区:

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). 所以基本上我不想遍历文件n-1次只是为了到达第n行...它只是看起来效率不高(而且,这是作业,我需要得到100%哈哈) 。

Any help would be appreciated. 任何帮助,将不胜感激。

If you know the length of each line, you can use fseek to skip to the line you want. 如果您知道每行的长度,可以使用fseek跳到所需的行。

Otherwise, you need to go through all lines. 否则,你需要经历所有行。

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 n th line. 文本文件不来与地图索引 ,它会让你跳到 n 线上。

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. 如果没有这种类型的额外知识,你就别无选择,只能遍历整个文件,直到达到所需的行。

First off, your line 首先,你的线

buffer =(char*)malloc(sizeof(char) * strlen(line));

is better written as: 写得更好:

buffer = malloc(strlen(line) + 1);

The + 1 is needed to provide room for the terminating ' \\0 ' character; 需要+ 1来为终止' \\0 '字符提供空间; strlen() doesn't account for that. strlen()没有考虑到这一点。 Casting the result of malloc() in C is not necessary, and in some cases can mask errors. 在C中转换malloc()的结果不是必需的,并且在某些情况下可以掩盖错误。 sizeof(char) is 1 by definition, so that's not needed. 根据定义, sizeof(char)是1,因此不需要。

And you never change the value of targetline , so your loop will never terminate. 而且你永远不会改变targetline的值,所以你的循环永远不会终止。

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. 但是在回答你的问题时,如果你有一个文本文件并且想要读取它的第N行,你必须阅读并跳过前面的第一行N-1行。 (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.) (可以设置一个单独的索引,但是创建索引需要通过文件读取,并且在文件更改时保持索引最新是一个难题,可能超出了您现在正在做的事情。并且它并不是特别必要;从文件中读取10行的时间不会很明显。)

I'm afraid, there is no other way to get nth line in the file. 我担心,没有其他方法可以在文件中获得第n行。 You have to go through. 你必须经历。 There is no random acces within the file. 文件中没有随机访问。

If you want to get the nth line from a text file, you have to read the n-1 lines before it. 如果要从文本文件中获取第n行,则必须先读取n-1行。 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. 除非您知道所有线都是相同的长度,否则无法可靠地定位到特定线的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM