Luis A.G.
Luis A.G.

Reputation: 1097

Add a space to a txt in C

I'm clearing a txt from plurals and empty words reading the txt with fscanf word by word. The problem is when I write the clean txt with fwrite, it writes all together.

I've tried to define a char and asignate only a space but when writting it also adds ' òwD'. Anyone knows how to add only ' ' ?

char espa[0];       
espa[0]=' ';
f2 = fopen("clean.txt", "w");   

       while(!feof(f))
       {
            char reader[100];
            int aux;
            fscanf (f, "%s", reader);

            if feof(f){
                printf("%s ", reader);
                printf("\n\nFin del fichero\n");
            }
            else
                if(cadena[(strlen(reader)-1)]=='s'){
                    for(aux=0;aux<(strlen(reader)-1);aux++){
                        printf("%c", reader[aux]);
                    }
                    fwrite(cadena, (strlen(reader))-1, 1, f2); //Add clean word
                }
                else{
                    printf(" %s ", reader);
                    fwrite(reader, (strlen(reader)), 1, f2); //Add normal word
                }

            fwrite(espa, (strlen(espa)), 1, f2); //Here I try to add the space
       }
       fclose(f);  
       fclose(f2);
}

Upvotes: 0

Views: 3006

Answers (1)

Joni
Joni

Reputation: 111239

char espa[0];

This is a character array with zero characters. You will want to store at least two characters: space and the terminating NUL character. But, to make things easier for you, you can have the compiler calculate the size of the array:

char espa[] = " ";

Upvotes: 5

Related Questions