How to write a string in a stream using physical files?

5

When you insert characters from a string into a text file into C, white space is ignored. How do I make long sentences within a string separate from words?

Follow the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int op = 10, k;
    char str[100], c, arquivo[20];
    FILE *fluxo;
    do{
        printf("Digite:\n1- Criar arquivo\n2- Inserir no arquivo\n3- Ler arquivo\n4- Fechar fluxo\n5- Remover arquivo");
        scanf("%d",&op);
        switch(op){
            case 1:
                printf("\nDigite o nome do arquivo: \n");
                scanf("%s",arquivo);
                fluxo = fopen(arquivo,"w");
                if(!fluxo)
                    printf("\n***Erro ao abrir/criar arquivo!***\n");
            break;
            case 2:
                printf("\nDigite o texto a ser gravado:\n");    
                scanf("%s",str);
                for (k=0;str[k];k++){
                    c = str[k];
                    putc(c, fluxo);
                }
                    //fprintf(fluxo, "%s",str);             
            break;
            case 3:
                fclose(fluxo);
                fluxo = fopen(arquivo, "r");
                while(!feof(fluxo)){
                    fscanf(fluxo,"%c", &c);
                    printf("%c",c);
                }
            break;
            case 4:
                fclose(fluxo);
            break;
            case 5:
                remove(arquivo);
            break;
        }
    }while(op!=0);
    fclose(fluxo);

}
    
asked by anonymous 17.02.2014 / 21:48

2 answers

4

According to the scanf function documentation:

  

Whitespace character: the function will read and ignore any whitespace   characters encountered before the next non-whitespace character   (whitespace characters include spaces, newline and tab characters -   see isspace).

Translating freely:

"White character: The function will read and ignore any white characters found before the next non-white character (white characters include space, new line, and tab)."

What happens then is that your reading is being "broken" in the spaces of the text. Assuming the test "This is a test.", The first reading will simply return "This", and the rest will be kept in the stream to return in the next readings "eh", "one" and "test." respectively.

I figured you could include a blank space between each reading, but @ C.E.Gesser well remembered that there is no way to tell if there are one or more spaces between words. So I think it's best to use fgets indicating stdin as the source for reading and the maximum data buffer size:

fgets(str, 100, stdin);
    
17.02.2014 / 22:28
2

The problem is that scanf("%s",str); reads the string and ignores the whitespace. A workaround for this is " %[^\n]" , instead of %s , which will read the entire line written by the user until you find a \n character, including spaces.

scanf(" %[^\n]", arquivo);

Or

scanf("%[^\n]%*c", arquivo);

This second appends a %*c to suppress ENTER that the user will give to confirm the end of the string (reference) .

In the test I did here with your code, I also changed the scanf("%d",&op); to scanf("%d%*c",&op); because ENTER after typing the index, swallowed the chosen options.

Note: Consider using fgets instead of scanf , if possible. When using scanf , you should be aware of some details to avoid overflow (as shown here ).

    
17.02.2014 / 22:29