Thread: Reading a File

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Reading a File

    Hi,

    I am having problems with this program. I am not gettting any errors but it just sits there and does nothing. It's a Product Inventory program. I am supposed to read an Input file and display it on the screen then write to an outfile. Here is my code, any help would be appreciated.

    Thank you,
    Jay

    Code:
    /*Libraries included*/
    # include <stdio.h>
    
    
    
    
    /*main Function*/
    int main()
    {
    	
    	FILE *inFile;
    	FILE *outFile;
    	int i;
    	char allIn[16] = {0};
    	char yearMonth[6] = {0};
    	int month;
    	char *monthWord[12];
    	int cdId;
    	int begBal;
    	int copRec;
    	int copSold;
    	int endBal;
    	int year;
    	
    
    
    	
    	
    	
    	
    	
    	/*Open Input File*/
    	if ((inFile = fopen("input.txt", "r")) == NULL)
    		{	
    			printf("\nFailed to open the data file named %s. \n", inFile);
    			exit(1);
    		}
    	
    
    	while (fgets(yearMonth,6,inFile) != NULL)
    	sscanf(yearMonth, "%4d%2d", &year, &month);	
    
    
    	for (i = 0; i <= 8; ++i)
    	{
    	
    		
    		while (fgets(allIn,16,inFile) != EOF)
    	
    		
    		sscanf(allIn,"%4d%4d%4d%4d",&cdId, &begBal, &copRec, &copSold);
    			
    		/*change month int to month name*/
    		if (month == 01)
    			monthWord[0] = "January";
    		else if (month == 02)
    			monthWord[1] = "February";
    		else if (month == 03)
    			monthWord[2] = "March";
    		else if (month == 04)
    			monthWord[3] = "April";
    		else if (month == 05)
    			monthWord[4] = "May";
    		else if (month == 06)
    			monthWord[5] = "Jume";
    		else if (month == 07)
    			monthWord[6] = "July";
    		else if (month == 8)
    			monthWord[7] = "August";
    		else if (month == 9)
    			monthWord[8] = "September";
    		else if (month == 10)
    			monthWord[9] = "October";
    		else if (month == 11)
    			monthWord[10] = "November";
    		else if (month == 12)
    			monthWord[11] = "December";
    		else
    			printf("That is not a month determiner\n");
    
    	
    		fclose(inFile);
    
    
    		/*Open the output file*/
    		if ((outFile = fopen("output.txt", "a")) == NULL)
    		{	
    			printf("\nFailed to open the data file named %s. \n", outFile);
    			exit(1);
    		}
    	
    	
    	
    
    
    
    
    		
    
    		fprintf(outFile,"\n\n			CD INVENTORY REPORT FOR %20s %4d\n\n", monthWord, year);
    		fprintf(outFile,"				Beginning			Copies							Ending\n");
    		fprintf(outFile,"CD ID			Balance				Received		Copies Sold		Balance\n");
    		fprintf(outFile,"___________________________________________________________________________\n");
    
    		/*loop to print Structure*/
    		for (i = 0; i <= 8; ++i)
    		printf("%4d			%4d				%4d				%4d				%4d\n", cdId, begBal, copRec, copSold, endBal = begBal + copRec - copSold);
    		fprintf(outFile,"%4d			%4d				%4d				%4d				%4d\n",cdId, begBal, copRec, copSold, endBal);
    
    
    	fclose(outFile);
    	
    
    		printf("			CD INVENTORY REPORT FOR %20s %4d\n\n", monthWord, year);
    		printf("				Beginning			Copies							Ending\n");
    		printf("CD ID			Balance				Received		Copies Sold		Balance\n");
    		printf("___________________________________________________________________________\n");
    	}
    
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should know that if you prefex a number with a zero, it is an octal number, not a base 10 number.

    So how far does your program get exactly?
    Code:
    void dump( FILE *fp )
    {
        int c;
        while ( (c=fgetc(fp)) != EOF )
            putchar( c );
        fflush( stdout);
        rewind( fp );
    }
    First, make sure your file actually has the data you expect.

    You should also break it down and test each function in turn to make sure it's behaving correctly...

    ...scratch that, you should actually use functions instead of having everything in main.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    Thanks,

    I need to rework this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM