简体   繁体   English

在C中逐行读取文件

[英]Reading file Line By Line in C

Preface: 前言:

This question is about reading a file, line by line, and inserting each line into a linked list. 这个问题是关于逐行读取文件,以及将每一行插入链表。

I have already written the implementation for the linked list, and tested the insert() function manually. 我已经编写了链表的实现,并手动测试了insert()函数。 This works. 这有效。 I have also written the code for reading text from a file, and writing it out. 我还编写了从文件中读取文本并将其写出来的代码。 Again, this also works. 同样,这也有效。

OKAY: HERE'S MY QUESTION 好的:这是我的问题

How can I merge these concepts, and write a function that reads text from a file, line by line , and inserts each line as a node in the linked list ? 如何合并这些概念,并编写一个逐行读取文件中的文本的函数, 并将每一行作为节点插入到链表中

When reading from a file, I do the following: 从文件中读取时,我执行以下操作:

//Code for reading a file
int c;
while((c = getc(f))!= EOF) {
    putchar(c);  //Prints out the character
}
fclose(f);  //Close the file

The insert() function takes two parameters, one being the linked list head node, and the second one being the dataEntry ("string") to be held in that node. insert()函数有两个参数,一个是链表头节点,第二个是要保存在该节点中的dataEntry(“string”)。

void insert(node_lin *head, char *dataEntry) { ... }

Hence, since the function getc gets each character separately, and the putchar writes out each character to the screen, I imagine the code to do something along these lines: 因此,由于函数getc分别获取每个字符,并且putchar将每个字符写出到屏幕,我想象代码可以沿着这些行做某些事情:

  • Read each character until the end of file (EOF) 读取每个字符直到文件结尾(EOF)
  • For each character, until reaching a new line ('\\n'), append this to the previously read characters (building a "string) 对于每个字符,在到达新行('\\ n')之前,将其附加到先前读取的字符(构建“字符串”)
  • If reaching the end of the line, insert this "string" into the linked list 如果到达行尾,请将此“字符串”插入链接列表
  • Repeat until reaching EOF 重复直到达到EOF

      //Code for reading a file int c; while((c = getc(f))!= EOF) { //Build a string here consisting of characters from c, until reaching a new line. /* if(c == '\\n') { //Indicates a new line //Insert the line you have into the linked list: insert(myLinkedList, line); } */ } fclose(f); //Close the file 

The thing is, I already have a working read_file function, as well as a working insert() function. 问题是,我已经有一个工作的read_file函数,以及一个工作的insert()函数。 The thing I need help with is separating the file into lines, and inserting these. 我需要帮助的是将文件分成行,然后插入这些。

Thanks guys! 多谢你们!

Replace your character-by-character reading by something more high-level. 用更高级别的东西替换你的逐字符阅读。

  • The most typical choice would be fgets() , but that requires you to specify a static limit for the line's length. 最典型的选择是fgets() ,但这需要您为行的长度指定静态限制。
  • If you have getline() you can use that, it will handle any line-length but it is POSIX, not standarc C. 如果你有getline()你可以使用它,它将处理任何行长,但它是POSIX,而不是standarc C.

Also, you should change your insert() function to accept const char * as the second argument (and remember to allocate memory inside and copy the text, of course). 此外,您应该更改insert()函数以接受const char *作为第二个参数(并记住在内部分配内存并复制文本)。

You can use fgets to read entire line from the file until new line character is encountered 您可以使用fgets从文件中读取整行,直到遇到换行符

fgets (buffer, 128, f);

When reading from a file, you can do the following: 从文件中读取时,您可以执行以下操作:

//Code for reading a file
char buffer[128];                 // decide the buffer size as per your requirements.
while((fgets (buffer, 128, f))!= NULL) {
    printf (buffer);
}
fclose(f);  //Close the file

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

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