FILE Handling

This is a discussion on FILE Handling within the C Programming forums, part of the General Programming Boards category; Hi, Hopefully a quick one. I have recently been doing some C Programming again after not doing any for 4-5years. ...

  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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    978
    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
    22,166
    What is the code for showMenu()?

    Also, you should avoid the use of global variables.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,832
    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,185
    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,185
    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, 12:43 PM
  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, 04:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04: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

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