0

I have this c module:

#include "stdafx.h"
#include "targetver.h"

#include "libavutil\mathematics.h"
#include "libavcodec\avcodec.h"

FILE fileName;

I did File fileName;

This i have init function:

void init(const char *filename)
{
    fileName = filename;
    avcodec_register_all();
    printf("Encode video file %s\n", fileName);

So i did fileName = filename; The reason i did is that i have another function i did called start():

void start()
{
    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
//    f = fopen(filename, "wb");
    errn = fopen_s(&f,fileName, "wb");

    if (!f) {
        fprintf(stderr, "Could not open %s\n", fileName);
        exit(1);
    }
}

And in start i had filename but it didn't find it so i wanted to use fileName instead. But i'm getting now few errors:

On this line: fileName = filename; on the = symbol i'm getting red line error:

Error 1 error C2440: '=' : cannot convert from 'const char *' to 'FILE'

Then on this line: errn = fopen_s(&f,fileName, "wb") On the fileName i'm getting:

Error 2 error C2065: 'filename' : undeclared identifier

Same error number 2 on this line on the fileName: fprintf(stderr, "Could not open %s\n", fileName);

Then another error on the fileName = filename:

6   IntelliSense: no operator "=" matches these operands
        operand types are: FILE = const char *

Last error: 7 IntelliSense: no suitable conversion function from "FILE" to "const char *" exists

All i wanted to do is to declare global fileName variable to use it in all places.

4

1 回答 1

5

FILE是一种类型,用于表示打开的文件(它包含文件句柄、文件中的位置等)。您不能将 a 存储char *在 type 的变量中FILE,因为它们是不同的类型。在此处阅读有关 FILE 类型的信息

您要做的是存储文件名。文件名是一个字符串。改用 a const char *。您的错误消息准确地告诉您:“无法将字符串转换为文件”。

Error 1 error C2440: '=' : cannot convert from 'const char *' to 'FILE'

阅读这些错误并尝试理解它们的实际含义可以帮助您解决此类问题。如果编译器抱怨将一种类型转换为另一种类型,这清楚地表明您对值的类型或您尝试将其分配给的变量感到困惑。

于 2013-04-25T15:28:24.693 回答