problem with fopen command

This is a discussion on problem with fopen command within the C Programming forums, part of the General Programming Boards category; i'm writing a program where i'm supposed take the name of a file with it's full path which will be ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    problem with fopen command

    i'm writing a program where i'm supposed take the name of a file with it's full path which will be fed by the user.then i'm supposed to store it in an array of charecters.then i'm supposed to use the fopen command to open file where the file name would be the one that is stored in the previous array.but the problem is that fopen only accepts strings within double quotes like following:

    File *f;
    f=fopen("name","w");

    that's why i cannot pass my array as the first parameter for the fopen command.so what can i do to have the work done(take the file name specified by the user and using that name open that file using fopen).i' also taking care of the double \\ in my array.plzzzzz help....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but the problem is that fopen only accepts strings within double quotes
    fopen doesn't care if the string is a string literal or a pointer to a string literal or an array of char terminated by '\0'. The following is perfectly acceptable:
    Code:
    char *name = "/me/dir/file";
    FILE *out = fopen ( name, "w" );
    My best code is written with the delete key.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    don't forget to take out the \\ if you are in windows and also don't forget the \n if you use fgets to get the filename Ex:
    Code:
    printf("Enter a file:  ");
    fflush(stdout);
    fgets(input,sizeof input,stdin);
    input[strlen(input-1)]='\0';
    file=fopen(input,"w");
    ....
    fclose(file);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. command line parameters problem...
    By ac251404 in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2006, 01:44 PM
  3. in program command console
    By howzer in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2006, 03:12 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 12:54 PM

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