2011-11-04 90 views
1
#include "stdafx.h" 
#include "stdio.h" 

int main(int argc, _TCHAR* argv[]) 
{ 
    FILE *fp; 
    fp = fopen("/tmp/test.txt", "w"); 
    fprintf(fp, "This is testing...\n"); 
    fclose(fp); 

    return 0; 
} 

我是新來的Visual Studio,但聽說其C/C的真棒IDE ++我做了一些基本的C和它的工作非常細,現在我有一個項目做,我很認真地停留在這一點上我不能夠與打開和關閉文件的方式是C當我編譯此代碼它給我一個錯誤錯誤而使用文件的使用C在Visual Studio 2010 IDE

看到這些圖片

http://farm7.static.flickr.com/6092/6311309820_9c06ef4f0a.jpg

http://farm7.static.flickr.com/6221/6310789365_4298e416bd.jpg

調試輸出:::

'FredEx Challenge.exe': Loaded 'C:\Users\Anunay\Documents\Visual Studio 2010\Projects\FredEx Challenge\Debug\FredEx Challenge.exe', Symbols loaded. 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded. 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\lpk.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\usp10.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\dwmapi.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\clbcatq.dll', Cannot find or open the PDB file 
'FredEx Challenge.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file 
FredEx Challenge.exe has triggered a breakpoint 

幫助我,我是開往危機

+0

當您在Visual Studio中創建項目時,您選擇了哪種類型?從發佈代碼的外觀上看,一個簡單的「Win32控制檯應用程序」是正確的。 – Blastfurnace

+1

你認爲「/tmp/test.txt」是什麼意思在Windows上? –

回答

1

最有可能一時間,程序無法創建文件/tmp/test.txt,可能是因爲有沒有/tmp目錄存在或不允許書寫。

在這種情況下,fopen()返回null,而且由於程序不檢查返回值,它會出現段錯誤(或今年無論Windows調用它)。

確保您知道程序運行時的默認驅動器。或者更好的是,刪除路徑並通過指定文件名爲test.txt來使用當前的工作目錄。

#include <errno.h> 
#include <stdio.h> 

int main(int argc, char *argv[]) 
{ 
    FILE *fp = fopen ("test.txt", "wt"); // t = text mode 
    if (!fp) 
    { 
     fprintf (stderr, "error %d creating file\n", errno); 
     return 1; 
    } 
    fprintf (fp, "This is testing...\n"); 
    fclose (fp); 
    return 0; 
}