0

I am trying to read a file and print all of the words that are in the file, ignoring all other spaces and symbols. I have it working with strcpy but it's giving me an error and I'm trying to use sprintf but I don't really understand how that function words. It's printing random integers instead of the strings.

Edit: I'm completely new to C so I don't have my pointers down too well.

  FILE *file;
  file = fopen("sample_dict.txt", "r");
  int c;
  int wordcount = 0;
  int count = 0;
  const char *a[10];
  char word[100];
  do {
    c = fgetc(file);
    //error statement
    if (feof(file)) {
      break;
    }
    if (isalpha(c) && count == 2) {
      printf("%s\n", word);
      memset(word, 0, sizeof(word));
      count = 1;
      wordcount++;
    }

    if (isalpha(c)) {
      //strcat(word, &c);
      sprintf(word, "%d", c);
      continue;
    }
    count = 2;
    continue;
  } while (1);
  fclose(file);
  return (0);

  return 0;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Kot
  • 11
  • 4
  • You print `char` as integer when using `%d` you can print it as is like this: `sprintf(word, "%c", c);` but you reset the word in each character. better use a counter and copy into array... – SHR Oct 09 '14 at 23:54

2 Answers2

1

Use a %c for a format specifier in C if you want the character. if you use %d, it will work, but will display as integer.
The other thing is that if you want to use sprintf to concatenate a string with a char, or and int, you must include both in the argument list of sprintf:

change this:

sprintf(word, "%d", c);

To this:

char newString[20];//adjust length as necessary
sprintf(newString, "%s%c",word, c);  

Your logic here suggests that you only want to append the char, if it is an alpha [a-z,A-Z]

  if(isalpha(c))
  {
    //strcat(word, &c);
    sprintf(word, "%d", c);
    continue;
  }  

Change it to:

  if(isalpha(c))
  {
    //strcat(word, &c);
    char newString[20];//bigger if needed, 20 just for illustration here
    sprintf(newString, "%s%d", word, c);
    continue;
  }    
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I changed it to sprintf(word, "%s%c", word, c); and it worked. Thank you – Kot Oct 10 '14 at 00:02
  • 1
    `sprintf(word, "%s%d", word, c);` is UB. because sprintf 1st argument is `restrict` (Copy in the area overlapping area behavior undefined). – BLUEPIXY Oct 10 '14 at 09:32
0
#define IN 1
#define OUT 0

FILE *file;
file = fopen("sample_dict.txt","r");
int c;
int wordcount = 0;
int status = OUT;//int count = 0;
//const char *a[10];//unused
char word[100] = "";

do {
    c = fgetc(file);
    if(feof(file)){
        if(*word){//*word : word[0] != '\0'
            printf("%s\n", word);
        }
        break;
    }
    if(isalpha(c)){
        char onechar_string[2] = {0};//{c};
        onechar_string[0] = c;
        strcat(word, onechar_string);
        if(status == OUT){
            wordcount++;
        }
        status = IN;
    } else {
        if(status == IN){
            printf("%s\n", word);
            *word = 0;//memset(word,0,sizeof(word));
        }
        status = OUT;
    }
}while(1);
fclose(file);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70