我想從./sample < input.txt
命令中讀取文件,但它不會因EOF而停止。
我需要按Ctrl + C來確定它是EOF。
那麼我怎樣才能閱讀多行?
我需要把它放入主循環,它可以確定EOF沒有按Ctrl + C。 按照我的計劃,必須有2個循環: 主循環:從文件中獲取其他行,插入新節點以填充條件應該是「EOF」。 子循環:按字符填充字符數組,條件應爲「\ n」。鏈接列表文件輸入,不能用EOF停止
#include <stdio.h>
#include <stdlib.h>
#define SIZE 80
struct node {
char str[SIZE];
struct node* next;
};
void read_line(struct node* line_list);
int main(void)
{
struct node* line_list = NULL; // linked list - headp
read_line(line_list);
return 0;
}
void read_line(struct node* line_list)
{
int ch, i = 0;
struct node* temp = malloc(sizeof(struct node));
if(temp!=NULL) {
while((ch = getchar()) != EOF){
if (ch != '\n')
{
temp->str[i] = '\0';
//insert(&line_list,temp.str);
break;
}
if (i < SIZE)
temp->str[i++] = ch;
printf("New str: %s\n",temp->str);
}
}
}
當你調試的代碼是什麼ch的值時,EOF達到? – Gangadhar 2012-03-24 05:07:57
其實,我真的不知道如何調試:(我在cygwin上工作,如果你告訴我,我可以嘗試調試它 – user1289547 2012-03-24 05:21:15
@ user1289547你確定輸入文件中的第一個字符不是換行符嗎?樣本輸入文件的內容。 – 2012-03-24 05:54:39