2011-11-04 59 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; 
}