using path names

This is a discussion on using path names within the C Programming forums, part of the General Programming Boards category; Is there a way to ask a user to enter a full path name and then open the file that ...

  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
    twm
    twm is offline
    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, 10:13 AM
  2. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-24-2008, 12:47 AM
  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 DeepBlackMagic in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 03:34 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

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