1. ## Help with qsort

Hey everyone,

I'm a program newbie, I'm writing a code for sorting numbers from a input file,

basically, I ask user to type in the file name, open file, read it, pass to a array, then sort it

qsort(array, array_size, sizeof(int), (*compare)(const void *, const void *));

since the array_size may vary for different file, I can't just use
int array_size= some #

How would I write this? Thank you very much!

2. Well presumably you had a count which you incremented when reading the file.

So use that count for the array_size when you call qsort()

3. hmm.. I don't know how to make count increment as I read the file.

If the program read a list of number and store to a char array called "instr"

Can I do : int count = sizeof(instr)/sizeof(char);

Thanks for helping me. This is my first time learning programing.

4. Post more code, so we can see what you're doing.

5. This is the code I have so far, took me along time to learn from googling. so basically, I open the file, read it, pass to a array, then sort it.

It's long, so thanks for reading it.

Code:
```#include <stdlib.h>
#include <stdio.h>
int main()
{
/*Promot user to enter file name*/
char infile_name[20];
printf("File:");
scanf("%s", infile_name);
printf("\n");

/* Open the file */
FILE *infile = fopen(infile_name, "r");
if (!infile)
{
/* If the operation was unsuccessful */
printf("unable to open %s\n", infile_name);
exit(1);

}

int in;
char instr;
do
{
fscanf(infile, "%d", &in); /* read from infile into int variable in */
if (!feof(infile))  /* only process in if didn't reach end of file */
{
instr=in; /* pass In to a array*/
}
} while(!feof(infile));
fclose(infile);

/* Sorting */
int compare (const void *s, const void *t)
{
return (*(const int*)s - *(const int*)t);
}
int count =sizeof(instr)/sizeof(char);
qsort(instr, count, sizeof(int), (*compare)(const void *, const void *));```

6. Well you need something like this for reading ints from a file.
Code:
```int array[10];
int count = 0;
while ( count < 10 &&                             // room in the array
fscanf(infile, "%d", &array[count]) == 1  // reading successful
) {
count++;
}```

7. Originally Posted by Salem
Well you need something like this for reading ints from a file.
Code:
```int array[10];
int count = 0;
while ( count < 10 &&                             // room in the array
fscanf(infile, "%d", &array[count]) == 1  // reading successful
) {
count++;
}```
My assignment kinda asked me to use feof to check, basically fscanf just like scanf but takes a extra (first) parameter, maybe the input file only has 1 number, so that's why I have if (!feof(infile)) to check if it's at the end of file.

Code:
```
int count = 0;
if (!feof(infile))  /*if didn't reach end of file*/
{
count++;
}```
But I think I'm actually discovered a problem:
I have
Code:
```    /* Read the file */
int in;
char instr;
do
{
fscanf(infile, "%d", &in); /* read from infile into int variable in */
if (!feof(infile))  /* only process  if didn't reach end of file */
{
instr=in; /* process in */
}
} while(!feof(infile));
fclose(infile);```
I think with instr=in; It will just replace the first number passed from in, how do I make a loop so it won't replace?

8. You need to put the value you read, into an array. I don't see anywhere where you set up an array, so step 1 would be "declare an array".

9. Originally Posted by muqing
My assignment kinda asked me to use feof to check,
so use it just to check!!!

Code:
```int array[10];
int count = 0;
while ( count < 10 &&                             // room in the array
fscanf(infile, "%d", &array[count]) == 1  // reading successful
) {
count++;
}

if(feof(infile))
{
puts("End of file reached");
}
else
{
puts("Something could be left in the file");
}```

10. Originally Posted by tabstop
You need to put the value you read, into an array. I don't see anywhere where you set up an array, so step 1 would be "declare an array".
so If I set a char array, what shoud i use to pass the vaule stored to "in" to that char array?

11. You shouldn't pass the values into a char array, since you are not reading chars.

12. sorry, my mistake there.

so If I set up a int array,

and I use fscanf(infile, "%d", &in); to store into interger variable "in".

If The file didn't reach to the end if (!feof(infile))

how should I process the variable "in"?

I assme I just need to pass "in" to a array, how should I do that?

13. By assigning the newly-read value to the next open spot in the array (which is what your counter will tell you).

14. Thanks for helping me tabstop
since the array size if determined by unknown input file, can I do something like this:

Code:
```int i, in;

int[] array;
do
{
fscanf(infile, "%d", &in); /* read from infile into int variable in */
if (!feof(infile))  /* only process in if didn't reach end of file */
{
for (i=1; i<=sizeof(in), i++)
int array[i]=in; /* process in */
}
} while(!feof(infile));
fclose(infile);```

15. Nope. If you don't know a limit ahead of time, then you'll have to use a "dynamic array", i.e., a big chunk of memory that you get from malloc or calloc or realloc.