0
votes

I added this code on a program supposedly to create a textfile(there were errors when I added the bottom part of the program marked with //textfile)

//here is a fragment of the program

    void next_macroblock(Macroblock *currMB)
    {
      VideoParameters *p_Vid = currMB->p_Vid;
      InputParameters *p_Inp = currMB->p_Inp;
      Slice *currSlice = currMB->p_Slice;
      int slice_type = currSlice->slice_type;
      BitCounter *mbBits = &currMB->bits;

          (some codes deleted)

  // Statistics
  cur_stats->quant[slice_type] += currMB->qp;
  ++cur_stats->num_macroblocks[slice_type];

  //textfile
  FILE * pFile;
  pFile = fopen ("mytable.txt","w");
  fprintf (pFile, " \t %d \t | \n",mbBits->mb_total);
  fclose (pFile);
}

errors:

**c:\jm 18.4\lencod\src\macroblock.c(223): error C2275: 'FILE' : illegal use of this type as an expression**
          **c:\program files\microsoft visual studio 11.0\vc\include\stdio.h(66) : see declaration of 'FILE'**
c:\jm 18.4\lencod\src\macroblock.c(223): error C2065: 'pFile' : undeclared identifier
c:\jm 18.4\lencod\src\macroblock.c(224): error C2065: 'pFile' : undeclared identifier
c:\jm 18.4\lencod\src\macroblock.c(224): warning C4047: '=' : 'int' differs in levels of indirection from 'FILE *'
c:\jm 18.4\lencod\src\macroblock.c(225): error C2065: 'pFile' : undeclared identifier
c:\jm 18.4\lencod\src\macroblock.c(225): warning C4047: 'function' : 'FILE *' differs in levels of indirection from 'int'
c:\jm 18.4\lencod\src\macroblock.c(225): warning C4024: 'fprintf' : different types for formal and actual parameter 1
c:\jm 18.4\lencod\src\macroblock.c(226): error C2065: 'pFile' : undeclared identifier
c:\jm 18.4\lencod\src\macroblock.c(226): warning C4047: 'function' : 'FILE *' differs in levels of indirection from 'int'
c:\jm 18.4\lencod\src\macroblock.c(226): warning C4024: 'fclose' : different types for formal and actual parameter 1

HERE'S THE PROBLEM: I already placed #include (becaues FILE is defined in stdio.h) then saved it in Unicode format (Because the program told me to save it in unicode format, otherwise more errors will appear), by going to FILE->advance saving options->UTF 8 with signature. The error about "saving the file in Unicode Format still persisted until I deleted some of the comments /* */ which contains German characters.

still the same errors about FILE. what went wrong?

According to @macduff and @KeithThompson I should include { } on that part of the code which I did. The errors were gone but no textfile was created (what I want to do is create a textfile which prints the values of the variables in the code.)

NEW QUESTION: If Visual Studio 2012 follows the C89/C90 standard and I can't declare variables in the middle of a function or a block, what is the best way to create a textfile in order to print the changing values of my variable mbBit->mb_total? (should I put { } on the codes to create a text file, make a new function or declare my paramaters in a header file - if yes, what parameters must I declare and how do I declare them?)

1
If you're asking a question about an error message, why don't you include the actual error message? Even some syntactically correct code would be a nice touch.Carl Norum
#in l clude? prile undefined? fprintf(pfile, "%d", number->var_inp? SSCCE, please.milleniumbug
my computer is running slow and this is what I remember from my code :) sorry. here is the codemc8
What are the lines before FILE.macduff
I think if you can't deal with basic C syntax errors, writing video processing code in C may not be a good plan.Carl Norum

1 Answers

2
votes

I don't believe the compiler will allow you to declare a variable at that point in the function, of course that is dependent on compiler/settings/etc. Maybe try beginning and ending {}:

{
  /*the other variables are declared and defined before this code fragment*/
  FILE *pfile;
  pfile=fopen("textfile.txt","w");
  fprintf(pfile, "%d", number->var_inp);
  fclose(pfile);
}

to address any scoping issues you may be running into.

To address some of your questions:

`The program '[5780] lencod.exe' has exited with code 300 (0x12c).`

This looks fine, as far as being able to compile the code with no errors. Likely the file is being written to, but you would have to open it up and look to make sure. The text file will be in the working directory, where the program is executing. Placing an absolute path, may be a bit more convenient, such as

 pfile=fopen("C:\\textfile.txt","a");

You may want to just append for debugging purposes for now. Also, you may want to include a test message like:

 fprintf(pfile, "Get the number\n");
 fprintf(pfile, "%d", number->var_inp);

I have a question: IF you're going to use FILE as a data type, do you have to declare it anywhere else in the program other than putting #include <stdio.h>?????????

If you are going to use the stdio.h definition of FILE, it must not be declared anywhere else. In short, do not declare any type as FILE, use the stdio.h definition.

What do you mean by scoping issues?

It appears your compiler, expects all variables in the block to be declared first and then functional code. If you want to create another variable for temporary use, like just to get a handle to a file, you must do it in another scope. One very simple way to define a scope is just to add the, {} around any code. After the new scope is created, you may define new variables, and then work on them. However, any new variables will be inaccessible after the }. In our case, this is of no consequence.

EDIT

I made this explanation far more accurate, by pulling from KeithThompson's comment. I feel like the comments really deserve to be here, he writes:

C89/C90 requires a block (a chunk of code surrounded by { and } to consist of zero or more declarations followed by zero or more statements. Declarations don't have to be at the beginning of a function, just at the beginning of a block. C99 relaxed this rule (borrowing from C++), permitting declarations and statements to be mixed within a block. It looks like your compiler is enforcing the C89/C90 rules. Microsoft's C compiler is notorious for this.

This is more succinct and accurate explaination.