Gosh I detest IE.
Anyways.
I'm trying to write a code that will allow me to open a file, or a text input, and then sort it alphabetically. I then want the program to be able to be stop/quit at anytime by using a scanf or something to read a key, or multiple key inputs at a time, such like windows reads Cntrl + alt + Del, I would like something similar, but reading func keys is beyond me.
I have NO idea how to transform chars into numbers, and vice versa. I tried surfing the net for help for a few hours, and I've seen bubble sort, and the like, but they come with the IOstream which doesn't seem to work anymore, so I'm mightily confused.
Anyways, Here is my code, I'll /* */ the sections I'm struggling in.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int readWord(FILE *in, char aWord[]);
FILE * openFile();
int main ()
{
printf("Would you like to read from a file, or input the text yourself? \n");
printf("Please specify: (f or i)");
scanf("" */ I want this to be the part where you specify wether or not you want to sort from a text file, or from your own input. The file read works perfectly though, I'm not quite sure how to implement the sorting algorithm in to it*/
{
char aWord[100];
FILE *in;
in = openFile();
while (readWord(in, aWord) != EOF)
{
printf("%s ", aWord);
}
printf("\n");
fclose(in);
return(0);
}
FILE * openFile()
{
FILE *in;
char filename[1000];
printf("Please enter the input file name including its path\n");
scanf("%s", filename);
if ((in = fopen (filename, "r")) == NULL)
{
fprintf(stderr, "Can't open the input file.\n");
printf("Program is terminated ...\n");
exit(1);
}
return in;
}
int readWord(FILE *in, char aWord[])
{
return fscanf(in, "%s", aWord);
}
{
/* Here is my main problem, getting the stuff to sort. I have NO idea how to transform chars into numbers, and vice versa. */
void sort(int mynum[], int size)
{
int pass, indx, hold;
for (pass = 0; pass < size - 1 ; pass++)
{
for (indx = 0; indx < size - pass - 1; indx++)
{
if ( mynum[indx] > mynum[indx + 1] )
{
hold = mynum[indx];
mynum[indx] = mynum[indx + 1];
mynum[indx + 1] = hold;
}
}
}
}
int main ()
{
int i, mynum[10], mycopy[10];
srand((unsigned)time(NULL));
for (i = 0; i < 10; i++)
{
mynum[i] = rand();
mycopy[i] = mynum[i];
}
sort(mynum, 10);
printf("original numbers sorted numbers\n");
for (i = 0; i < 10; i++)
{
printf("%10d %17d \n", mycopy[i], mynum[i]);
}
return(0);
}