user defined path for FILENAME

This is a discussion on user defined path for FILENAME within the C Programming forums, part of the General Programming Boards category; When I need to use a data file I'll put it in the preprocessor directives: #include FILENAME "c:\datafile.dat" Is there ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    Question user defined path for FILENAME

    When I need to use a data file I'll put it in the preprocessor directives:

    #include FILENAME "c:\datafile.dat"

    Is there a way of assigning FILENAME to some user defined file name? In other words, I would like to:

    printf ("Enter the path of your data file =>");

    and somehow associate this entry with FILENAME. I've tried playing around with some string subroutines and I can't figure out how to associate a string to a preprocessor directive (if that's even possible).

    TIA for your response,

    drdigimon

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: user defined path for FILENAME

    Originally posted by drdigimon
    When I need to use a data file I'll put it in the preprocessor directives:

    #include FILENAME "c:\datafile.dat"

    Is there a way of assigning FILENAME to some user defined file name? In other words, I would like to:

    printf ("Enter the path of your data file =>");

    and somehow associate this entry with FILENAME. I've tried playing around with some string subroutines and I can't figure out how to associate a string to a preprocessor directive (if that's even possible).

    TIA for your response,

    drdigimon
    Note the term you used: preprocessor directive . That specifically means "before compiling the program." Therefore the program would already be built using whatever include files are specified.

    You may need to simply read the data from the specified file as the program starts.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Basically (and crudely)

    Code:
    char filename[100];
    printf ("Enter the path of your data file =>");
    fflush( stdout );
    scanf( "%s", filename );
    fp = fopen( filename, "r" );
    if ( fp == NULL ) complain_to_user();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    and it is not
    #include FILENAME "c:\.datafile.dat"
    it is
    #define FILENAME "c:\datafile.dat"
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>it is
    >>#define FILENAME "c:\datafile.dat"
    Don't forget to double up on the back slashes in the source:

    #define FILENAME "c:\\datafile.dat"
    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. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 04:21 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Problem returning values in user defined format
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 11-20-2006, 04:20 PM
  4. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 11:09 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM

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