0
int main(void)
{
    FILE* cfPtr;

    fopen_s(&cfPtr, "clients.txt", "w");
    if (cfPtr == NULL) {
        puts("File could not be opened.");
    }
    else {
        puts("Enter the account ID, name, and phone number.");
        puts("Enter EOF to end input.");
        printf("%s", "> ");

        char id[10];
        char name[30];
        char phone[16];

        scanf_s("%9s%29s%15s", id, 10, name, 30, phone, 16);

        while (!feof(stdin)) {
            fprintf(cfPtr, "%s %s %s\n", id, name, phone);
            printf("%s", "> ");
            scanf_s("%9s%29s%15s", id, 10, name, 30, phone, 16);
        }

        fclose(cfPtr);
    }
}

I'm trying to print three strings into a file. The code above works fine for strings id and name but for the phone it just prints 0 in the file and for some reason the format is incorrect, it writes phone before name. When it should've been: id name phone

Kareem
  • 1
  • 2
  • Are you redirecting input to a file? I'll bet the file has CRLF line endings. Fix it with `dos2unix`. – Barmar Dec 10 '21 at 23:56
  • See [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2410359). Post sample input and output seen. Report the return value of `scanf_s("%9s%29s%15s", id, 10, name, 30, phone, 16);`. – chux - Reinstate Monica Dec 11 '21 at 02:26
  • `scanf_s("%9s%29s%15s", id, 10, name, 30, phone, 16);` is no more "secure" or "safe" than `scanf("%9s%29s%15s", id, name, phone );`. And `scanf()` is actually portable and doesn't [vendor-lock](https://en.wikipedia.org/wiki/Vendor_lock-in) your code to Microsoft. The warnings Microsoft spews at you are, for lack of a better word, not true. [Likely even deliberately not true](https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish). – Andrew Henle Dec 11 '21 at 14:59

0 Answers0