简体   繁体   中英

writing a C code in Visual studio

I wrote a simple function in Visual Studio to be able to study how to write a C project in Visual Studio but it gave the following errors:

Error   1   error C2275: 'FILE' : illegal use of this type as an expression        
c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c    25    
1   Exc4

Error   2   error C2065: 'file' : undeclared identifier   
c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c    25  
1   Exc4

  Error 3   error C2065: 'file' : undeclared identifier 
 c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c   31     1    
    Exc4
  Error 6   error C2065: 'file' : undeclared identifier 
   c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c 39  1   
   Exc4
    Error   9   error C2065: 'file' : undeclared identifier 
   c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c 41  1   
    Exc4

   Error    12  error C2065: 'file' : undeclared identifier 
    c:\users\henry\documents\visual studio 2010\projects\exc4\exc4\measurement.c    45  1   
   Exc4
17  IntelliSense: a value of type "void *" cannot be used to initialize an 
     entity of type "Tmeasurement *"    c:\users\henry\documents\visual studio 
      2010\projects\exc4\exc4\measurement.c 99  33  Exc4

I never had this error when i compiled the function on the mac Xcode compiler. Hoping any one out there could explain why the statement FILE *file is not identified.

Tmeasurement readMeasurements(Tmeasurement a, char *filename)
{
    int filesize, i;
    struct stat st;
    stat(filename, &st);
    filesize = st.st_size;

    a.ArraySize = filesize;
    a.measureArray = (float*)malloc(filesize*sizeof(float));

    FILE *file = fopen(filename, "r+");
    // FILE *file = fopen(filename, "r");
    /*filesize = fread(a.measureArray, 1, filesize, file);
    a.ArraySize = filesize;*/

    if(file==NULL)
    {
        printf("Could not open mea.dat!\n");
        return; 
    }

    for(i=0;  i<102 &&(fgetc(file))!=EOF;i++)
    {
        fscanf(file,"%e",&a.measureArray[i]);
    }

    fclose(file);
    return a;
}

Visual Studio is a C89 compiler.

Mixing declarations and code is a C99 feature.

Do not mix declarations and code or do not use Visual Studio :)

Microsoft compilers only support C89 standard, so variable declarations must be at the beginning of scope before any other statements.

Change to:

Tmeasurement readMeasurements(Tmeasurement a, char *filename)
{
   int filesize, i;
   struct stat st;
   FILE* file;

   ...

   file = fopen(filename, "r+");

   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM