Thread: using path names

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    using path names

    Is there a way to ask a user to enter a full path name and then open the file that way?
    I've got this right now and assumed it would work but it doesn't
    Does anyone have experience with this sort of thing?

    Code:
    printf("Enter full path and file name");
    scanf("%s",&path_name);
    
    readfile = fopen(path_name, "r");

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >scanf("%s",&path_name);
    You don't need the &, otherwise it should work. Try using perror to see if you have any errors opening the file. Most of the time the path will be mistyped or the file doesn't exist, such is the way with interactive input. It's usually wrong.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm bored, so here's a quick version:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char  foo[BUFSIZ];
      FILE  *fp;
    
      printf("Enter path: ");
      fflush(stdout);
      if (fgets(foo, sizeof(foo), stdin))
      {
        strtok(foo, "\n");
        if ((fp = fopen(foo, "r")) == NULL)
        {
          perror(foo);
        }
        else
        {
          fclose(fp);
        }
      }
    
      return(0);
    }
    
    /*
    Output
    
    Enter path: c:\mydir\myfile.txt
    c:\mydir\myfile.txt: No such file or directory
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    thanks guys for all your help

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I didn't say it on my previous post, so I thought I'd better say it now... scanf() isn't a good choice for grabbing text from the user, as it can easily assist in buffer overflows. As you probably noticed, I recommended fgets() .
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-23-2008, 11:47 PM
  3. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  4. Path Finding Using Adjacency List.
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 02:34 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM