Question about fopen

This is a discussion on Question about fopen within the C Programming forums, part of the General Programming Boards category; I am trying to open a file to write too but the user needs the ability to select what file ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    23

    Question about fopen

    I am trying to open a file to write too but the user needs the ability to select what file they want to open. How do I use fopen to open a string and not a filename?

  2. #2
    The Determined
    Join Date
    Nov 2005
    Location
    England
    Posts
    5
    Just put it in the fopen function...

    Code:
    boolean file(char *filename)
    {
    	FILE *file;
    
    	if ((file = fopen(filename, "w")) == NULL) {
                           return 0;
    	} 
    	else 
    	{
                          //code
                    }
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    thanks

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Code:
    int main()
    {
    
    	char *input, *output;
       FILE *ifp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%c", &input);
    	if ((ifp = fopen(input, "r")) == NULL) 
    	{ 
    		return 0; 
    	}
    	 
    	printf("Which file would you like to write too?\n");
    	scanf("%c", &output);
    	if ((ifp = fopen(output, "w")) == NULL) 
    	{ 
    		return 0; 
    	} 
    
    
    
    	return 0;
    	
    }
    After the first scanf the program is exiting now. Sorry I am new too this.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    input is a pointer, you need a character array
    Code:
    char filename[_MAX_PATH]; // in MS-Windows this is defined as 255
    printf("Which file would you like to read from?\n");
    // use fgets() instead of scanf so that you can enter filenames with spaces
    fgets(filename,sizeof(filename),stdin);
    // now open the file using the above filename.
    if ((ifp = fopen(filename, "w")) == NULL) 
    { 
    	return 0; 
    }
    Last edited by Ancient Dragon; 11-15-2005 at 03:07 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Don't forget that fgets() leaves a '\n' at the end of the input string. You'll have to chop that off before using it in fopen().
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen question
    By DramaKing in forum C Programming
    Replies: 17
    Last Post: 12-19-2001, 10:09 PM

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