nor*_*ner 2 c file linked-list
前言:
这道题是关于逐行读取文件,并将每一行插入到一个链表中。
我已经编写了链表的实现,并手动测试了insert()函数。这有效。我还编写了从文件中读取文本并将其写出的代码。同样,这也有效。
好的:这是我的问题
如何合并这些概念,并编写一个函数,从文件中逐行读取文本,并将每一行作为链接列表中的节点插入?
从文件中读取时,我执行以下操作:
//Code for reading a file
int c;
while((c = getc(f))!= EOF) {
putchar(c); //Prints out the character
}
fclose(f); //Close the file
Run Code Online (Sandbox Code Playgroud)
insert ()函数有两个参数,第一个是链表头节点,第二个是该节点中要保存的 dataEntry(“字符串”)。
void insert(node_lin *head, char *dataEntry) { ... }
Run Code Online (Sandbox Code Playgroud)
因此,由于函数 getc 分别获取每个字符,并且 putchar 将每个字符写到屏幕上,我想象代码会按照以下顺序执行某些操作:
重复直到到达 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
Run Code Online (Sandbox Code Playgroud)问题是,我已经有一个有效的 read_file 函数,以及一个有效的 insert() 函数。我需要帮助的是将文件分成几行,然后插入它们。
多谢你们!
| 归档时间: |
|
| 查看次数: |
15233 次 |
| 最近记录: |