C++ fopen_s not declared in scope

I understand fopen_s is not part of C++ std library. How can I replace it qwith std::fstream? Also I need to replace malloc and free with new and delete operator. please help me.

Line 20:[Error] 'fopen_s' was not declared in this scope
Line 30:[Error] 'fopen_s' was not declared in this scope

Below is my code which gives errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdio>
#include <fstream>
#include <windows.h>
#include "Header.h"

using namespace std;

int main(int argc, char** argv)
{
	FILE* TGAFile, * yuvFile;
	HEADER Hd;
	int Width, Height, pxCount;

	char* TGAFileName = NULL;
	char* yuvFileName = NULL;
	TGAFileName = argv[1];
	yuvFileName = argv[2];

	if (fopen_s(&TGAFile, TGAFileName, "rb") == 0)
	
	{
		cout << "File opened! " << TGAFileName << "." << endl;
	}
	else
	{
		cout << "File not opened! " << TGAFileName << "." << endl;
		exit(0);
	}
	if (fopen_s(&yuvFile, yuvFileName, "wb+") == 0)
	
	{
		cout << "File opened! " << yuvFileName << "." << endl;
	}
	else
	{
		cout << "File not opened!  " << yuvFileName << "." << endl;
		exit(0);
	}

	ReadTGAHeader(&Hd, TGAFile);

	Width = Hd.Height;
	Height = Hd.Width;
	pxCount = Width * Height;
	unsigned char* RGB = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height * 3));
	unsigned char* Y, * U, * V;
	unsigned char* R, * G, * B;
	R = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
	G = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
	B = (unsigned char*)malloc(sizeof(unsigned char) * (Width * Height));
	Y = (unsigned char*)malloc(sizeof(char) * (Width * Height));
	U = (unsigned char*)malloc(sizeof(char) * (Width * Height / 4));
	V = (unsigned char*)malloc(sizeof(char) * (Width * Height / 4));

	DATA* RGBaData = NULL;
	RGBaData = new DATA[Hd.Width * Hd.Height];
	memset(RGBaData, 0, Hd.Height * Hd.Width);
	RGB = new unsigned char[Hd.Width * Hd.Height * 3];
	memset(RGB, 0, Hd.Height * Hd.Width * 3);

	int Ost = 0;
	Ost += Hd.IdLength;
	Ost += Hd.ColourMapType * Hd.ColourMapLength * Hd.ColourMapDepth;
	fseek(TGAFile, Ost, SEEK_CUR);

	ReadColourData(&Hd, RGBaData, TGAFile);

	Width = Hd.Width;
	Height = Hd.Height;
	for (int i = 0; i < Height; i++)
	{
		for (int j = 0; j < Width; j++)
		{
			int RGBPxNum = (Height - 1 - i) * Width + j;
			int TGAPxNum = i * Width + j;

			RGB[3 * RGBPxNum + 2] = RGBaData[TGAPxNum].R;
			RGB[3 * RGBPxNum + 1] = RGBaData[TGAPxNum].G;
			RGB[3 * RGBPxNum] = RGBaData[TGAPxNum].B;
		}
	}

	RGB2YUV(RGB, Y, U, V, Width, Height);
	free(RGB);

	fwrite(Y, sizeof(unsigned char), Width * Height, yuvFile);
	fwrite(U, sizeof(unsigned char), Width * Height / 4, yuvFile);
	fwrite(V, sizeof(unsigned char), Width * Height / 4, yuvFile);
	fclose(yuvFile);
}

Use fopen() to open the files. For example:

1
2
3
4
if (TFAFile = fopen(TGAFileName, "rb"))
	{
		cout << "File opened! " << TGAFileName << "." << endl;
	}


To use new instead of malloc:
1
2
	R = new unsigned char[Width * Height];
	G = new unsigned char[Width * Height];

The syntax to delete an array is a little odd:
1
2
	delete[] R;
	delete[] G;




Hello leo2008,

At the moment this is untested because I do not have the input files that you are using. This is code I have used in the past so it should work. I will know more later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <cstdio>
#include <windows.h>

#include <fstream>

#include "Header.h"

using namespace std;  // <--- Best not to use.

int main(int argc, char** argv)
{
    const std::string inFileNameTGAF = argv[1];
    const std::string outFileNameYUV = argv[2];

    std::ifstream inFileTGAF(inFileNameTGAF, std::ios::binary);

    if (!inFileTGAF)
    {
         //return std::cerr << "\n\n     File " << std::quoted(inFileName) << " did not open.\n", 1;  // <--- Requires header file "<iomanip>".
        return std::cerr << "\n\n     File \"" << inFileNameTGAF << "\" did not open.\n", 1;
    }

    std::ofstream outFileYUV(inFileNameYUV, std::ios::binary);

    if (!outFileYUV)
    {
        //return std::cerr << "\n\n     File " << std::quoted(outFileNameYUV) << " did not open.\n", 1;
        return std::cerr << "\n\n     File \"" << outFileNameYUV << "\" did not open.\n", 2;
    }


In the future if you have a program the requires an input file it is very helpful to include the file or at least a good sample if it is large. This way everyone will be using the same information and there is no guess work as to what the file should be.

Andy
Hello leo2008,

I just noticed that you have a file "Header.h". You need to include this also so that the program can be compiled and tested.

Andy
Topic archived. No new replies allowed.