Thread: reading in a filename correcty

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    reading in a filename correcty

    I'm currently in a intro to C programming class and in a lab we have to open a file to read in data for a maze. The following code is what i have so far and i am using a extra printf statement to debug and figure out why my file reading in ability is not working. when i type in a file path such as "c:\test data.txt" it only scans the first part of the string "c:\test" into char filename. how do i get it scan the whole string into filename?

    Thanks
    Bobby

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    
    FILE *fp;
    char filename[1024];
    
    void get_file ();
    
    int main () {
    
    	get_file ();
    
    	return 0;
    }
    
    void get_file () {
    	char filename[1024];
    	printf("Please enter the filepath of the maze...\n");
    	fgets(filename, 1024, stdin);
    	printf("%s", filename);
    	}
    Last edited by Salem; 04-07-2010 at 10:57 PM. Reason: Added [code][/code] tags, learn to use them yourself

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Welcome to the forum Bobby!

    Please note for future postings that code needs to appear between code tags. You can read the sticky note for posting instructions on this forum for more about that.

    As for your code, it works perfectly on my machine. I don't really know what the problem is. Is that everything you have? If not post your entire code.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %s, by design, stops reading at a space. If you don't want that to happen, then don't use %s. You can use the %[ conversion specifier, or you can use fgets (although that has its own end-of-line quirks).

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's not that. I just ran his code in CodeBlocks and it works fine with his example (i.e. "C:\test data.txt"). He must be doing something else he is not showing us.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ha, apparently I've gone insane. So: since you are using fgets after all, fgets has an end-of-line quirk; you could see this if you did something like
    Code:
    printf("%son the same line!\n", filename)
    Since your filename presumably doesn't have a new-line in its name, you will need to remove that character. (Searching on fgets and \n will probably give you lots and lots of examples of same.)

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I don't get why printf doesn't print the entire filename though.

    As for how to handle the newline problem, see this:
    https://www.securecoding.cert.org/co...ng+fgets%28%29

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Help with reading strings and opening files PLEASE
    By green2black in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 05:46 PM
  4. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  5. Reading whole file w/ ifstream
    By cboard_member in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:09 AM