예제 #1
0
int main(void) {
  char * testNames[NUM_TESTS] = {"input.txt",
				 "anotherTestFileName.txt",
				 "somethingelse"};
  
  for (int i = 0; i < NUM_TESTS; i++) {
    char * outName = computeOutputFileName(testNames[i]);
    printf("'%s' => '%s'\n", testNames[i], outName);
    free(outName);
  }
  return EXIT_SUCCESS;
}
예제 #2
0
파일: main.c 프로젝트: jvm3487/C
int main(int argc, char ** argv) {
  //WRITE ME (plus add appropriate error checking!)
  if (argc == 1){
    fprintf(stderr, "Please input a valid File!\n");
    exit (EXIT_FAILURE);
  }
  FILE * file1 = fopen(argv[1], "r");
  if (file1 == NULL){
      fprintf(stderr, "File does not exist!\n");
      exit (EXIT_FAILURE);
  }
  if (fclose(file1) != 0){
    fprintf(stderr, "Failed to close file!\n");
    exit (EXIT_FAILURE);
  }
//read the key/value pairs from the file named by argv[1] (call the result kv)
  kvarray_t * kvs = readKVs(argv[1]);
 //count from 2 to argc (call the number you count i)
  for (int i = 2; i < argc; i++) {
    //count the values that appear in the file named by argv[i], using kv as the key/value pair
    //   (call this result c)
    FILE * file2 = fopen(argv[i], "r");
    if (file2 == NULL){
      fprintf(stderr, "File does not exist!\n");
      exit (EXIT_FAILURE);
    }
    if (fclose(file2) != 0){
      fprintf(stderr, "Failed to close file!\n");
    }
    counts_t * c = countFile(argv[i], kvs);
    //compute the output file name from argv[i] (call this outName)
    char * outName = computeOutputFileName(argv[i]);

    //open the file named by outName (call that f)
    FILE * f = fopen(outName,"w");
    //print the counts from c into the FILE f
    printCounts(c,f);
    //close f
    fclose(f);
    //free the memory for outName and c
    free(outName);
    freeCounts(c);
  }
 //free the memory for kv
  freeKVs(kvs);
  return EXIT_SUCCESS;
}
예제 #3
0
int main(int argc, char ** argv) {
  int i;
  if(argc<=2)
    {
      fprintf(stderr,"The no. of inputs are less");
      return EXIT_FAILURE;
    }
  kvarray_t *kv=readKVs(argv[1]);
  if(kv->length==-1)
    {
      printf("Couldn't open file!");
      return EXIT_FAILURE;
    }
  if(kv->length==-2)
    {
      printf("Couldn't close file");
      return EXIT_FAILURE;
    }

  //WRITE ME (plus add appropriate error checking!)
 //read the key/value pairs from the file named by argv[1] (call the result kv)
  for(i=2;i<argc;i++) {
    counts_t *c=countFile(argv[i],kv); //count the values that appear in the file named by argv[i], using kv as the key/value pair
    //   (call this result c)
    char *outName = computeOutputFileName(argv[i]);//compute the output file name from argv[i] (call this outName)
    FILE *f = fopen(outName,"w");//open the file named by outName (call that f)
    printCounts(c,f);//print the counts from c into the FILE f
    if(fclose(f)!=0)
      {
	fprintf(stderr,"Couldn't close file!");
	return EXIT_FAILURE;
      }//close f
    free(outName);
    freeCounts(c); //free the memory for outName and c
    }
  freeKVs(kv); //free the memory for kv
  return EXIT_SUCCESS;
}