User input file

This is a discussion on User input file within the C Programming forums, part of the General Programming Boards category; Hey guys, so I have a bit of a silly problem. I'm starting to write a program in MS visual ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    User input file

    Hey guys, so I have a bit of a silly problem. I'm starting to write a program in MS visual studio that requires me to open a user specified file. I've created the file in notepad, but when I run the code, it always tells me that the file doesn't exist and that's not true. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #ifdef _MSC_VER
    #include <crtdbg.h>  // needed to check for memory leaks (Windows only!)
    #endif
    
    
    #include "stackADT.h"
    
    
    int main (void)
    {
    //  Local Definitions
        FILE *fp;
        char filename[100] = "";
    
    
    //  Statements
        printf("\n\n\t\tHomework 1: STACK ADT\n\n");
        printf("\tThis program ...\n" );
        printf("Please enter the name of the file that you want to use: \n");
        scanf("%s", &filename);
        fp = fopen(filename, "r");
                if(fp == NULL)
                {
                        printf("Your filename was not found!\n");
                        exit(100);
                }
    
    
    
    
    
    
    
    
    
    
    
    
        printf("\n\t\tThank you for using the program,"
               "\n\t\tHave a great day!\n");
    
    
        #ifdef _MSC_VER
        printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
        #endif
    
    
        return 0;
    
    
    } // main
    Also, for some reason my #include stackADT.h is not recognized... but that's a piece of code that my teacher specified. Why does the compiler not like it?

    Thank you for your time.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I've tried specifying the directory path and everything without any luck... Anyone have any ideas? Oh and I removed the & in front of filename for the scanf. I realized that that was an error.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    In theory, there looks like there should be nothing wrong with this code. Try (i) printing out the name of the file before you try to open it and (ii) printing the value of errno when it fails (there are several reasons an fopen() can fail):

    Code:
    #include <errno.h>
    #include <string.h>
    
    // ...
        printf("Opening file '%s'\n", filename);
        fp = fopen(filename, "r");
        if(fp == NULL)
        {
            printf("Error opening file: %s\n", strerror(errno));
            exit(100);
        }
    (Then post output if this doesn't make things clearer...)
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Okay, so I did the printf statement before opening the file and it prints out the file name just fine. When I use the strerror it says "No such file or directory". Any other suggestions?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    IDEs usually set a working directory, so you probably won't find files in other places.

    If you're feeling confused about that problem, you could ask the user for the path to the file as well.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I tried specifying the file path and everything. Its getting really annoying. Do you know where the IDE working directory is for visual studio? Because the user input is really just me. I'm not planning on anyone else using the program. Its just that the teacher wants us to get the file name from the user. But I even tried skipping the user and just manually writing the file name and even the path. But still, nothing.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    I tried specifying the file path and everything. Its getting really annoying, Its getting really annoying.
    That's what we're here for. You should explain how you did that in some detail. If you did it with new code, you might have messed it up and the answer could be simple.

    Do you know where the IDE working directory is for visual studio?
    Usually the same as the current project folder.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I tried specifying the path with C:\\User\Anastasia\Documents... And a bunch of other methods. Basically using the path that I see when I open that notepad document. I tried adding .txt at the end and removing it. I've bypassed the user input and simply typed in the filename/pathname in the fopen statement. Not quite sure what else I can do. Oh, and I tried saving the notepad doc in the same folder as visual studio as well.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    A lot of that sounds really wrong.

    First of all, using backslashes in strings - you need to escape each backslash character ('\\'). [edit] And the console will escape input for you, so you don't need it for user input, that's important. And I forgot. [/edit]
    The extension is part of the file name, don't remove it.
    You cannot take a variable like filename and glue it to a file path without code to concatenate the string. Concatenate is a real word, by the way.

    If you have a string that is big enough you can do something like:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void RemoveNewlines(char *s)
    {
        char *nl = strrchr(s, '\n');
        if (nl!=NULL)
            *nl = '\0';
    }
    
    int main(void)
    {
        char OpenMe[FILENAME_MAX] = "";
        char enteredpath[FILENAME_MAX / 2];
        char enteredname[FILENAME_MAX / 2];
    
        FILE *fil = NULL;
    
        printf("Please enter the file path:\n");
        fgets(enteredpath, FILENAME_MAX / 2, stdin);
        RemoveNewlines(enteredpath);
    
        printf("Please enter the file name:\n");
        fgets(enteredname, FILENAME_MAX / 2, stdin);
        RemoveNewlines(enteredname);
    
        strcat(OpenMe, enteredpath);
        strcat(OpenMe, enteredname);
    
        printf("Opening %s...\n", OpenMe);
    
        fil = fopen(OpenMe, "r");
        if (fil!=NULL)
        {
            printf("good\n");
            fclose(fil);
        }
        return 0;
    }
    /*
    Please enter the file path:
    C:\Users\Josh2\My Documents\
    Please enter the file name:
    hello.txt
    Opening C:\Users\Josh2\My Documents\hello.txt...
    good
    */

    Of course, reading the path requires reading in spaces so scanf(%s", enteredpath); is not sufficient. Use fgets and remove any '\n' in the path.
    Last edited by whiteflags; 10-07-2012 at 01:54 AM. Reason: Some better code...

  10. #10
    Registered User
    Join Date
    Oct 2012
    Location
    Florida
    Posts
    2
    Put the file into your Debug/bin files for VS to use the file.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Yes! Thank you! That's what I did and it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2012, 09:35 AM
  2. Replies: 3
    Last Post: 08-28-2010, 10:21 PM
  3. User input into a text file
    By Starr in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2006, 07:52 PM
  4. Open file from user input?
    By p0gig in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 08:09 AM
  5. User Input and File Handling
    By Disturbed1 in forum Game Programming
    Replies: 1
    Last Post: 05-27-2003, 12:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21