简体   繁体   English

在C编程中扫描文件中的整行

[英]Scan whole line from file in C Programming

I was writing a program to input multiple lines from a file. 我正在编写一个程序来从文件中输入多行。 the problem is i don't know the length of the lines, so i cant use fgets cause i need to give the size of the buffer and cant use fscanf cause it stops at a space token I saw a solution where he recommended using malloc and realloc for each character taken as input but i think there's an easier way and then i found someone suggesting using 问题是我不知道行的长度,所以我不能使用fgets因为我需要给出缓冲区的大小而不能使用fscanf因为它停在空格令牌我看到了一个解决方案,他建议使用malloc和realloc为每个角色作为输入,但我认为有一个更简单的方法,然后我发现有人建议使用

fscanf(file,"%[^\n]",line);

Does anyone have a better solution or can someone explain how the above works?(i haven't tested it) 有没有人有更好的解决方案或有人解释上述工作原理?(我还没有测试过)

i use GCC Compiler, if that's needed 如果需要,我使用GCC编译器

You can use getline(3) . 你可以使用getline(3) It allocates memory on your behalf, which you should free when you are finished reading lines. 它代表您分配内存,当您阅读完行后,您应该释放内存。

and then i found someone suggesting using fscanf(file,"%[^\\n]",line); 然后我发现有人建议使用fscanf(file,"%[^\\n]",line);

That's practically an unsafe version of fgets(line, sizeof line, file); 这实际上是fgets(line, sizeof line, file);的不安全版本fgets(line, sizeof line, file); . Don't do that. 不要那样做。

If you don't know the file size, you have two options. 如果您不知道文件大小,则有两个选项。

  1. There's a LINE_MAX macro defined somewhere in the C library (AFAIK it's POSIX-only, but some implementations may have equivalents). 在C库的某处定义了一个LINE_MAX宏(AFAIK只有POSIX,但有些实现可能有等价物)。 It's a fair assumption that lines don't exceed that length. 线条不超过该长度是公平的假设。

  2. You can go the "read and realloc" way, but you don't have to realloc() for every character. 您可以使用“read和realloc”方式,但不必为每个字符重新分配realloc() A conventional solution to this problem is to exponentially expand the buffer size, ie always double the allocated memory when it's exhausted. 这个问题的传统解决方案是指数地扩展缓冲区大小,即在耗尽时总是将分配的内存加倍。

A simple format specifier for scanf or fscanf follows this prototype scanf或fscanf的简单格式说明符遵循此原型

%specifier 

specifiers

As we know d is format specifier for integers Like this 我们知道d是整数的格式说明符,就像这样

[characters] is Scanset Any number of the characters specified between the brackets. [characters]Scanset括号中指定的任意数量的字符。 A dash (-) that is not the first character may produce non-portable behavior in some library implementations. 在某些库实现中,不是第一个字符的破折号( - )可能会产生非可移植行为。

[^characters] is Negated scanset Any number of characters none of them specified as characters between the brackets. [^characters]Negated scanset任意数量的字符,它们都没有指定为括号之间的字符。


fscanf(file,"%[^\n]",line);  

Read any characters till occurance of any charcter in Negated scanset in this case newline character 读取任何字符,直到Negated scanset中的任何字符出现,在本例中为newline character


As others suggested you can use getline() or fgets() and see example 正如其他人建议您可以使用getline()fgets()并查看示例

The line fscanf(file,"%[^\\n]",line); fscanf(file,"%[^\\n]",line); means that it will read anything other than \\n into line . 意味着它会读取以外的任何其他\\nline This should work in Linux and Windows, I think. 我认为这应该适用于Linux和Windows。 But may not work in OS X format which use \\r to end a line. 但可能无法在OS X格式中使用\\r来结束一行。

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

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