Thread: Help with user-defined input file name

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    8

    Help with user-defined input file name

    I am writing a program that would allow a user to input the name of the file they wish to open. I have added in there an option to enter a specific character ("Q" in this instance) to return to the previous menu (which I believe I have working properly).

    I am running into a little hiccup, whenever, I enter "1", it will return me to the previous menu.

    Below is the function:
    Code:
    ppr *readPPRForm(ppr *L){
        ppr *new = malloc(sizeof(ppr));
        char fileName[50];
        FILE *inFile = NULL;
    
    
        do{
            printf("Please enter the name of the file you wish to read (type Q to return): ");
            fgets(fileName, sizeof(fileName), stdin);
            removeNL(fileName);
    
    
            if (((fileName[0] = 'q') || (fileName[0] == 'Q')) && (fileName[1] =='\0')){
                return 0;
            }
            inFile = fopen(fileName, "r");
                if (inFile == NULL){
                    printf("Could not open %s file for reading.\n", fileName);
                }
        } while (inFile == NULL);
    
    
    
    
        fclose(inFile);
        return new;
    }
    Here is what the output looks like when I compile and test run it:

    Code:
    ***********************************
           PPR DATABASE FOR ORBI       
    ***********************************
    
    
    -----------------------------------
    |****SELECT FROM THE FOLLOWING****|
    | 1) Input New PPR Request        |
    | 2) Read External PPR Form       |
    | 3) Lookup Existing PPR Request  |
    | 4) Quit                         |
    -----------------------------------
    Option: 2
    Please enter the name of the file you wish to read (type Q to return): q
    -----------------------------------
    |****SELECT FROM THE FOLLOWING****|
    | 1) Input New PPR Request        |
    | 2) Read External PPR Form       |
    | 3) Lookup Existing PPR Request  |
    | 4) Quit                         |
    -----------------------------------
    Option: 2
    Please enter the name of the file you wish to read (type Q to return): 1
    -----------------------------------
    |****SELECT FROM THE FOLLOWING****|
    | 1) Input New PPR Request        |
    | 2) Read External PPR Form       |
    | 3) Lookup Existing PPR Request  |
    | 4) Quit                         |
    -----------------------------------
    Option: 4

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    193
    One of your == is an =.

    Maybe turn on all the compiler warnings?

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    8
    That did the trick! Thanks...

    It's amazing how such a small thing to overlook can cause such odd behavior...lol

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,541
    > return 0;
    Don't forget to clean up the malloc'ed memory which you're no longer using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create file with user defined name?
    By JohnRaphael in forum C Programming
    Replies: 2
    Last Post: 05-19-2016, 08:12 AM
  2. User defined output file name help!!!
    By nathank in forum C Programming
    Replies: 4
    Last Post: 05-07-2015, 07:42 AM
  3. File names using a user defined name
    By KnightMan in forum C Programming
    Replies: 2
    Last Post: 04-11-2011, 08:28 PM
  4. Input problem when using user-defined functions.
    By 843 in forum C Programming
    Replies: 12
    Last Post: 10-16-2010, 12:33 PM
  5. user defined file I/O
    By montablac in forum C++ Programming
    Replies: 3
    Last Post: 06-05-2005, 04:26 AM

Tags for this Thread