0

I was trying to find an answer for this in some internet pages, at the end I tried here and found an answer similar but it doesn't satisfy at all my question

Here answers how to write some text by writing it in a constant, but it doesn't allows you to whrite by console

Im so novice in programming world so I apologize if it's too ordinary my question. In addition, I apollogize about my english skills too.

Thanks in advance.

  #include "modificator.h"

    int main(void) {
        editFile();
        return 0;
    }

    void editFile() {
        FILE* f;
        Cadena cad, res;  //"Cadena is an array of char"

        printf("Write the access rout to file required: \n");
        scanf("%s", cad);

        f = fopen(cad, "w");

        if (f == NULL) {
            printf("Wrong opening file\n");
        }
        const char *text = scanf("%s", res);    
        fprintf(f, "Some text: %s\n", text);

        fclose(f);
    }
Community
  • 1
  • 1
Dave
  • 3
  • 3

1 Answers1

0
const char *text = scanf("%s", res);

No, no, no. scanf() returns a value of int type

int chk = scanf("%s", res); // beware buffer overflows
if (chk == 1) {
    fprintf(f, "Some text: %s\n", res);
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • That was a huge mistake. Thanks. Now the problems are different. It doesn't show the "printf" phrase and whrite no more than one word in spite of I give a char array of 256 spaces and get the message with the specificator "%s". – Dave May 31 '15 at 17:05
  • `scanf()` with `%s` ignores whitespace. You may be better off with `fgets()` – pmg May 31 '15 at 17:19
  • But I don't want to scan or get something from file, only write in whatever I place by console – Dave May 31 '15 at 17:46
  • `fgets()` can read from `stdin` ==> assuming `res` to be an array of char: `while (fgets(res, sizeof res, stdin)) { /* do your thing with res; you may want to remove the final ENTER in res */ }` – pmg May 31 '15 at 17:57
  • Now it never ends taking text, and it keeps not showing me printf at begin of code. Thanks for your help and time, Maybe another day I'll try it again – Dave May 31 '15 at 18:59