help with output file!

This is a discussion on help with output file! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define MAX_PATIENT 100 #define HR_GIVEN 200 #define LEN 5 void getData(FILE* spData, int ID[], float ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help with output file!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_PATIENT 100
    #define HR_GIVEN    200
    #define LEN         5
    
    void getData(FILE* spData, int ID[], float aveofAveCommutingHR[], int dayexcers[],
    	int estMaxHR[], float ratioMaxHR[], float ratioCommuHR[]);
    void calData(int num, int age, int maxHR, int estMaxHR[], float ratioMaxHR[]);
    void printHeader();
    void printData( int ID[], float aveofAveCommutingHR[], int dayexcers[],
    	int estMaxHR[], float ratioMaxHR[], float ratioCommuHR[]);
    //void printData(int ID[]);
    
     int main (void)
     { 
    	 // global declarations
    	 FILE* spData;
    	 int ID[MAX_PATIENT];
    	 float aveofAveCommutingHR[MAX_PATIENT];
    	 int dayexcers[MAX_PATIENT];
    	 int estMaxHR[MAX_PATIENT];
    	 float ratioMaxHR[MAX_PATIENT];
    	 float ratioCommuHR[MAX_PATIENT];
    	 int num;
    
    
    
    //statement 
    if((spData=fopen("C:\\Users\\khoa\\Documents\\Visual Studio 2010\\Projects\\lab 1\\lab 1\\HR.txt","r"))== NULL)
    	 {
              printf("\n Error opennning file!\n");
              exit(100);
         }
    	else
    	{
    		printf("opened file! Data Processing\n");
    	}
    
    getData(spData, ID, aveofAveCommutingHR, dayexcers,estMaxHR, ratioMaxHR, ratioCommuHR);
    printHeader();
    printData( ID, aveofAveCommutingHR, dayexcers,estMaxHR, ratioMaxHR, ratioCommuHR);
    //printData(ID);
    	fclose(spData);
    
     system("pause");
     
     return 0;
     }
    
     /*==================================== getData ====================================
     Pre:
     Post:
     */
    
    void getData(FILE* spData, int ID[], float aveofAveCommutingHR[], int dayexcers[],
    	int estMaxHR[], float ratioMaxHR[], float ratioCommuHR[])
    {
    	int i = 0;
    	int j;
    	int size = LEN;
    	int highestMaxHR = 0;
    	int maxHR;
    	int age;
    	int maxCommuHR;
    	float aveCommuHR;
    	int counter = 0;
    	float excersHR;
    	int countave = 0;
    	int countdayexcers = 0;
    
    	while(i < MAX_PATIENT && (fscanf(spData,"%d %d %d", &ID[i], &maxHR, &age)) != EOF )
    	{
    		
    		
    		for(j = 0; j < LEN; j++)
    		{
    			fscanf(spData,"%f %f %f", &aveCommuHR, &maxCommuHR, &excersHR);
    		
    		
    		
    
    		while( aveCommuHR > 0)
    		{
            countave++;
    		aveCommuHR += aveCommuHR;
    		}
    		while(excersHR > 0)
    		{
    			countdayexcers++;
    		}
    
    		while(maxCommuHR > highestMaxHR)
    		{
    			highestMaxHR = maxCommuHR;
    		}
    
    		}
    		
    		aveofAveCommutingHR[i] = aveCommuHR / (float)countave;
            dayexcers[i]           = countdayexcers;
    		calData(i, age, maxHR, estMaxHR, ratioMaxHR );
            ratioCommuHR[i]        = (float)highestMaxHR / (float)estMaxHR[i] * 100;
    		calData(i, age, maxHR, estMaxHR, ratioMaxHR );
    
    		i++;
    		size += LEN;
    		highestMaxHR = 0;
    		countave = 0;
    	    countdayexcers = 0;
    		counter++;
    
    	}
    
    
    
    	return;
    }
    /*	==================== CalcData ====================
    Pre:
    Post:
    */
    
    void calData(int num, int age, int maxHR, int estMaxHR[], float ratioMaxHR[])
    {
    
    	estMaxHR[num]   = HR_GIVEN - age;
    	ratioMaxHR[num] = ( (float)maxHR / (float)estMaxHR[num] ) * 100 ;
    
    return;
    }
    
    
    
    /*	==================== printHeader ====================
    Pre:
    Post:
    */
    void printHeader()
    {
    //Local Declaration
    	FILE *spOUT;
    
    //Statements
    	if(!(spOUT = fopen("output.txt", "w")))
    		printf("Error writing output.txt");
    
    	fprintf(spOUT, "COMMUTING AND EXERCISE HEART RATE SUMMARY\n\n");
    	fprintf(spOUT, "SUBJECT   AVERAGE   DAYS      ESTIMATED     %%MEASURED    %%MAX\n");
    	fprintf(spOUT, "NUMBER    COMMUTING EXERCISED  MAX HR       MAX HR TO    COMMUTING\n");
    	fprintf(spOUT, "          HR                                ESTIMATED    HR TO\n");
    	fprintf(spOUT, "                                            HR           MEASURED\n");
    
    	fclose(spOUT);
    	return;
    }
    
    
    
    
    
    /*	==================== printData ====================
    Pre:
    Post:
    */
    
    void printData( int ID[], float aveofAveCommutingHR[], int dayexcers[],
    	int estMaxHR[], float ratioMaxHR[], float ratioCommuHR[])
    
    {
    	//local declarations
    	int i;
    	FILE* spOUT;
    	//statements
    	if(!(spOUT = fopen("output.txt", "a")))
    		printf("Error writing output.txt");
    	for(i = 0;i < MAX_PATIENT; i++)
    		fprintf(spOUT ,"%4d      %5.1f        %d         %3d          %5.1f       %5.1f\n",
    				   ID[i], aveofAveCommutingHR[i], dayexcers[i], estMaxHR[i], ratioMaxHR[i], ratioCommuHR[i]);
    
    	fclose(spOUT);
    
    
    
    return;
    }

    can anyone tell me why doesn't this code create an output file?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you can print the data to the screen, then check:

    1) if the disk is full
    2) whether you have write permission in that folder
    3) you are writing out the file, but it's not where you are looking for it

    Why not check whether output.txt file is being opened?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. output file is a unicode file under certain circumstances
    By rivkyfried1 in forum C Programming
    Replies: 4
    Last Post: 10-25-2010, 09:16 AM
  3. Replies: 3
    Last Post: 10-20-2006, 07:59 PM
  4. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 10:15 AM
  5. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:52 AM

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