error reading input from file

This is a discussion on error reading input from file within the C Programming forums, part of the General Programming Boards category; Ok, I have a text file that I am reading into buffers, and it breaks after around 107 lines of ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    51

    error reading input from file

    Ok, I have a text file that I am reading into buffers, and it breaks after around 107 lines of the file have been scanned. Any help would be greatly appreciated.


    Code:
    void ReadFile(struct SListHeader *ListInfo, FILE *pFLocation)
    {
    	int i;
    	char *acLineBuffer;
    	char acStateBuff[4], acCityBuff[41], acLatBuff[8], acLongBuff[8], acPopBuff[10], acZipBuff[8];
    
    	acLineBuffer = (char*)malloc(100 * sizeof(char));
    	if (acLineBuffer == NULL)
    	{
    		printf("\n\nOut of Memory!!!\n\n");
    		/* Insert Memory Error Check Here  */
    	}
    
    
    	while ( fgets(acLineBuffer, 99, pFLocation) )
    	{
    		for ( i=0; acLineBuffer[0] != ','; i++ )
    		{
    			acCityBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acCityBuff[i] = '\0';
    		acLineBuffer++;
    		printf("\n%s read from file.... ", acCityBuff);
    		printf("\n%s", acLineBuffer);
    			
    		for ( i=0; acLineBuffer[0] != ','; i++ )
    		{
    			acStateBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acStateBuff[i] = '\0';
    		acLineBuffer++;
    		printf("\n%s - ", acStateBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[0] != ','; i++ )
    		{
    			acZipBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acZipBuff[i] = '\0';
    		acLineBuffer++;
    		printf("\n%s - ", acZipBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[0] != ','; i++ )
    		{
    			acLatBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acLatBuff[i] = '\0';
    		acLineBuffer++;
    		printf("\n%s - ", acLatBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[0] != ','; i++ )
    		{
    			acLongBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acLongBuff[i] = '\0';
    		acLineBuffer++;
    		printf("\n%s - ", acLongBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[0] != '\n'; i++ )
    		{
    			acPopBuff[i] = acLineBuffer[0];
    			acLineBuffer++;
    		}
    		acPopBuff[i] = '\0';
    		printf("\n%s - ", acPopBuff);
    		printf("\n%s", acLineBuffer);
    
    		printf("\nReady to insert node.....");
                   }
    
                   return;
    }
    p.s. Sorry about all the extra printf's, it's just for debugging. (didn't help ;-)

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Basically, I created a variable "global" that would loop through the contents of acLineBuffer.

    BTW: you should NOT increment the NAME of your allocated buffer. Instead, you would assign a pointer to it (even though it is itself a pointer), and increment that. Anyway, you would be using different copy syntax for that...but FYI...




    Code:
    
    void ReadFile(struct SListHeader *ListInfo, FILE *pFLocation)
    {
    	int i, global = 0;
    	char *acLineBuffer;
    	char acStateBuff[4], acCityBuff[41], acLatBuff[8], acLongBuff[8], acPopBuff[10], acZipBuff[8];
    
    	acLineBuffer = (char*)malloc(100 * sizeof(char));
    	if (acLineBuffer == NULL)
    	{
    		printf("\n\nOut of Memory!!!\n\n");
    		/* Insert Memory Error Check Here  */
    	}
    
    
    	while ( fgets(acLineBuffer, 100, pFLocation) && global < 100)
    	{
    		for ( i=0; acLineBuffer[global] != ','; i++ )
    		{
    			acCityBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acCityBuff[i] = '\0';
    		//acLineBuffer++;
    		printf("\n%s read from file.... ", acCityBuff);
    		printf("\n%s", acLineBuffer);
    			
    		for ( i=0; acLineBuffer[global] != ','; i++ )
    		{
    		               acStateBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acStateBuff[i] = '\0';
    		//acLineBuffer++;
    		printf("\n%s - ", acStateBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[global] != ','; i++ )
    		{
    			acZipBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acZipBuff[i] = '\0';
    		//acLineBuffer++;
    		printf("\n%s - ", acZipBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[global] != ','; i++ )
    		{
    			acLatBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acLatBuff[i] = '\0';
    		//acLineBuffer++;
    		printf("\n%s - ", acLatBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[global] != ','; i++ )
    		{
    		               acLongBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acLongBuff[i] = '\0';
    		//acLineBuffer++;
    		printf("\n%s - ", acLongBuff);
    		printf("\n%s", acLineBuffer);
    			
    
    		for ( i=0; acLineBuffer[global] != '\n'; i++ )
    		{
    			acPopBuff[i] = acLineBuffer[global];
    			global++;
    		}
    		acPopBuff[i] = '\0';
    		printf("\n%s - ", acPopBuff);
    		printf("\n%s", acLineBuffer);
    
    		printf("\nReady to insert node.....");
                   }
    
                   return;
    }



    ITSA
    Socket Library!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    PS: free your memory before the function returns!



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    still not sure why the first method didn't work, but using a second count worked. Thank you very much!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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