Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm starting to program and I'm using Visual Studio. I have this simple program that some days ago worked, but after working with another project, it returns me an error:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>
int main() {
    double a = 3;
    FILE *A;
    scanf("%lf", &a);
    A = fopen("B:\\Mis Documentos\\Coding\\Test 200.txt", "wt");
    fprintf(A, "Hello World, I have %lf", a);
    fclose(A);

But I can't compile it, it returns me this error. This is the log:

'Project1.exe' (Win32): Loaded 'B:\Mis Documentos\Documents\Visual Studio 2015\Projects\Project1\Debug\Project1.exe'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-timezone-l1-1-0.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l2-1-0.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-localization-l1-2-0.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-synch-l1-2-0.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-processthreads-l1-1-1.dll'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l1-2-0.dll'. Symbols loaded.
Debug Assertion Failed!
Program: ...ments\Visual Studio 2015\Projects\Project1\Debug\Project1.exe
File: minkernel\crts\ucrt\src\appcrt\stdio\output.cpp
Line: 31
Expression: stream != nullptr
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
                That is not a compiler error.  Your program has a bug and you need to debug it.  For one thing, you didn't check if fopen returned NULL.
– PaulMcKenzie
                Jul 13, 2016 at 7:21
                Just start with reading the documentation of fopen/fprintf (for example, fopen returns a pointer to the open file. A null pointer value indicates an error), and use the debugger, and you'll see for yourself quickly enough. Also you tagged this c++ but really it's just C?
– stijn
                Jul 13, 2016 at 7:24
                What is that 't' supposed to be doing in the 2nd parameter to fopen()? It might be causing the failure of the call.
– D Hydar
                Jul 13, 2016 at 7:32
                @DanHydar That means opening the file in text mode, and is perfectly fine and the right thing to do here.
– Sebastian Redl
                Jul 13, 2016 at 7:55

You're not checking if the file was actually opened. You should check if file was actually opened after calling fopen

FILE *A = NULL;
A = fopen("B:\\Mis Documentos\\Coding\\Test 200.txt", "wt");
//Check if file was actually opened
if(A) {

Check documentation on this function, for example here. You should also initialize A variable, it's a good practice.

This way it also gets initialized, however, his code flow might be different, for example, this pointer would be passed by reference in some function, where fopen is called, and it's better to get a habit to initialize everything. – buld0zzr Jul 13, 2016 at 8:06

At the end it was my fault, I didn't create the folder "Coding" where I wanted the file. I can finish what I wanted to do now, but I saw that if I want to create the file in my Google Drive folder, sometimes works an others don't. Not sure why, maybe because it's connected to internet?

Anyway, thanks a lot for your fast help!

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.