Thread: FILE Handling

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    FILE Handling

    Hi,

    Hopefully a quick one.
    I have recently been doing some C Programming again after not doing any for 4-5years.

    So, I have gone a bit all out to make a program that does a little bit of everything to refresh my memory.
    The function I have issues with is one that closes an open file. It causes everything to crash out.

    Function wise I have, menu, openfile, closefile, confirmfile.
    I have made it so if I run the program with the argument stuff.txt (as an example) it passes stuff.txt into confirmfile.
    This function fopen's the file and points FILE *fp at the file.
    FILE *fp is declared at the top and so should be global.
    I then use the closefile function to fclose fp.

    When I leave the confirmfile function will C automatically close that file, even if I dont declare it?
    The error is a double free or corruption error.

    Regards,
    Steve

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Please post some code

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Code:
    FILE *fp; //This is declared at the very top of program. outside of any functions
    
    int main(int argc, char *argv[])
    {
        int x;
    
    	if(argc == 1) //if only 1 entry in array then go to menu
    	{
    	showMenu(0);
    	}
    	else //otherwise a filename or argument was passed in, so try to open
    	{
    	fn[0] = argv[1];
    	confirmFile(fn); //pass filename to function confirmFile
    	}
    return 0;
    }
    
    int confirmFile(char *fn[])
    {
    
    fp=fopen(fn[0],"w"); //open File, point fp to the filename
    showMenu(1); //run menu function, passing 1 to signify that a file was opened on load
    }
    
    //I wont show the menu as its by the by, the below is the closeFile function which follows
    // on to directly close the file when called.
    int closeFile()
    {
    	//printf("%s\n", fn[0]);
    	//printf("%d\n", *fn[0]);
    	//printf("%f\n",fp);
    	fclose(fp);
    	return(0);
    }
    I think that's the important parts covered, am I making a simple error here?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the code for showMenu()?

    Also, you should avoid the use of global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Code:
    void showMenu(int x)
    {
    	int option; //set variable for menu choice
    	char rtn;  //clear screen
    	system("clear");
    
    	printf("###       2 - Close File               ###\n");
    
    	printf("What would you like to do?                \n");
    	scanf("%d%c",&option,&rtn);
    
    while (option !=4)
    {
    	switch(option)
    	{
    		case 1:
    		printf("\nOpen File\n");
    		break;
    
    		case 2:
    		closeFile();
    		break;
    		
    		case 3:
    		printf("\nPointer Menu\n");
    		break;
    
    		default:
    		printf("Error: I am sorry, that option doesn't exist - Please try again\n");
    	}
    }
    }
    My thoughts were that if I didnt set that pointer as a global then I wouldnt be able to use it easily between functions. Or should I be passing the pointer through the functions instead?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I assume fn is decrclaed as char *fn[1] or greater?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be passing the FILE pointer to (only) the functions that need it.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Code:
    void showMenu(int x);
    int confirmFile(char *fn[]);
    int closeFile();
    
    char *fn[0];
    char *file[0];
    Sorry, poor effort on the code pasting! This is my prototyping and variable declarations.
    since I only want to deal with one filename I kept fn to a size 0

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you only want one filename then (a) why make it an array and (b) if you do make it an array, then you need to specify that you want one entry:
    Code:
    char *file[1];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM