[英]How to read a .txt file from user input in C
我知道如何對程序進行核對以接收文件,但是當我嘗試使用scanf采取類似策略時,什么也沒發生。 我的意思是,我進行了一次錯誤檢查,看是否存在並且格式是否正確,但是每次我輸入文件名時,它都不會在scanf下方打印printf語句。 這是為什么? 我還發現我正在打開文件,但是while語句是無限的。 這沒有意義。 我嘗試了下面顯示的另一種解決方案,但結果相同。
void parseFile(struct student_record_node** head)
{
FILE*input;
const int argCount = 4;
char filename[100]="";
const char rowformat[] = "%20s %20s %d %d";
struct student_record record;
struct student_record_node* node = NULL;
printf("\n Please Enter the FULL Path of the .txt file you like to load. \n");
scanf("%s",filename);
input = fopen(filename, "r");
printf("I am here");
if(input == NULL)
{
printf("Error: Unable to open file.\n");
exit(EXIT_FAILURE);
}
while(!feof(input))
{
/* creating blank node to fill repeatedly until end of document*/
memset(&record, 0, sizeof(struct student_record));
if(fscanf(input, rowformat, record.first_name_,record.last_name_,&record.student_id_,&record.student_age_) != argCount)
{
continue;
}
/* set node into the doubly linked list */
node = student_record_allocate();
/* copies values from the blank node reading from document into node in my linked list */
strcpy(node->record_->first_name_, record.first_name_);
strcpy(node->record_->last_name_, record.last_name_);
node->record_->student_id_ = record.student_id_;
node->record_->student_age_ = record.student_age_;
/* check if node right after absolute head is empty if so fills it */
if(*head == NULL)
{
*head = node;
}
else
{
printf(" stuck in loop\n");
/* if current isn't null start linking the node in a list */
appendNode(head,node);
}
}
fclose(input);
printf(" end of parsefile");
}
當我進入parsefile()函數並輸入NEW.txt時,其格式正確,並且位於程序本身所在的文件夾中。 我知道當我輸入一個不存在或為空的.txt文件時,支票正在運行,它會像應有的那樣被捕獲。
預期的行為是程序應從new.txt加載此列表並將其加載到雙向鏈接列表中。 然后返回提供用戶選項的菜單。 然后可以操縱列出的雙向鏈接,例如添加學生操縱數據,刪除,保存和打印當前花名冊。 我在此程序中使用gdb遇到麻煩,因為我從parsefile中收到了new.txt。
New.txt內容示例。 (僅是名字,姓氏,ID,年齡)
貝琳達住宅345 50
斯科特皇冠456 18
失敗的解決方案:使用fgetc代替feof
int c = fgetc(input);
while(c != EOF)
{
printf("\n in loop \n");
/* creating blank node to fill repeatedly until end of document*/
memset(&record, 0, sizeof(struct student_record));
if(fscanf(input, rowformat, record.first_name_,record.last_name_,&record.student_id_,&record.student_age_) != argCount)
{
continue;
}
/* set node into the doubly linked list */
node = student_record_allocate();
/* copies values from the blank node reading from document into node in my linked list */
strcpy(node->record_->first_name_, record.first_name_);
strcpy(node->record_->last_name_, record.last_name_);
node->record_->student_id_ = record.student_id_;
node->record_->student_age_ = record.student_age_;
/* check if node right after absolute head is empty if so fills it */
if(*head == NULL)
{
*head = node;
}
else
{
printf(" stuck in loop\n");
/* if current isn't null start linking the node in a list */
appendNode(head,node);
}
c = fgetc(input);
}
這是從文件讀取並打印出來的最簡單方法。 我假設您要打印文件或對其進行處理。
int c;
FILE *file;
file = fopen("test.txt", "r");
if (file) {
while ((c = getc(file)) != EOF){
putchar(c);
}
fclose(file);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.