我正在尝试使用Visual Studio 2008,因为我在XCode中打开文件时遇到问题。我是VS的新手,但这些是我采取的步骤。我创建了一个新项目,选择了Win32 Console Application,空项目。我的代码是:
// C_test.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
printf("Hello");
FILE *filePtr;
filePtr = fopen( "test.txt", "r" );
if (filePtr == NULL)
{
fprintf(stderr, "Can't open \"test\"\n");
exit(EXIT_FAILURE);
}
else
{
int x;
printf("File open successful\n");
/* read one character at a time until EOF is reached */
while ((x = fgetc(filePtr)) != EOF)
{
//printf("%c", x);
fprintf(stderr, "%x\n",x);
}
}
fclose(filePtr);
system("pause");
return 0;
}
由于我的错误列表是:
,因此某些内容似乎不正确>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(9) : error C2275: 'FILE' : illegal use of this type as an expression
1> c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(69) : see declaration of 'FILE'
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(9) : error C2065: 'filePtr' : undeclared identifier
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(11) : error C2065: 'filePtr' : undeclared identifier
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(11) : warning C4047: '=' : 'int' differs in levels of indirection from 'FILE *'
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(12) : error C2065: 'filePtr' : undeclared identifier
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(12) : warning C4047: '==' : 'int' differs in levels of indirection from 'void *'
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(23) : error C2065: 'filePtr' : undeclared identifier
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(23) : warning C4047: 'function' : 'FILE *' differs in levels of indirection from 'int'
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(23) : warning C4024: 'fgetc' : different types for formal and actual parameter 1
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(29) : error C2065: 'filePtr' : undeclared identifier
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(29) : warning C4047: 'function' : 'FILE *' differs in levels of indirection from 'int'
1>c:\documents and settings\jon\my documents\visual studio 2008\projects\c_test\c_test\main.c(29) : warning C4024: 'fclose' : different types for formal and actual parameter 1
1>Build log was saved at "file://c:\Documents and Settings\jon\My Documents\Visual Studio 2008\Projects\C_test\C_test\Debug\BuildLog.htm"
1>C_test - 6 error(s), 6 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
有什么想法?很抱歉,如果这是一个&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(9):error C2275:'FILE':非法使用此类型作为表达式 1 GT; c:\ program files \ microsoft visual studio 9.0 \ vc \ include \ stdio.h(69):查看'FILE'的声明 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(9):error C2065:'filePtr':未声明的标识符 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(11):error C2065:'filePtr':未声明的标识符 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(11):warning C4047:'=':'int'的间接级别与'FILE *不同“ 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(12):error C2065:'filePtr':未声明的标识符 1&gt; c:\ documents和settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(12):warning C4047:'==':'int'的间接级别与'void'不同*” 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(23):error C2065:'filePtr':未声明的标识符 1&gt; c:\ documents和settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(23):warning C4047:'function':'FILE *'的间接级别与'int'不同“ 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(23):warning C4024:'fgetc':正式和实际参数1的不同类型 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(29):error C2065:'filePtr':未声明的标识符 1&gt; c:\ documents and settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(29):warning C4047:'function':'FILE *'的间接级别与'int'不同“ 1&gt; c:\ documents和settings \ jon \ my documents \ visual studio 2008 \ projects \ c_test \ c_test \ main.c(29):warning C4024:'fclose':正式参数和实际参数1的不同类型 1&gt;构建日志保存在“file:// c:\ Documents and Settings \ jon \ My Documents \ Visual Studio 2008 \ Projects \ C_test \ C_test \ Debug \ BuildLog.htm”中 1&gt; C_test - 6个错误,6个警告 ==========构建:0成功,1个失败,0个最新,0个跳过==========
有什么想法?谢谢!
答案 0 :(得分:5)
在C中的任何代码之前,您只能在范围{}的顶部声明变量。
在C ++中,您可以在任何地方声明它们。所以要么将你的文件名更改为test.cpp或 将您的第一个printf移到FILE * fileptr。
的声明之下int main(void )
{
FILE *filePtr;
printf("Hello");