./sample < input.txt
コマンドでファイルから読み込みたいが、EOFで停止しない。
私はそれがEOFであることを確認するためにCtrl + Cを押す必要があります。
どうすれば複数の行を読むことができますか?
私はそれをctrl + cなしでEOFを決定できるメインループに入れる必要があります。 私が計画したように、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);
}
}
}
でコードを書くのか? – Gangadhar
実際には、私はデバッグ方法がわかりません:(私はcygwinで作業していますが、デバッグしようとしている場合は – user1289547
@ user1289547入力ファイルの最初の文字は改行ではありませんか?サンプル入力ファイルの内容。 –