Pokerkook wrote:
Hello,
If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:
49
49
10
49
52
10
I'm trying to sort integers from a text file. Sorry if my code is bad or way
off base. I am a newbie to the C Language & I am just learning Arrays &
pointers.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void Outputdata(char filename[], char theString[]);
This function should return a value which can be use
to determine whether or not the file was accessed.
void Sortdata(int theString[], int n );
int main()
{
char theString[20000]="";
using theString is a poor approach in parsing the file. char filename[20];
int count;
int n=10;
int x[20000];
Since the size of the array is unknown, why not dynamically
allocate space as you need it using function realloc?
You can define a data struct that will a member pointint to
the array and another member keeping a count. See the Example
below.
printf("\nPlease enter Filename: ");
gets(filename); /* user inputs filename */
Function gets very unsafe, use function fgets. Outputdata(filename, theString);
*x = atoi(theString);
Sortdata( x, n );
printf("\nAfter the sort, the contents are:\n");
for( count = 0; count < n; count++ )
printf("Count = %d = %.d\n", count, theString[ count ]);
}
void Sortdata( int String[], int n )
.........snip,,,,,,,,
You can use function qsort to sort the numbers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct INTARRAY
{
int *num;
size_t count;
} INTARRAY;
int GetDataINTARRAY(const char *filename, INTARRAY *p);
void SortINTARRAY(INTARRAY *p, int (*cmp)(const void *, const void *));
void PrintINTARRAY(INTARRAY *p);
void FreeINTARRAY(INTARRAY *p);
int cmp(const void *v1, const void *v2);
int main(void)
{
char filename[64],*s;
INTARRAY myint = {NULL};
printf("\nPlease enter Filename: ");
fflush(stdout);
fgets(filename, sizeof filename,stdin);
if(s = strrchr(filename,'\n')) *s = '\0';
else while('\n' != getchar());
if(GetDataINTARRAY(filename, &myint))
{
puts("\tUnSorted");
PrintINTARRAY(&myint);
SortINTARRAY(&myint,cmp);
puts("\n\tSorted");
PrintINTARRAY(&myint);
}
else puts("File not Found or Memory Allocation Failure");
FreeINTARRAY(&myint);
return 0;
}
void SortINTARRAY(INTARRAY *p, int (*cmp)(const void *, const void *))
{
if(p->num)
qsort(p->num,(size_t)p->count,sizeof *p->num,cmp);
return;
}
int GetDataINTARRAY(const char *fname, INTARRAY *p)
{
FILE *fp;
int ibuf, *tmp;
if((fp = fopen (fname, "r")) == NULL) return 0;
while(1 == fscanf(fp," %d",&ibuf))
{
tmp = realloc(p->num,(p->count+1)*(sizeof *tmp));
if(tmp == NULL)
{
FreeINTARRAY(p);
return 0;
}
p->num = tmp;
p->num[p->count++] = ibuf;
}
fclose(fp);
return 1;
}
int cmp(const void *v1, const void *v2)
{
const int *i1 = v1;
const int *i2 = v2;
return (*i1<*i2)?-1:(*i1!=*i2);
}
void PrintINTARRAY(INTARRAY *p)
{
size_t i;
for(i = 0; i < p->count; i++)
printf("num[%u] = %d\n",i,p->num[i]);
return;
}
void FreeINTARRAY(INTARRAY *p)
{
free(p->num);
p->num = NULL;
p->count = 0;
return;
}
--
Al Bowers
Tampa, Fl USA
mailto:
xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/