Thread: problems accessing an external file

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    problems accessing an external file

    My linked list final seems to have been going well except accessing a file. The syntax looks correct to me, so I was hoping that maybe someone could spot something that I missed. Help is very appreciated. Here are the functions that
    access the file.
    Code:
    void MainMenu(char *data)
    {
    	int choice;
    	LINK_LIST * databases;
    	DBASE dbase;
    	databases = ReadMe("C:\\data.txt");
    	do
    	{
    		DisplayMenu();
    		fflush(stdin);
    		choice = getchar();
    		switch(choice)
    		{
    			case ADD     :	GetData(&dbase);
    						    databases = Add(databases, dbase);
    						    break;
    
    			case MODIFY  :	databases = Modify(databases);
    							break;
    
    			case DELETE  :	databases = Delete(databases);
    							break;
    
    			case SEARCH  :	Search(databases);
    							break;
    
    			case EXIT    :	break;
    
    			default      :	MenuError();
    		};
    	}while(choice != EXIT);
    	WriteMe("C:\\data.txt", databases);
    }
    
    void WriteMe(char *data, LINK_LIST * databases)
    {
    	int written = 0;
    	DBASE dbase;
    	FILE *fp;
    	fp = fopen("C:\\data.txt", "wt");
    	while(databases)
    	{
    		dbase = databases->dbase;
    		fwrite(&dbase, sizeof(DBASE), 1, fp);
    		written++;
    		databases = databases->next;
    	}
    	fclose(fp);
    	printf("\n%d record(s) written\n", written);
    }
    
    LINK_LIST * ReadMe(char *data)
    {
    	int read = 0;
    	LINK_LIST * databases;
    	DBASE dbase;
    	FILE *fp;
    
    	databases = NULL;
    
    	if((fp = fopen("C:\\data.txt", "rt")) != NULL)
    	{
    		while(fread(&dbase, sizeof(DBASE), 1, fp) == SUCCESS) 
    		{
    			read++;
    			databases = Add(databases, dbase);
    		}
    	}
    	fclose(fp);
    	printf("\n%d record(s) read\n", read);
    	return(databases);
    }

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    What is it doing that you don't expect? The first problem I stopped reading at is that you call fclose even if the file fails to open.
    When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kip Neuhart
    What is it doing that you don't expect? The first problem I stopped reading at is that you call fclose even if the file fails to open.
    I stopped reading at:
    Code:
    DisplayMenu();
    fflush(stdin);
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM