How do you open a file from user input?

This is a discussion on How do you open a file from user input? within the C Programming forums, part of the General Programming Boards category; I need to open a file that will be entered by the user and I don't know how to do ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    42

    How do you open a file from user input?

    I need to open a file that will be entered by the user and I don't know how to do this. I tried putting

    Code:
    ifp= fopen("%s", "r", &filename);
    
    //also tried:
    
    ifp= fopen(filename, "r");

    Code:
    printf("Enter the name of the file with the ticket data.\n");
        scanf("%s", filename);
       
        FILE *ifp;


    so what is the command you use to open a file based on user input that was stored in a variable filename?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    ... got it. why do i always get it right after i ask for help.

    i didn't have &filename in my scan i just had filename and then when i used
    Code:
        ifp= fopen(filename, "r");

    it worked.


    but thanks for looking.
    Last edited by DaNxTh3xMaNx; 08-29-2010 at 11:25 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    This is incorrect.
    It should be:
    Code:
    printf("Enter the name of the file with the ticket data.\n");
    if (fgets(filename, sizeof(filename), stdin))
        filename[strlen(filename)] = '\0';
    FILE *ifp = fopen(filename, "r");
    SourceForge.net: Scanf woes - cpwiki
    I also urge you to check your local documentation on fopen. It does not behave like scanf or printf.

    Disclaimer: Not tested. Use at your own risk.
    Last edited by Elysia; 08-29-2010 at 11:01 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    ^^^yea that crashes right after i type in the filename.



    is that necessary for a first assignment in computer science 1? it seems a bit over my head.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Yes, because scanf is unsafe.
    And for some reason, those who made the standard thought that fgets should leave the newline in the input buffer, making it even more pain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You cannot use %s with fopen. Try something like this.

    Code:
    char filename[256];
    printf("Filename: ");
    scanf("%s", &filename);
    FILE *in = fopen(filename, "r");
    if (in == NULL) printf("File does not exist.\n");
    else printf("File exists.\n");

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    scanf("%s", &filename);
    Question 12.12b

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Babkockdood: READ what has already been posted.
    You should NOT read strings with scanf for reasons already stated in a link. And passing an array with & is undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by Elysia
    And passing an array with & is undefined behavior.
    Passing the address of an array to scanf with a corresponding %s format specifier, that is.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 05:24 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Replies: 16
    Last Post: 01-04-2007, 03:38 PM

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