File I/O, help

This is a discussion on File I/O, help within the C Programming forums, part of the General Programming Boards category; Hey guys need help on opening a file in c. i know how to open a file using a hardcoded ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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,528
    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 06: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-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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