question

JitendraKumarChauhan-8834 avatar image
0 Votes"
JitendraKumarChauhan-8834 asked WayneAKing-0228 commented

Unable to write characters to a file using fputc in C

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++
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you check if fopen_s succeeded (i.e. err is zero)?


0 Votes 0 ·

yes many times still i am getting the same error.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 JitendraKumarChauhan-8834 ·

When you execute the program in Visual Studio using <F10> key (step-by-step), do you observe any issue?


0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 JitendraKumarChauhan-8834 ·

Can you successfully create, edit and save "C:/Users/PRASHANT SONI/Desktop/fileopening.txt" using notepad?

0 Votes 0 ·
Show more comments
BarrySchwarz-8780 avatar image BarrySchwarz-8780 JitendraKumarChauhan-8834 ·

When your program terminates, does the output file exist? If so, what are the contents?

0 Votes 0 ·
Show more comments
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered SimpleSamples commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The specified path is correct, here is the screenshot of the file properties.102268-screenshot-file-details.png


Hello Wayne, can u please send me the C program for writing characters to the file "C:/Users/PRASHANT SONI/Desktop/fileopening.txt" which should work in VS2019 IDE. I searched it on websites and copied to the editor and did the necessary modification, still they are not working.
Here is the details of the file which has been created on my desktop.102248-screenshot-file-details.png


0 Votes 0 ·
WayneAKing-0228 avatar image WayneAKing-0228 JitendraKumarChauhan-1166 ·

Hello Wayne, can u please send me the C program for
writing characters to the file

You are not giving us enough information about exactly
what you are trying to do when the write fails, and
whether any error messages, popups, etc. occur before
the program exits with code 3.

As I said earlier, the code you posted will correctly
write to the file specified as long as the path is
correct. However, other conditions will cause the
attempt to open the file for writing to fail.

Some examples:

(1) The file is already opened in another program
that does not allow sharing of the file.

(2) The file is already opened for reading in the
same program that is trying to open it for writing.

(3) The file has the read-only attribute set.

(4) Security settings prevent this program from opening
a file for writing.

  • Wayne

0 Votes 0 ·

Also if the invalid path causes a return code of 3 for the application then the error from fopen_s would be 2 but you (JitendraKumarChauhan-1166) are saying it is zero.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered SimpleSamples commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That could be the explanation and if so then the JitendraKumarChauhan-8834 sure should have told us more.

0 Votes 0 ·
WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered SimpleSamples commented

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://docs.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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You might be correct but it does assume facts not in evidence. As I said, the question should have included the details about the debugger complaining. And even if it is compiled as C++ the debugger does complain about the stream being a nullptr.

1 Vote 1 ·

As a footnote, the interpretation of the error code 3
can also be found within the VS IDE itself by using
the Error Lookup tool from the IDE Tools menu:

102222-error-lookup.jpg

  • Wayne

0 Votes 0 ·
error-lookup.jpg (24.0 KiB)
JitendraKumarChauhan-1166 avatar image
0 Votes"
JitendraKumarChauhan-1166 answered WayneAKing-0228 commented

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;

}

· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If the code works when the file does not exist but fails when you first create the file with notepad, it would seem that something is different between the two environments.

What is the name of the file you create with notepad. Your text says the name is "fileopening" but a file with that name should have effect on your program. Could the file notepad creates actually be called "fileopening.txt"?

If so, is notepad still open when you run your program? I would expect notepad to have an exclusive hold on the file which would prevent your program from erasing its contents and writing new data to it.

Regardless, either step through the program with the debugger and note the values of err and fp after the call to fopen_s or, better yet, check the value of err before calling fputc and generate an error message if it is not 0.

1 Vote 1 ·

It means fputc can create a file. Is it true ...?

Not exactly. It is fopen which creates the file.
After which fputc can write to it.

If no writes are done to the file via fputc,
fwrite, etc. then fopem/fclose will create
an empty - zero length - file.

  • Wayne

1 Vote 1 ·

The compiler is creating the file "fileopening.txt" by its own on the desktop.

That is a ridiculous assertion.

But the problem is arising only when i create a file by opening notepad and saving as "fileopening".

Well, are you creating a file named "fileopening" or "fileopening.txt"?

And posting the same code that we have already seen adds no relevant information.

When you decide to address the numerous points and suggestions that have been posted to this thread by the various community members that have responded to it I'll revisit this question.





0 Votes 0 ·

I did not give any assertion but i told just what i got after debugging the C program after deleting the text file "fileopening". Have you seen the C program? Instead of giving right answers you are using words like "ridiculous". I am a just begginer in programming and i want to learn new things from you all so please do not demotivate anyone by using such words. If u think i am doing mistakes please guide me instead of demotivating.

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 JitendraKumarChauhan-1166 ·

We have tried to guide you but you have diligently ignored every bit of advice given to you.

Go back and re-read my earlier comments and answers. Its obvious that I have looked at your posted code.

And the compiler would not create a file on your desktop on its own. Saying that you were able to successfully build your executable and that it ran to completion without error and created the desired file on the desktop is a completely different statement.

1 Vote 1 ·
Show more comments
SimpleSamples avatar image SimpleSamples JitendraKumarChauhan-1166 ·

Yes RLWA32-6355 should not have said ridiculous; he sometimes is not as careful about what he says as he should. At least he is not ignoring you.

0 Votes 0 ·
Show more comments