Ben Kochavi
Ben Kochavi

Reputation: 293

How can i declare a file name in c language?

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.

Upvotes: 0

Views: 4378

Answers (1)

Joe
Joe

Reputation: 47609

FILE is a type, used for representing an open file (it contains the file handle, position in file etc). You can't store a char * in a variable of type FILE, because they are different types. Read about the FILE type here.

What you want to do is store a file name. A file name is a string. Use a const char * instead. Your error message tells you this exactly: "can't convert a string into a 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.

Upvotes: 5

Related Questions