I'm trying to use Visual Studio 2008 since I had problems opening a file in XCode. I am new to VS, but these are the steps I took. I created a new project, selected Win32 Console Application, empty project. My code is:
// 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;
}
Something doesn't seem to be correct since my error list is:
>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开发者_如何学JAVA, 0 up-to-date, 0 skipped ==========
Any thoughts? Sorry if this is a>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 ==========
Any thoughts? Thanks!
You can only declare variables at the top of a scope {} before any code in C.
In C++ you can declare them anywhere. So either change your filename to test.cpp or move your first printf down below the declaration of FILE* fileptr.
int main(void )
{
FILE *filePtr;
printf("Hello");
精彩评论