2017-10-12 94 views
0

在windows下编译的以下C代码在linux(wine)下导致异常。fwrite函数在酒中运行时导致异常

该代码只是写入文件的东西,在Windows下工作正常。代码没有使用任何图形API,但葡萄酒警告说要创建一个窗口。

文本实际上是正确写入文件,但程序崩溃。

#include <windows.h> 
#include <stdio.h> 

FILE *fp; 
int main(){ 
    if(fp = fopen("Write.txt", "w")){ 
     if (fp){ 
      fwrite("WineTest\n", 1, 9, fp); 
      fclose(fp); 
      fp = NULL; 
     } 
     else printf("Cannot open file.\n"); 
     fclose(fp); 
    } 
    return 0; 
} 

错误信息:

wine: Unhandled page fault on read access to 0x00000050 at address 0x7bc49308 (thread 005f), starting debugger... err:winediag:nulldrv_CreateWindow Application tried to create a window, but no driver could be loaded. err:winediag:nulldrv_CreateWindow Make sure that your X server is running and that $DISPLAY is set correctly. Unhandled exception: page fault on read access to 0x00000050 in 64-bit code (0x000000007bc49308). Register dump: rip:000000007bc49308 rsp:000000000033fbc0 rbp:000000000033fc50 eflags:00010206 ( R- -- I 
- -P-) rax:00007f48dacafe40 rbx:0000000000000030 rcx:0000000000000030 rdx:00007f48dacb13a0 rsi:0000000000000019 rdi:0000000000010620 r8:00000000004300e8 r9:0000000000540000 r10:0000000000000008 r11:0000000000000246 r12:0000000000000001 r13:0000000000000008 r14:00007f48dc70e608 r15:0000000000000019 Stack dump: 0x000000000033fbc0: 0000000000000000 0000000000000000 0x000000000033fbd0: 00007f48dacafed0 0000000000000000 0x000000000033fbe0: 0000000000000000 00007f48daa2a45e 0x000000000033fbf0: 0000000000000000 0000000000000000 0x000000000033fc00: 0000000000000001 0000000000000008 0x000000000033fc10: 0000000000000001 0000000000000001 0x000000000033fc20: 0000000000000019 00000000004015cc 0x000000000033fc30: 0000000000000000 0000000000000000 0x000000000033fc40: 0000000000000000 0000000000000001 0x000000000033fc50: 0000000000431910 00000000004013e8 0x000000000033fc60: 00004d338ffde2c3 00002b992ddfa232 0x000000000033fc70: 00000000f9f27ba5 000000000000005f Backtrace: 
=>0 0x000000007bc49308 RtlEnterCriticalSection+0x8() in ntdll (0x000000000033fc50) 1 0x00007f48daa2a45e MSVCRT_fclose+0xd() in msvcrt (0x000000000033fc50) 2 0x00000000004015cc in winetest (+0x15cb) (0x000000000033fc50) 3 0x00000000004013e8 in winetest (+0x13e7) (0x0000000000431910) 4 0x000000000040151b in winetest (+0x151a) (0x00007fffff7ef000) 5 0x000000007b47937b in kernel32 (+0x5937a) (0x00007fffff7ef000) 6 0x000000007bc964be call_thread_func+0xcd() in ntdll (0x00007fff73b1b600) 7 0x000000007bc8f552 RtlRaiseException+0x7d() in ntdll (0x00007fff73b1b600) 8 0x000000007bc5eca0 in ntdll (+0x3ec9f) (0x00007fff73b1b600) 9 0x00007f48dc372843 wine_call_on_stack+0x12() in libwine.so.1 (0x00007fff73b1b600) 10 0x00007f48dc3729a9 wine_switch_to_stack+0x8() in libwine.so.1 (0x00007fffff7ef000) 11 0x000000007bc65210 LdrInitializeThunk+0x30f() in ntdll (0x00007fffff7ef000) 12 0x000000007b47fa78 
__wine_kernel_init+0x9f7() in kernel32 (0x00007fff73b1baa0) 13 0x000000007bc661e8 __wine_process_init+0x167() in ntdll (0x00007fff73b1c7c0) 14 0x00007f48dc37149e wine_init+0x29d() in libwine.so.1 (0x00007fff73b1c8a0) 15 0x000000007c000b12 main+0x71() in <wine-loader> (0x00007fff73b1cd98) 16 0x00007f48dbdb1b45 
__libc_start_main+0xf4() in libc.so.6 (0x0000000000000000) 17 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 18 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 19 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 20 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 21 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 22 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 23 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 24 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 25 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 26 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 27 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 28 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 29 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 30 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 31 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 32 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 33 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 34 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 35 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 36 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 37 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 38 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 39 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 40 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 41 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 42 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 43 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 44 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 45 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 46 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 47 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 48 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 49 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 50 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 51 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 52 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 53 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 54 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 55 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 56 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 57 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 58 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 59 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 60 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 61 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 62 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 63 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 64 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 65 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 66 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 67 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 68 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 69 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 70 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 71 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 72 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 73 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 74 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 75 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 76 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 77 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 78 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 79 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 80 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 81 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 82 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 83 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 84 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 85 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 86 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 87 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 88 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 89 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 90 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 91 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 92 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 93 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 94 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 95 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 96 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 97 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 98 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 99 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 100 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 101 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 102 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 103 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 104 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 105 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 106 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 107 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 108 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 109 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 110 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 111 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 112 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 113 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 114 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 115 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 116 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 117 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 118 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 119 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 120 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 121 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 122 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 123 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 124 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 125 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 126 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 127 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 128 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 129 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 130 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 131 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 132 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 133 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 134 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 135 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 136 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 137 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 138 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 139 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 140 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 141 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 142 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 143 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 144 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 145 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 146 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 147 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 148 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 149 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 150 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 151 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 152 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 153 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 154 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 155 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 156 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 157 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 158 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 159 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 160 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 161 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 162 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 163 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 164 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 165 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 166 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 167 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 168 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 169 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 170 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 171 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 172 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 173 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 174 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 175 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 176 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 177 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 178 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 179 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 180 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 181 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 182 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 183 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 184 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 185 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 186 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 187 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 188 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 189 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 190 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 191 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 192 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 193 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 194 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 195 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 196 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 197 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 198 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 199 0x000000007c000c0a _start+0x28() in <wine-loader> (0x0000000000000000) 200 0x000000007c000c0a 
_start+0x28() in <wine-loader> (0x0000000000000000) 0x000000007bc49308 RtlEnterCriticalSection+0x8 in ntdll: cmpq $0,0x0000000000000020(%rcx) Modules: Module Address     Debug info  Name (22 modules) PE    400000-   422000  Dwarf   winetest ELF    7b400000-  7b804000  Dwarf   kernel32<elf> \-PE   7b420000- 7b804000  \    kernel32 ELF    7bc00000-  7bd13000  Dwarf   ntdll<elf> \-PE   7bc20000- 7bd13000  \    ntdll ELF    7c000000-  7c002000  Dwarf   <wine-loader> ELF   7f48da589000- 7f48da7b3000  Deferred  libtinfo.so.5 ELF   7f48da7b3000- 7f48da9d8000  Deferred  libncurses.so.5 ELF   7f48da9d8000- 7f48dacb2000  Dwarf   msvcrt<elf> \-PE  7f48daa00000- 7f48dacb2000  \   msvcrt ELF   7f48dae3b000- 7f48db047000  Deferred  libnss_files.so.2 ELF   7f48db047000- 7f48db252000  Deferred  libnss_nis.so.2 ELF   7f48db252000- 7f48db46a000  Deferred  libnsl.so.1 ELF   7f48db46a000- 7f48db672000  Deferred  libnss_compat.so.2 ELF   7f48db672000- 7f48db888000  Deferred  libgcc_s.so.1 ELF   7f48db888000- 7f48dbb89000  Deferred libm.so.6 ELF   7f48dbb8c000- 7f48dbd90000  Deferred  libdl.so.2 ELF   7f48dbd90000- 7f48dc13b000  Dwarf  libc.so.6 ELF   7f48dc13c000- 7f48dc359000  Deferred  libpthread.so.0 ELF   7f48dc369000- 7f48dc70f000  Dwarf libwine.so.1 ELF   7f48dc711000- 7f48dc934000  Deferred ld-linux-x86-64.so.2 ELF   7fff73b34000- 7fff73b36000  Deferred  [vdso].so Threads: process tid  prio (all id:s are in hex) 0000000e services.exe 
     00000020 0 
     0000001f 0 
     00000014 0 
     00000010 0 
     0000000f 0 00000012 winedevice.exe 
     0000001e 0 
     00000019 0 
     00000018 0 
     00000013 0 0000001c plugplay.exe 
     00000022 0 
     00000021 0 
     0000001d 0 00000023 explorer.exe 
     00000027 0 
     00000026 0 
     00000025 0 
     00000024 0 00000036 wineconsole.exe 
     0000003a 0 
     00000037 0 00000038 Cam_1.exe 
     0000003c 0 
     0000003b 0 
     00000039 0 0000003e wineconsole.exe 
     00000042 0 
     0000003f 0 00000040 Cam_2.exe 
     00000044 0 
     00000043 0 
     00000041 0 0000005e (D) Z:\home\jwz\WineTest.exe 
     0000005f 0 <== System information: 
    Wine build: wine-2.0.2 
    Platform: x86_64 
    Version: Windows XP 
    Host system: Linux 
    Host version: 3.16.0-4-amd64 

