多个文件程序

时间:2016-03-28 04:58:29

标签: c file-management

以下代码用于读取值并将其保存到数组中。只是当我构建并运行我的代码时,我的文件找不到。我基本上试图制作一个从文件中获取数据点并将其输入我的代码的程序。当它运行时,它表明虽然正确输入了文件名,但找不到该文件。

#include <stdio.h>
#include <stdlib.h>

//function prototype
void calc_results(int time[], int size);
void read_temps(int temp[]);

//set size of array as global value
#define SIZE 25

int main ()
{
   rand();
   //Declare temperature array with size 25 since we are going from 0 to 24
   int i, temp[SIZE];

   read_temps(temp);
   //Temperature for the day of October 14, 2015
   printf("Temperature conditions on October 14, 2015:\n");
   printf("\nTime of day\tTemperature in degrees F\n\n");

   for (i = 0; i < SIZE; i++)
      printf( "%d \t\t%d\n",i,temp[i]);
   //call the metod calc_results(temp, SIZE);
   calc_results(temp, SIZE);
   //pause the program output on console until user enters a key
   system ("PAUSE"); return 0;
}

/**The method read_temps that takes the input array temp
  and prompt user to enter the name of the input file
  "input.txt" and then reads the tempertures from the file
  for 24 hours of day */
void read_temps(int temp[])
{
   char fileName[50];
   int temperature;
   int counter=0;

   printf("Enter input file name : ");
   //prompt for file name
   scanf("%s",fileName);
   //open the input file
   FILE *fp=fopen(fileName, "r");
   //check if file exists or not
   if(!fp)
   {
      printf("File doesnot exist.\n");

      system ("PAUSE"); //if not exit, close the program
      exit(0);
   }
   //read temperatures from the file input.txt until end of file is encountered
   while(fscanf(fp,"%d",&temperature)!=EOF)
   {
      //store the values in the temp array
      temp[counter]=temperature;
      //incremnt the coutner by one
      counter++;
   }
   //close the input file stream fp
   fclose(fp);
}

void calc_results(int temp[], int size)
{
   int i, min, max, sum = 0;
   float avg;
   min = temp[0];
   max = temp[0];
   //Loop that calculates min,max, sum of array
   for (i = 0; i < size; i++)
   {
      if (temp[i] < min)
      {
         min = temp[i];
      }
      if (temp[i] > max)
      {
         max = temp[i];
      }
      sum = sum + temp[i];
   }
   avg = (float) sum / size;
   //open an external output file
   FILE *fout=fopen("output.txt","w");
   //Temperature for the day of October 14, 2015
   fprintf(fout,"Temperature conditions on October 14, 2015:\n");
   fprintf(fout,"\nTime of day\tTemperature in degrees F\n\n");
   //write time of day and temperature
   for (i = 0; i < SIZE; i++)
   {
      fprintf( fout,"%d \t\t%d\n",i,temp[i]);
   }
   printf("\nMin Temperature for the day is : %d\n", min);
   printf("Max Temperature for the day is :%d\n", max);
   printf("Average Temperature for the day is : %f\n", avg);
   //write min ,max and avg to the file "output.txt"
   fprintf(fout,"\nMin Temperature for the day is : %d\n", min);
   fprintf(fout,"Max Temperature for the day is :%d\n", max);
   fprintf(fout,"Average Temperature for the day is : %f\n", avg);
   //close the output file stream
   fclose(fout);
}

0 个答案:

没有答案