Thread: File I/O, help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    File I/O, help

    Hey guys need help on opening a file in c. i know how to open a file using a hardcoded path

    for example: "C:\Program Files\input.txt"
    but i want to be able to type any path and then c searches for the file and then opens it,

    example: "Input the location for the input.txt file" , then the user types the path "C:\Documents and Settings\Temp\Input.txt" and then c will look for the text file in the indicated folder, and open it. Any help would be appreciated

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    i have attempted the following, and it does ask the user to input a path for the file, however when the program runs it always says "cannot open the file selected", i am trying to open "u:\input.txt"

    Code:
    int main()
    {
    	char filename;
    
    option_1:
    	printf("Option 1 has been selected\n");
    	printf("Please enter the full path of the file to be loaded: ");
    	scanf("%s", filename);
    	getchar();
    		
    		FILE * fptr;
    		if((fptr = fopen(filename,"r")))
    		{
    			printf("Cannot open the file selected\n);
    		}
    
    		else
    		{
    			printf("The file has opened sucessfully for reading\n\n");
    		}
    		
    		int fclose(FILE * fptr);
    	return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Recommendation: You should put the filename in the message, like
    Code:
    printf("Cannot open the file %s selected\n", filename);
    which will allow you to immediately see if there's something up with how your filename got input. In this case, you will find the error immediately, as the compiler will then tell you that filename is not a C-style string, but merely a single char.

    And of course the file has to previously exist.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Also have a look at this bit of your code:

    Code:
    char filename;
    ...
    scanf("%s", filename);
    The filename you want to grab from the user will be input as an array of chars terminated by a nul character, AKA, a string. Armed with this knowledge, what sort of data type would you store an array of chars in?
    Last edited by kermit; 04-11-2010 at 05:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM