Thread: Scanning filename in?

  1. #1
    Registered User
    Join Date
    Apr 2009

    Scanning filename in?

    So usually we do:

    #define FILENAME input.txt
    But this project we have to prompt for the filename. I've tried doing:

    printf("Enter the name of the data file w/ extension: ");
          scanf("%c", &FILENAME);
    But it only scans in the first letter, which I somewhat expected. As far as I can tell, you can't do Strings in C, is this correct?

    If someone can please help me, what is the correct way to scan it in?

    On a side note, when I'm opening the file with:

     FILE *inp;
          inp = fopen( FILENAME, "r" );
    I get the warning:

    warning: passing arg 1 of `fopen' makes pointer from integer without a cast
    Not as big of a problem, but still an issue.


  2. #2
    Registered User
    Join Date
    Jan 2009
    The #define directive tells the C Preprocessor to replace the first 'argument' with the second 'argument' wherever it is used within the source file.

    So the part where you are trying to get input using scanf evaluates to something like 'scanf( "%c", &"input.txt");'.

    I'm not really sure if "input.txt" is put in read only memory or not, but you may not be able to write to it.

    The safest way is to create an array called filename. And then use that as the buffer for scanf.

    It should look something like:
    #include <stdio.h>
    #include <string.h> /* Needed for memset */
    #define MAXNAMELEN 20
    int main(void)
               char filename[MAXNAMLEN];
               memset( filename, '\0', MAXNAMLEN ); /*Fills 'filename' with 0 */
               scanf( "%20s", filename ); /*%s takes in strings, the 20 specifies the field width, to prevent buffer overflows/*
               return 0;
    And if you supply the array 'filename' as the first arg to fopen I think it will fix your problem.
    Last edited by DeadPlanet; 04-23-2009 at 11:40 PM. Reason: Forgot return value

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by jmack549
    As far as I can tell, you can't do Strings in C, is this correct?
    You can, since strings in standard C are null terminated character sequences. However, since this is C++, you could use:
    std::cout << "Enter the name of the data file w/ extension: ";
    std::string filename;
    std::cin >> filename;
    std::ifstream inp(filename.c_str());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Time as filename
    By rkooij in forum C Programming
    Replies: 8
    Last Post: 03-02-2006, 09:17 AM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM