简体   繁体   English

如何用C语言声明文件名?

[英]How can i declare a file name in c language?

I have this c module: 我有这个C模块:

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

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

FILE fileName;

I did File fileName; 我做了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; 所以我做了fileName = filename; The reason i did is that i have another function i did called start(): 我这样做的原因是我还有另一个名为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. 在开始时我有文件名,但是找不到,所以我想改用fileName。 But i'm getting now few errors: 但是我现在得到一些错误:

On this line: fileName = filename; 在这一行:fileName = filename; on the = symbol i'm getting red line error: 在=符号上,我收到红线错误:

Error 1 error C2440: '=' : cannot convert from 'const char *' to 'FILE' 错误1错误C2440:“ =”:无法从“ const char *”转换为“ FILE”

Then on this line: errn = fopen_s(&f,fileName, "wb") On the fileName i'm getting: 然后在这一行:errn = fopen_s(&f,fileName,“ wb”)在fileName上,我得到:

Error 2 error C2065: 'filename' : undeclared identifier 错误2错误C2065:“文件名”:未声明的标识符

Same error number 2 on this line on the fileName: fprintf(stderr, "Could not open %s\\n", fileName); 在fileName的这一行上出现相同的错误编号2:fprintf(stderr,“无法打开%s \\ n”,fileName);

Then another error on the fileName = filename: 然后在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 上一个错误:7 IntelliSense:不存在从“ FILE”到“ const char *”的合适转换函数

All i wanted to do is to declare global fileName variable to use it in all places. 我要做的就是声明全局fileName变量以在所有地方使用它。

FILE is a type, used for representing an open file (it contains the file handle, position in file etc). FILE是一种类型,用于表示打开的文件(它包含文件句柄,文件中的位置等)。 You can't store a char * in a variable of type FILE , because they are different types. 您不能将char *存储在FILE类型的变量中,因为它们是不同的类型。 Read about the FILE type here . 在此处阅读有关FILE类型的信息

What you want to do is store a file name. 您要做的是存储文件名。 A file name is a string. 文件名是一个字符串。 Use a const char * instead. 请改用const char * Your error message tells you this exactly: "can't convert a string into a FILE". 您的错误消息完全告诉您:“无法将字符串转换为FILE”。

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

Reading these errors and trying to understand what they actually mean can help you solve problems like this. 阅读这些错误并尝试理解它们的实际含义可以帮助您解决此类问题。 If the compiler complains about converting one type to another, it's a clear sign that you've got confused about the type of the value or the variable you're trying to assign it to. 如果编译器抱怨将一种类型转换为另一种类型,那么这很明显表明您对要分配给它的值的类型或变量的类型感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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