2016-07-13 3 views
0

프로그램을 시작하고 Visual Studio를 사용하고 있습니다. 그것은 나를 this error을 반환디버그 어설 션이 실패했습니다. nullptr

#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); 

} 

그러나 나는 그것을 컴파일 할 수 없습니다 : 나는 몇 일 전 일이 간단한 프로그램을 가지고 있지만, 다른 프로젝트 작업을 한 후, 그것은 나에게 오류를 반환합니다. 로그입니다 :

'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. 
+0

이는 컴파일러 오류가 아닙니다. 프로그램에 버그가있어서 디버깅해야합니다. 한 가지는'fopen'이 NULL을 반환했는지 확인하지 않았다는 것입니다. – PaulMcKenzie

+1

fopen/fprintf의 문서를 읽는 것으로 시작하십시오 (예를 들어, * fopen은 열린 파일에 대한 포인터를 반환합니다. 널 포인터 값은 오류를 나타냅니다 *). 디버거를 사용하면 충분히 빠르게 볼 수 있습니다. 또한이 C++로 태그를 붙 였지만 실제로는 C 일 뿐이죠? – stijn

+0

fopen()의 두 번째 매개 변수에서 수행해야하는 작업은 무엇입니까? 통화가 실패했을 수 있습니다. –

답변

0

파일이 실제로 열렸는지 확인하지 않았습니다. 파일이 실제로 예를 here를 들어,이 기능에 fopen

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

확인 문서를 호출 한 후 연 경우 확인해야합니다. A 변수도 초기화해야합니다. 좋은 방법입니다.

+0

더 나은 것은'FILE * A = fopen (...', no? – stijn

+0

) 이렇게 쓰면됩니다. 그러나 그의 코드 흐름은 다를 수 있습니다. 예를 들어,이 포인터는 일부에서는 참조에 의해 전달됩니다. 함수에서 fopen을 호출하면 모든 것을 초기화하는 습관을 얻는 것이 좋습니다. – buld0zzr

0

결국 그것은 내 잘못이었습니다. 나는 파일을 원할 때 "코딩"폴더를 만들지 않았습니다. 지금하고 싶은 것을 끝낼 수는 있지만 Google 드라이브 폴더에 파일을 만들고 싶으면 다른 사람이 제대로 작동하지 않는 경우가 있습니다. 왜 인터넷에 연결되어 있는지, 이유는 모르겠습니까?

어쨌든 빠른 도움을 주셔서 감사합니다.

관련 문제