0

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?)

mc8
  • 315
  • 7
  • 21
  • 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 Jun 25 '13 at 16:46
  • 1
    #in **l** clude? `prile` undefined? `fprintf(pfile, "%d", number->var_inp`? [SSCCE](http://sscce.org/), please. – milleniumbug Jun 25 '13 at 16:46
  • my computer is running slow and this is what I remember from my code :) sorry. here is the code – mc8 Jun 25 '13 at 16:49
  • What are the lines *before* `FILE`. – macduff Jun 25 '13 at 16:51
  • FILE * pFile; pFile = fopen ("textfile.txt","w"); fprintf (pFile, " \t %d \t | \n",mbBits->mb_total); fclose (pFile); – mc8 Jun 25 '13 at 16:52
  • there are so many lines in the code. it is for digital video processing. – mc8 Jun 25 '13 at 16:52
  • here is the actual code. the one with the errors are at the bottom – mc8 Jun 25 '13 at 16:55
  • Put the code in your question, not in a comment. – Carl Norum Jun 25 '13 at 16:57
  • 2
    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 Jun 25 '13 at 16:58
  • @urzeit they told me to post the codes above the code fragment I posted. This is the entire function declaration. – mc8 Jun 25 '13 at 17:28
  • `FILE` *is* recognized as a type name; the error message says so: "illegal use of this type as an expression". It just doesn't like your use of it at that point in the program. – Keith Thompson Jun 25 '13 at 18:34
  • but I know someone who used this kind of declaration and it worked well. He is also using Visual Studio 2012 @KeithThompson – mc8 Jun 25 '13 at 19:07
  • @mc8: He's probably using C++, which permits mixed declarations and statements. The dialect of C supported by Visual Studio does not. – Keith Thompson Jun 25 '13 at 19:19
  • What's the difference between C and C++? Is it on the compiler? Or the file extension? Wait can I make a variable declaration in a header file and not here? If yes, what else will I place in here? What are other options for creating a text file? (where in you need to print the parameters found inside this function in a .c file? @KeithThompson – mc8 Jun 26 '13 at 03:33
  • @mc8: C and C++ are two distinct (but related) languages. There are numerous books written about both of them; I can't summarize in a comment. MS Visual Studio includes compilers for both languages. Typically C source files have a `.c` extension, and C++ uses `.cpp`, though there are other possibilities. – Keith Thompson Jun 26 '13 at 04:40

1 Answers1

2

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.

macduff
  • 4,655
  • 18
  • 29
  • I am unable to see difference - Sorry, but will you please tell ? :P – Shumail Jun 25 '13 at 16:55
  • @Shumail92, create a new scope by "encasing" the code in "curly" brackets – macduff Jun 25 '13 at 16:56
  • I see. But you sure it's issue ? Anyways, lets see if he posts error message and other code. – Shumail Jun 25 '13 at 16:57
  • @mc8, did you try this? If it's of no value I'll remove it, but I think it may help. – macduff Jun 25 '13 at 17:11
  • @macduff yes i'll try it. I already posted the revisions which include the errors – mc8 Jun 25 '13 at 17:25
  • @macduff The program '[5780] lencod.exe' has exited with code 300 (0x12c). – mc8 Jun 25 '13 at 17:29
  • I think this means there are no errors, right? I placed curly brackets and debugged it and this is the output. – mc8 Jun 25 '13 at 17:30
  • this removed all the errors but the text file was not created. – mc8 Jun 25 '13 at 17:46
  • @macduff when will the textfile be created? there is still no text file created. but there where no build errors. – mc8 Jun 25 '13 at 18:20
  • oh. so you mean to say the pointer to a file pFile is only defined on that scope? I'm using Visual Studio 2012 @macduff – mc8 Jun 25 '13 at 18:22
  • what variables are you referring to? – mc8 Jun 25 '13 at 18:32
  • 1
    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. – Keith Thompson Jun 25 '13 at 18:37
  • What's C89/C90? @KeithThompson – mc8 Jun 25 '13 at 18:48
  • @mc8: ANSI published the first official C standard in 1989. ISO republished it in 1990. The 1989 ANSI standard and the 1990 ISO standard describe the same language, commonly referred to as C89 or C90 (and incorrectly as ANSI C). ISO published revised standards in 1999 (C99) and 2011 (C11). (A quick Google search would probably have answered that for you.) – Keith Thompson Jun 25 '13 at 18:50
  • so C89/C90 is ANSI C? Sorry this is the first time I have encountered C89/C90. Thanks – mc8 Jun 25 '13 at 18:53
  • So what is wrong with my previous code without the {}? @KeithThompson – mc8 Jun 25 '13 at 18:55
  • @mc8: It had a *declaration* `FILE * pFile;` following *statements* in the same block. That's perfectly legal under C99 rules, but illegal under the C90 rules that your compiler seems to be enforcing. If you're using Visual Studio, you'll have to write code that conforms to the C90 rules (or switch to C++ if you prefer). – Keith Thompson Jun 25 '13 at 18:57
  • Can you please elaborate more on what are "legal" for C90? I can't quite comprehend why FILE *pFile is illegal. @KeithThompson – mc8 Jun 25 '13 at 19:02
  • @mc8: Because declarations may not follow statements within a block in C90. – Keith Thompson Jun 25 '13 at 19:20
  • @KeithThompson alright I get it! Declarations should only be placed at the start of the block! This worked but there is still no text file created. – mc8 Jun 26 '13 at 03:23
  • @KeithThompson is C90 a standard for C language only? – mc8 Jun 26 '13 at 17:06
  • @KeithThompson Visual studio 2012 is using C89/C90 as standard?I can't find the standard used for Visual studio 2012. – mc8 Jun 26 '13 at 17:13
  • @mc8: Microsoft has said they have no plans to support C99 or C11. I understand that their support for C90 is pretty good, though (apart from their insistence on warning about certain standard library functions). – Keith Thompson Jun 26 '13 at 17:17
  • thanks. I got a copy of the draft of C89. I read the limitations on the scope. Is there any other significant constraints I must be aware of? Regarding the scope, what best way do you suggest on how will create a textfile? Where shall I put my declaration? Is putting a { } solves the problem? Because there aren't any textfile created. @KeithThompson – mc8 Jun 27 '13 at 00:23
  • @mc8: Your original question was about a compile-time failure. Now your program (which doesn't appear in the question) is not working as you expect at run time. That's a different question. (1) Check the values returned by your I/O calls, especially `fopen()`. (2) Post a new question with a small sample program that exhibits the problem you're now asking about (everything but the last half dozen or so lines seems irrelevant to that). – Keith Thompson Jun 27 '13 at 01:03
  • @KeithThompson how can i check the values returned by I/O calls? – mc8 Jun 27 '13 at 01:50
  • @KeithThompson may I ask you. what is the sequence in which the program is executed? line by line? is the main () function always executed first? what if there are other function calls before the main () function? thanks. – mc8 Jun 29 '13 at 02:43
  • @macduff may I ask you. what is the sequence in which the program is executed? line by line? is the main () function always executed first? what if there are other function calls before the main () function? thanks. – mc8 Jun 29 '13 at 02:43