Unable to write characters to a file using fputc in C

Jitendra Kumar Chauhan 1 Reputation point
2021-06-03T10:01:37.203+00:00

I wrote a C program to copy characters from one file to other in Vs 2019 but it is not working, please help me out. I am getting the error"D:\C PROGRAM\TEST\test1\x64\Debug\Test1.exe (process 15472) exited with code 3".
Here is the code--

include<stdio.h>

include<stdlib.h>

int main()
{
FILE* fp;
errno_t err;
err = fopen_s(&fp,"C:/Users/PRASHANT SONI/Desktop/fileopening.txt", "w");
fputc('H',fp);
fclose(fp);
return 0;
}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,612 questions
{count} votes

5 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2021-06-03T22:46:36.583+00:00

    Using the OP's code exactly as posted and building it
    as a C program, not C++, the debugger should complain
    about an assertion: stream != nullptr

    This occurs on the fputc because fp == NULL

    After choosing Abort from the choices offered, the
    program should exit and the Output window will show
    that the program exited with code 3.

    The interpretation I gave of that code is from:

    System Error Codes (0-499)
    https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

    If I change the user name from what the OP posted to
    my own user name and rebuild, the program then runs
    correctly and creates the file on my desktop, exiting
    with code 0 as expected.

    • Wayne
    1 person found this answer helpful.

  2. WayneAKing 4,921 Reputation points
    2021-06-03T17:35:32.407+00:00

    I am getting the error"...\Test1.exe (process 15472) exited with code 3".

    Exit code 3 means:

    ERROR_PATH_NOT_FOUND

    3 (0x3)

    The system cannot find the path specified.

    The path you have specified in the fopen is wrong in some way.

    If it were correct, the code you posted would create
    the file with the letter H in it.

    Note that fopen will not create a directory from the
    path specified. That path must already exist.

    Check your spelling, punctuation, etc. in the fopen line.

    • Wayne

  3. Sam of Simple Samples 5,521 Reputation points
    2021-06-03T21:58:35.087+00:00

    For me if fopen_s fails then fp is null and the debugger really complains when it gets to fputc.

    You can show the error message. The following produces Error: No such file or directory for me.

    FILE* fp;
    errno_t err;
    char buffer[80];
    err = fopen_s(&fp, "C:/Users/Nobody/Desktop/fileopening.txt", "w");
    if (err != 0) {
    strerror_s(buffer, 80, err);
    std::cout << "Error: " << buffer << "\n";
    return err;
    }
    fputc('H', fp);
    fclose(fp);

    And if Wayne is correct and it is a problem with the directory name then you should accept his answer but I do not see why you are getting 3 for a return code. If fopen_s returns 3 then the error is No such process.

    0 comments No comments

  4. RLWA32 42,476 Reputation points
    2021-06-03T22:41:54.79+00:00

    There are no statements in the posted code that would return 3 as the exit code for the process. However, if the fopen_s statement failed to open the file and fp was null then the call to fputc would cause a debug build to assert. If the OP responded to that assertion by pressing the abort button then the process would be terminated by the CRT with an exit code of 3.


  5. Jitendra Kumar Chauhan 26 Reputation points
    2021-06-05T13:52:33.403+00:00

    Hello everyone, the program is working properly when i deleted the file "fileopening.txt". The compiler is creating the file "fileopening.txt" by its own on the desktop. But the problem is arising only when i create a file by opening notepad and saving as "fileopening". It means fputc can create a file. Is it true or some other logic is there?
    Here is the same C program which i posted few days ago.

    include<stdio.h>

    include<stdlib.h>

    int main()
    {
    FILE* fp;
    errno_t err;
    err = fopen_s(&fp, "C:/Users/PRASHANT SONI/Desktop/fileopening.txt", "w");
    fputc('H', fp);
    fclose(fp);

    return 0;
    

    }