Thread: Function and stuff

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Function and stuff

    Hi all,

    I have been writing a function which opens a file and return the handling for future use… I’m missing something there… can you please help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    FILE* openFile (char* text, char* handleName);
    		
    void main ()
    {	
    	openFile ("enter filename: ", "myHandleName");
    
    }
    
    FILE* openFile (char* text, char* handleName)
    {
    	char fileName[25];
    	printf ("%s", text);
    	scanf ("%s",fileName);
    	handleName=fopen (fileName,"rt"); //opening second file for reading.
    	if (handleName==NULL)
    	{
    		printf ("\nCannot open second file: %s\n",fileName);
    		exit(0);
    	}
    	return (handleName);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main ()
    The correct definition of main is:
    Code:
    int main ( void )
    {
      return 0;
    }
    >openFile ("enter filename: ", "myHandleName");
    I'm thinking you want something more like this:
    Code:
    FILE *myHandleName = openFile("enter filename: ");
    >scanf ("%s",fileName);
    What if the name has spaces? fgets would be better here

    >handleName=fopen (fileName,"rt");
    fopen returns a FILE*, not a char*. Also, t is not a standard mode specifier for fopen.

    >exit(0);
    Do you really want to terminate the program just because a file can't be opened? What if the calling code can recover? If this function is meant to be generic, the last thing a library function should do is close the program. In fact, a good library function should refrain from taking user input (so that it can be used non-interactively) or logging errors (which is the job of the calling code). Really the best you can do is return a null pointer on error, which is basically what fopen does. So such a function would be a pointless wrapper in the first place.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Thanks, but...

    Thanks, Prelude for your reply, but I'm not sure you have understand me (or maybe I'm looking for something which is impossible)...
    In the main section, I would like (in 1 line) to call a function which will ask from the user the filename he wants to open and return the handle of this action so that it will be usable in the main section. I don't want to have scanf's in the main. I want a "clean" main

    Is it possible at all to specify (by calling the function) the handle name I would like to use?

    Thanks, again...

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    In C, the handle is represented as a FILE *. char * are not file handles.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE* openFile (char* text, char* handleName);
    		
    void main ()
    {
    	FILE * myHandle;
    	myHandle = openFile ("enter filename: ");
    
    }
    
    FILE* openFile (char* text)
    {
    	FILE * handle;
    	char fileName[25];
    	printf ("%s", text);
    	scanf ("%s",fileName);
    	handle=fopen (fileName,"rt"); //opening second file for reading.
    	if (handle==NULL)
    	{
    		printf ("\nCannot open second file: %s\n",fileName);
    		exit(0);
    	}
    	return (handle);
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main ()
    OMG OMG - 500 posts and still void main'ing

    Doomed I tell you, we're all DOOMed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Thumbs up Thanks!!!!

    Thanks, QuestionC and Thanks, Salem! I promise I'll not void main again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. help in c++ program
    By halloula in forum C++ Programming
    Replies: 21
    Last Post: 06-01-2006, 06:27 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM