我在一个程序上添加了这个代码,据说是为了创建一个文本文件(当我添加用// 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);
}
错误:
**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
这里有问题: 我已经放置#include(因为FILE在stdio.h中定义)然后以Unicode格式保存(因为程序告诉我以unicode格式保存它,否则会出现更多错误),通过转到FILE->提前保存options->带签名的UTF 8。关于“以Unicode格式保存文件”的错误仍然存在,直到我删除了包含德语字符的一些注释/ * * /。
关于FILE的错误仍然相同。出了什么问题?
根据@macduff和@KeithThompson,我应该在我所做的那部分代码中包含{}。错误消失但没有创建文本文件(我想要做的是创建一个文本文件,在代码中打印变量的值。)
新问题: 如果Visual Studio 2012遵循C89 / C90标准并且我不能在函数或块的中间声明变量,那么创建文本文件以打印变量mbBit->的变化值的最佳方法是什么? mb_total? (我应该在代码上添加{}来创建文本文件,创建新函数或在头文件中声明我的参数 - 如果是,我必须声明哪些参数以及如何声明它们?)
答案 0 :(得分:2)
我不相信编译器会允许你在函数中的那一点声明一个变量,当然这取决于编译器/设置/等。也许尝试开始和结束{}
:
{
/*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);
}
解决您可能遇到的任何范围问题。
解决您的一些问题:
`The program '[5780] lencod.exe' has exited with code 300 (0x12c).`
这看起来很好,只要能够编译没有错误的代码。可能文件正在被写入,但你必须打开它并期待确保。文本文件将位于正在执行程序的工作目录中。放置绝对路径可能会更方便一些,例如
pfile=fopen("C:\\textfile.txt","a");
您现在可能只想附加调试目的。此外,您可能希望包含以下测试消息:
fprintf(pfile, "Get the number\n");
fprintf(pfile, "%d", number->var_inp);
我有一个问题:如果你要将FILE用作数据类型,那么你呢? 必须在程序中的任何其他地方声明它而不是推杆
#include <stdio.h>
?????????
如果您要使用stdio.h
的{{1}}定义,则必须在其他地方声明 。简而言之,请勿将任何类型声明为FILE
,请使用FILE
定义。
范围界定问题是什么意思?
您的编译器似乎要求首先声明块中的所有变量,然后再声明功能代码。如果要为临时使用创建另一个变量,就像获取文件句柄一样,必须在另一个范围内执行。定义范围的一种非常简单的方法是在任何代码周围添加stdio.h
。创建新范围后,您可以定义新变量,然后处理它们。但是, {}
之后将无法访问任何新变量。在我们的例子中,这没有任何后果。
修改强>
通过引用KeithThompson的评论,我使这个解释更加准确。我觉得这些评论真的值得在这里,他写道:
C89 / C90需要一个块(由
}
和{
包围的一大块代码 由零个或多个声明组成,后跟零个或多个 声明。声明不必在a的开头 功能,就在一个块的开头。 C99宽松了这条规则 (借用C ++),允许声明和陈述 在一个区块内混合。看起来您的编译器正在执行 C89 / C90规则。微软的C编译器因此而臭名昭着。
这是更简洁的和准确的解释。