编译系统:视窗7 X64的

编译器:MinGW的克++

Linux操作系统:Debian的8 64

酒:葡萄酒/ wineconsole/wine64(尝试所有三种,同样的错误)

+8

你为什么要关闭它两次? – farbiondriven

+0

如果'fp'与'NULL'不同,为什么要测试两次呢?第一个测试'if(fp = fopen(“Write.txt”,“w”)){'确实已经测试过fp是否为NULL。 –

+0

你真的不想在'fp = NULL'后面调用'fclose(fp)' - 这是未定义的行为,而你只是不幸的是它没有在你的Windows主机上发生段错误。 –

回答

1

如果公开成功,您将关闭两次! 我评论了第二个fclose(fp)。 请检查: Fclose a file that is already fclose

#include <windows.h> 
#include <stdio.h> 

FILE *fp; 
int main(){ 
    if(fp = fopen("Write.txt", "w")){ 
     if (fp){ 
      fwrite("WineTest\n", 1, 9, fp); 
      fclose(fp); 
      fp = NULL; 
     } 
     else printf("Cannot open file.\n"); 
     //fclose(fp); EDIT:Commented 
    } 
    return 0; 
} 
+0

不完全 - 它关闭'fp',然后关闭'NULL'。这可以说是更糟。 –

1
if (fp){ 
    fwrite("WineTest\n", 1, 9, fp); 
    fclose(fp); 
    fp = NULL; 
} 
else printf("Cannot open file.\n"); 
fclose(fp); 

fclose第二次调用始终传递NULL指针。