0

Unable to figure out this issue that is causing my program to throw this error, i'm reading from 3 files and have to make a dynamic array to store all the values of the text file(which are INTs). Then sort and print out to a new file.

This is my updated code

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

//Jeffrey Hennen

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

void main(){
     //printf("hello\n");
     int counter = 0;     

     FILE *fp;
     fp = fopen("list0.txt", "r");

     int ch;
     while((ch = fgetc(fp)) != EOF){
          //counter++;         
          if(ch == '\n'){
              counter++;
          }
     }
     //printf("%d\n", counter);
     //printf("hello1\n");

     fclose(fp);
     fp = fopen("list1.txt", "r");     

     while((ch = fgetc(fp)) != EOF){

          //counter++;        
          if(ch == '\n'){
              counter++;
          }
     }
     //printf("%d\n", counter);
     //printf("hello1\n");
     fclose(fp);
     fp = fopen("list2.txt", "r");

     while((ch = fgetc(fp)) != EOF){
          //counter++;          
          if(ch == '\n'){
              counter++;
          }
     }
     //printf("%d\n", counter);
     fclose(fp);
     //printf("hello2\n");
     int size = counter;
     int * sizeOfFile = (int*) malloc(size * sizeof(int));

     //int z = 0;
     //for (z = 1; z<= size; z++){
     //     printf("%d %d\n", z, sizeOfFile[z]);
     //}

     char ch1[sizeof(int)];
     int counter1 = 0;
     int y = 0;
     fp = fopen("list0.txt", "r");
     while ((ch = fgetc(fp)) != EOF){
          fgets(ch1, sizeof(int), fp);
          y = atoi(ch1);
          printf("%s\n", ch1);
          sizeOfFile[counter1] =  y;
          counter1++;
     }
     fclose(fp);

     fp = fopen("list1.txt", "r");
     while ((ch = fgetc(fp)) != EOF){
          fgets(ch1, sizeof(int), fp);
          y = atoi(ch1);
          printf("%s\n", ch1);
          sizeOfFile[counter1] =  y;
          counter1++;
     }   
     fclose(fp);  

     fp = fopen("list1.txt", "r");
     while ((ch = fgetc(fp)) != EOF){
          fgets(ch1, sizeof(int), fp);
          y = atoi(ch1);
          printf("%s\n", ch1);
          sizeOfFile[counter1] =  y;
          counter1++;
     }  
     fclose(fp);

     qsort(sizeOfFile, size, sizeof(int), cmpfunc);
     fp = fopen("Hw3.out", "w");  
     int x = 0;   
     for(x; x < size; x = x + 1){
          fprintf(fp, "%d\n", sizeOfFile[x]);         
     }
     fclose(fp);

     free(sizeOfFile);


}
  • `counter` is unintialized. – kaylum Feb 04 '17 at 03:54
  • Also see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – kaylum Feb 04 '17 at 03:55
  • Initialized the counter and same issue, i am getting a error after running. not compiling – Jeffrey Hennen Feb 04 '17 at 04:02
  • Huh? Who said anything about compiling. Accessing uninitialised values results in Undefined Behaviour at runtime. If you are on Linux suggest you use [valgrind](http://valgrind.org) to help you diagnose memory problems like this. It's unlikely anybody is going to debug your whole program for you so best to learn how to do that on your own. – kaylum Feb 04 '17 at 04:03
  • okay, so while(!feof(file)) will alway return a false and cause the while loop to not run? – Jeffrey Hennen Feb 04 '17 at 04:21
  • There are multiple issues in your code. Can only point out some of them and hope it helps you progress. You have a misunderstanding of how files are stored. You can't read from a file and directly interpret that as an `int`. It is not stored as `int` but as `char.` that is, if the file contains `10` what you get from read will be the two characters with value `31 `and `32` which corresponds to ascii `1` and `0` respectively. – kaylum Feb 04 '17 at 04:25
  • Okay, well how do i convert from char to int then? – Jeffrey Hennen Feb 04 '17 at 04:45
  • Surely you can find that in your C book or google that? There are many ways, simplest (but not best) is to use a standard function such as [atoi](https://linux.die.net/man/3/atoi) – kaylum Feb 04 '17 at 04:47
  • Thanks btw, I am just new to programming. especially in C – Jeffrey Hennen Feb 04 '17 at 04:49
  • I put an updated thing of code up there for what i have based on some of your ideas that you were saying. But right now i am getting a segfault with the added issue of only portions of my reads are actually getting displayed when i am testing before i convert. – Jeffrey Hennen Feb 04 '17 at 05:28

0 Answers0