Hi all,
In the following code i am trying to give the user the option to print to a chosen text file if they choose to. I have been reading up on pointers and file io and am still lost.
Any help offered would be greatly appreciated.
thanks in advance.
Mike
Code:#include <stdio.h> #include <string.h> void mysort(char word[][100], int size); int i, a, x=0, j=0; void main() { char word[10][100]; printf("this program will sort up to 10 words in alphabetical order\n"); printf("if you have fewer than 10 words enter <ctrl> z twice\n\n"); for(a=0; a<10; a++) { printf("enter a word\n"); if(scanf("%s", word[x]) != 1) break; x=x+1; j=x; } x=0; printf("original words\n--------------\n"); for (i=0; i<j; i++) { printf("%s \n", word[i]); } mysort (word, j); printf("\nsorted words\n------------\n"); for (i = 0; i <j; i++) { printf("%s \n", word[i]); } } void mysort(char word[][100], int size) { int pass, indx; char hold[100]; for(pass = 0; pass < size - 1 ; pass++) { for(indx = 0; indx < size - pass - 1; indx++) { if(strcmp(word[indx], word[indx + 1]) >0) { strcpy(hold, word[indx]); strcpy(word[indx], word[indx +1]); strcpy(word[indx + 1], hold); } } } }



LinkBack URL
About LinkBacks




You could fix that by checking the return value of fgets(): fgets() returns NULL if it encountered EOF or another error before it stored any characters in the array.