This program is supposed to make two passes through a file; the first pass counts the number of elements inside the file to get the total size; the second pass will fill the array and sort it. The problem i'm having is that on the second pass, the array is filled with garbage. I tried to use rewind(), but my program crashes :eek: Here is the code:

/*
1. Get array dimensions and print array.
2. Create array based on total.
3. Fill AND sort array.
4. Print sorted array.
*/

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

#define FILENAME	"data.txt"
#define MODE		"r"

int get_dim(FILE *);
int *make_array(int );
void fill_array(FILE *, int *, int );
void insertion(int *, int );

int main(void) {

	FILE *fp = fopen(FILENAME, MODE);

	int total = get_dim(fp);
	int *array = make_array(total);
	fill_array(fp, array, total);

	return 0;
}

int get_dim(FILE *fp) {

	int hold;

	printf("*** File Contents ***\n\n");
	for (int cnt = 0; fscanf(fp, "%d", &hold) != EOF; cnt++) {
		printf("%3d ", hold);
		if (cnt == 0) 
			continue;
		if (cnt % 10 == 0)
			printf("\n");	
	}
	
	printf("\nTotal number of items: %d\n", cnt);

	return cnt;
}

int *make_array(int total) {
	
	int *array = (int *)malloc(sizeof(int) * total);
	
	return array;
}

void fill_array(FILE *fp, int array[], int total) {

	for (int cnt = 0; cnt < total; cnt++) {
		if (fscanf(fp, "%d", array[cnt]) == 1)
			insertion(array, cnt);
	}

	for (cnt = 0; cnt < total; cnt++) {
		printf("%3d", array[cnt]);
		if (cnt == 0)
			continue;
		if (cnt % 10 == 0) 
			printf("\n");
	}
}

void insertion(int list[], int last) {
	 
	int walk;
	int temp;
	bool located;

	for (int current = 1; current <= last; current++)
	{
		located = false;
		temp = list[current];
		for (walk = current - 1; walk >= 0 && !located;)
			if (temp < list[walk])
			{
				list[walk + 1] = list[walk];
				walk--;
			}
			else
				located = true;
		list[walk + 1] = temp;
	}
	
	return;
}

Recommended Answers

All 5 Replies

MSVC6? cnt is udefined in several places using standard C++. Yet is seems to want to be C...

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

Except for the malloc casting:

int *array = (int *)malloc(sizeof(int) * total);

I don't suppose you could post "data.txt" and specify which language you are using (God forbit the bastard C/C++!).

Yes, this program is written in C.

data.txt:

838 758 113 515 51 627 10 419 212 86
749 767 84 60 225 543 89 183 137 566
966 978 495 311 367 54 31 145 882 736
524 505 394 102 851 67 754 653 561 96
628 188 85 143 967 406 165 403 562 834
353 920 444 803 962 318 422 327 457 945
479 983 751 894 670 259 248 757 629 306
606 990 738 516 414 626 116 825 181 134
343 22 233 536 760 979 71 201 336 61

Oh and for the malloc casting, my compiler complains if I don't typecast the malloc :rolleyes:

Yes, this program is written in C.

[...]

Oh and for the malloc casting, my compiler complains if I don't typecast the malloc :rolleyes:

Thanks, and that's because you're compiling as C++.

Minor issues aside, I think this is a major one:

if ( fscanf(fp, "%d", &array[cnt]) == 1 )

[edit]Here's my latest version:

/*
1. Get array dimensions and print array.
2. Create array based on total.
3. Fill AND sort array.
4. Print sorted array.
*/

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

int get_dim(FILE *file)
{
   int hold, i;
   printf("*** File Contents ***\n");
   for ( i = 0; fscanf(file, "%d", &hold) == 1; i++ )
   {
      printf("%3d ", hold);
      if ( i % 10 == 9 )
         printf("\n");
   }
   printf("Total number of items: %d\n", i);
   rewind(file);
   return i;
}

void insertion(int list[], int last)
{
   int walk, temp, located, current;

   for ( current = 1; current <= last; current++ )
   {
      located = 0;
      temp = list[current];
      for ( walk = current - 1; walk >= 0 && !located; )
      {
         if ( temp < list[walk] )
         {
            list[walk + 1] = list[walk];
            walk--;
         }
         else
         {
            located = 1;
         }
      }
      list[walk + 1] = temp;
   }

   return;
}

void fill_array(FILE *file, int array[], int total)
{
   int i;
   for ( i = 0; i < total; i++ )
   {
      if ( fscanf(file, "%d", &array[i]) == 1 )
      {
         insertion(array, i);
      }
   }
   for ( i = 0; i < total; i++ )
   {
      printf("%3d ", array[i]);
      if ( i % 10 == 9 )
      {
         printf("\n");
      }
   }
}

int main(void)
{
   FILE *file = fopen("data.txt", "r");
   if ( file )
   {
      int total = get_dim(file);
      int *array = malloc(total * sizeof *array);
      if ( array )
      {
         fill_array(file, array, total);
      }
      fclose(file);
   }
   return 0;
}

/* my output
*** File Contents ***
838 758 113 515  51 627  10 419 212  86 
749 767  84  60 225 543  89 183 137 566 
966 978 495 311 367  54  31 145 882 736 
524 505 394 102 851  67 754 653 561  96 
628 188  85 143 967 406 165 403 562 834 
353 920 444 803 962 318 422 327 457 945 
479 983 751 894 670 259 248 757 629 306 
606 990 738 516 414 626 116 825 181 134 
343  22 233 536 760 979  71 201 336  61 
Total number of items: 90
 10  22  31  51  54  60  61  67  71  84 
 85  86  89  96 102 113 116 134 137 143 
145 165 181 183 188 201 212 225 233 248 
259 306 311 318 327 336 343 353 367 394 
403 406 414 419 422 444 457 479 495 505 
515 516 524 536 543 561 562 566 606 626 
627 628 629 653 670 736 738 749 751 754 
757 758 760 767 803 825 834 838 851 882 
894 920 945 962 966 967 978 979 983 990 
*/

:o

i made that fix but somehow i'm still getting garbage. <o/

if ( i % 10 == 9 )

wow, i didn't even think of that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.