Scanning filename in?

This is a discussion on Scanning filename in? within the C++ Programming forums, part of the General Programming Boards category; So usually we do: Code: #define FILENAME input.txt But this project we have to prompt for the filename. I've tried ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Scanning filename in?

    So usually we do:

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

    Code:
    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:

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

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

    THANKS!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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:
    Code:
    #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
    Location
    Singapore
    Posts
    21,629
    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:
    Code:
    std::cout << "Enter the name of the data file w/ extension: ";
    std::string filename;
    std::cin >> filename;
    
    std::ifstream inp(filename.c_str());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:17 AM
  5. Replies: 3
    Last Post: 01-25-2006, 09:30 PM

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