Search code examples
cuser-inputscanf

How to read a .txt file from user input in C


I know how to hardcore a program to receive a file but when I try a similar tactic with scanf nothing happens. I mean that I have an error check that looks to see if it exist and if it has the right format but everytime I enter the filename it doens't print the printf statement below the scanf. Why is that? I also found out that I am opening the file but the while statement is infinite. Which doesn't make sense. I have tried another solution shown below but same results.

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");
}

When I got to the parsefile() function and enter NEW.txt which is in the correct format and inside the same folder as the program itself. I know that my check is working when I enter a .txt file that doesn't exist or that is empty it gets caught like it should.

The expected behavior is that the program should load this list from new.txt and load it into a doubly linked list. Then return to a menu that gives user options. The doubly linked listed can then be manipulated such as add students manipulate data, deleting, saving and printing current roster. I have trouble using gdb with this program since I receive new.txt from parsefile.

Sample of New.txt contents. (Its just First Name, Last Name, Id, Age)

Belinda Homes 345 50

Scott Crown 456 18

Failed Solution: Using fgetc instead of 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);
    }

Solution

  • This is the easiest way to read from file and print out. I assume you want to print the file or do something with it.

    int c;
    FILE *file;
    file = fopen("test.txt", "r");
    if (file) {
        while ((c = getc(file)) != EOF){
            putchar(c);
        }
        fclose(file);
    }