0

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);
    }
EthanGlory
  • 17
  • 1
  • 5
  • Because you don't read the file. You only open it – RoiHatam May 05 '17 at 03:54
  • Is this all the code? That is just opening a flle. Where is the code for reading? – Nguai al May 05 '17 at 03:57
  • @Nguaial I can show you all the code if you need to see it. I was just thinking the problem lies in these lines since the file doesn't open. – EthanGlory May 05 '17 at 04:02
  • What does "isn't working" mean? How do you know it doesn't open? Please provide a [mcve]. – kaylum May 05 '17 at 04:03
  • 1
    the file isn't sought in the folder of the program but *in the current working directory*. – Antti Haapala -- Слава Україні May 05 '17 at 04:04
  • I concur with `Pras`. There is definitely a problem with opening a file. Always check success of opening of a file: `if(input == NULL ){ printf{"File open failed.\n"); return 1;}` – Nguai al May 05 '17 at 04:06
  • Thanks for adding more code. It is still not an MCVE but at least more complete. Please also provide the input and what "doesn't working" means - usually be describing the expected behaviour and the actual behaviour. Are you sure the input is a valid full path? Please show that. – kaylum May 05 '17 at 04:08
  • Refrain from using while(!feof)[link](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Nguai al May 05 '17 at 04:11
  • @nguaial here is the function in question. – EthanGlory May 05 '17 at 04:12
  • 2
    "The program does nothing. I just get a blank line it doesnt return to menu". You can do much better than that. Use a debugger to trace the execution of the program. – kaylum May 05 '17 at 04:16
  • Can you also provide a sample of an input file? – Nguai al May 05 '17 at 04:24
  • @kaylum I may be doing it wrong but I ran the debugger through till that line setting up break points before and after the scanf. I treid giving the NEW.txt as a (args) i.e. run New.txt but that didn't work. How would I give that argument to my program while in the debugger since I don't take the extra arguments in main but this sub function – EthanGlory May 05 '17 at 04:35
  • 1
    `scanf("%s",filename);` reads from stdin not from the command line. That is, you need to run your program first, then type `New.txt`. – kaylum May 05 '17 at 05:49
  • @kaylum I have been looking online on how to do it but everyone just shows how to do it from command line. Would you know how? Because when I run the program in gdb it doesn't stop to let me type the file in it's goes straight to the break point after the scanf. – EthanGlory May 05 '17 at 12:51

1 Answers1

1

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);
}
RoiHatam
  • 876
  • 10
  • 19
  • I already know how to get files that you hard code into the program. I wanted to know how to get them from the user input. – EthanGlory May 05 '17 at 04:48
  • Ethanglory you can use arguments that is from the main function to read file from stdin. You just need to pass text file as argument when you execute the program – danglingpointer May 05 '17 at 07:46