0

This is the code why when I show in output the string I have all words but with in the final row a strange symbol , an ASCII random symbol...

My objective is to save in a string all words to operate with it.

For example I have this document:

Mario


Paul


Tyler

How can i save all words in a string??

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int l,i=0,j=0,parole=0;
    char A[10][10];
    char leggiparola;
    char testo[500];
    FILE*fp;
    fp=fopen("parole.txt","r");
    if(fp!=NULL)
    {
        while(!feof(fp))
        {
            fscanf(fp,"%c",&leggiparola);
            printf("%c", leggiparola);
            testo[j]=leggiparola;
            j++;
        }  
    }
    fclose(fp);
    printf("%s",testo);
    return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
Paky100
  • 13
  • 2
  • 5
    Perhaps because you are using `feof` incorrectly. Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Better to test the return value from `fscanf` provided for your assurance that it did scan what you intended. – Weather Vane Jan 04 '17 at 13:57
  • 1
    Note that you should not call `fclose()` when you fail to open the file. Since you make no use of the command line arguments, it would be clearer to write `int main(void)`. – Jonathan Leffler Jan 04 '17 at 14:31

3 Answers3

1

Besides while(!feof(fp)) being "always wrong" you miss to 0-terminate the result string.

To do so place a

testo[j] = '\0'

just after the while-loop.

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
1

Instead of using fscanf, try with getc:

int leggiparola; /* This need to be an int to also be able to hold another 
                    unique value for EOF besides 256 different char values. */

...

while ( (leggiparola = getc(fp)) != EOF)
{
   printf("%c",leggiparola);
   testo[j++] = leggiparola;
   if (j==sizeof(testo)-1)
       break;
 }
 testo[j] = 0;
alk
  • 69,737
  • 10
  • 105
  • 255
dems98
  • 814
  • 3
  • 9
  • 22
  • 1
    `leggiparola` needs to be typed `int` (which isn't the case as per the OP's code) for your proposal to work. – alk Jan 04 '17 at 14:38
  • `getc` works with `char` so there should be no problem. – dems98 Jan 04 '17 at 16:50
  • There will be a problem the moment `getc()` returns `EOF`. Recommended further reading: http://stackoverflow.com/questions/7119470/int-c-getchar – alk Jan 04 '17 at 16:51
  • I didn't know that. Sometimes i used `getc()` with `char` and i never had problems. Anyway thanks for the advice. – dems98 Jan 04 '17 at 17:02
0

Here's fslurp. I't a bit messy due to the need to grow the buffer manually.

/*
  load a text file into memory

*/
char *fslurp(FILE *fp)
{
  char *answer;
  char *temp;
  int buffsize = 1024;
  int i = 0;
  int ch;

  answer = malloc(1024);
  if(!answer)
    return 0;
  while( (ch = fgetc(fp)) != EOF )
  {
    if(i == buffsize-2)
    {
      if(buffsize > INT_MAX - 100 - buffsize/10)
      {
          free(answer);
          return 0;
      }
      buffsize = buffsize + 100 * buffsize/10;
      temp = realloc(answer, buffsize);
      if(temp == 0)
      {
        free(answer);
        return 0;
      }
      answer = temp;
    }
    answer[i++] = (char) ch;
  }
  answer[i++] = 0;

  temp = realloc(answer, i);
  if(temp)
    return temp;
  else
    return answer;
}
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18