P
Pokerkook
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[]);
void Sortdata(int theString[], int n );
int main()
{
char theString[20000]="";
char filename[20];
int count;
int n=10;
int x[20000];
printf("\nPlease enter Filename: ");
gets(filename); /* user inputs filename */
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 )
{
int index = 0;
int x = 0;
int temp;
while ( x < (n - 1) )
{
index = x + 1;
while ( index < n )
{
if( String[ x ] > String[ index ] )
{
temp = String[ x ];
String[ x ] = String[index];
String[index] = temp;
}
index++;
}
x++;
}
}
void Outputdata(char filename[], char theString[])
{
FILE *fp;
char buf[BUFSIZ+1];
fp = fopen (filename, "r"); /* file is 10 integers on separate lines */
while (fgets(buf, BUFSIZ, fp) != NULL)
{
strcat(theString, buf);
}
printf("\n%s", theString);
fclose(fp);
}
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[]);
void Sortdata(int theString[], int n );
int main()
{
char theString[20000]="";
char filename[20];
int count;
int n=10;
int x[20000];
printf("\nPlease enter Filename: ");
gets(filename); /* user inputs filename */
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 )
{
int index = 0;
int x = 0;
int temp;
while ( x < (n - 1) )
{
index = x + 1;
while ( index < n )
{
if( String[ x ] > String[ index ] )
{
temp = String[ x ];
String[ x ] = String[index];
String[index] = temp;
}
index++;
}
x++;
}
}
void Outputdata(char filename[], char theString[])
{
FILE *fp;
char buf[BUFSIZ+1];
fp = fopen (filename, "r"); /* file is 10 integers on separate lines */
while (fgets(buf, BUFSIZ, fp) != NULL)
{
strcat(theString, buf);
}
printf("\n%s", theString);
fclose(fp);
}