从C程序文件扫描整条生产线 [英] Scan whole line from file in C Programming

查看:95
本文介绍了从C程序文件扫描整条生产线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序从文件中输入多行。
问题是我不知道该线的长度,所以因为我需要给缓冲区和不能使用的大小FSCANF导致它停在一个空令牌我不能使用与fgets
我看见他在那里使用作为输入的每个字符的malloc和realloc推荐一个解决方案,但我认为还有一个更简单的方法,然后我发现有人使用提示

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

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

有没有人有更好的解决方案,或有人可以解释如何在上述工程?(我没有测试过)

Does anyone have a better solution or can someone explain how the above works?(i haven't tested it)

我使用GCC编译,如果这是需要的。

i use GCC Compiler, if that's needed

推荐答案

您可以使用<一个href=\"http://www.openbsd.org/cgi-bin/man.cgi?query=getline&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html\"相对=nofollow>函数getline(3)。它分配您的名义,内存,当你读完线,你应该释放。

You can use getline(3). It allocates memory on your behalf, which you should free when you are finished reading lines.

这篇关于从C程序文件扫描整条生产线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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