Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running a file that works normally in a Windows environment on Linux.
Binary files created on Windows are read by Linux.
Data that was normally displayed on Windows does not work on Linux.
Is there any other way of reading data that is unique to Linux that I don't know?

What I have tried:

class File_ptr
{
	FILE *p;

public:

	File_ptr(const char *n, const char *a) { p = fopen(n, a); };
	File_ptr(FILE *pp) { p = pp; };
	~File_ptr()
	{
		if (p)
			fclose(p);
	};
	operator FILE *() { return p; };
};


#pragma pack(push, 1)
typedef struct _CTE_
{ 
	unsigned short Version;
	unsigned short Signature;
	unsigned short core;
	unsigned short editmode
} CTE;
#pragma pack(pop) t

#pragma pack(push, 1)
typedef struct _GTE_
{
	unsigned char LTL : 1;
	unsigned char trans : 1;
	uint_least8_t wcountry;
	uint_least8_t wlanguage;
	unsigned int count; 
	int WIDTH;
	int HEIGHT;	
} GTE;
#pragma pack(push, 1)

class WXR
{
    _HEADERFILEINFO_ m_FileHeader;
    _PROPERTIES_ m_properties;
public:
    WXR();
    ~WXR();
	const void OpenProjectFile(const int id);
	const void LoadHeader(FILE *hFile);

}
WXR::WXR() :
m_FileHeader{1, 42,10, 0},	
m_properties{1, 1, 9, 142, 0, 1920, 1080 }
{
}

WXR::~WXR()
{
	m_FileHeader = {};
	m_properties = {};
}

const void WXR::OpenProjectFile(const int id)
{
	const char *dir = NULL;

	if (id == 1)
	{
		dir = "data/1.pro/Project.txt";
	}
	else if (id == 2)
	{
		dir = "data/2.pro/Project.txt";
	}
	else if (id == 3)
	{
		dir = "data/3.pro/Project.txt";
	}
	else if (id == 4)
	{
		dir = "data/4.pro/Project.txt";
	}
	else
	{
		cout << "error file" << endl;
		return;
	}

	File_ptr *fp = new File_ptr(dir, "rb+,ccs=UNICODE");
    cout << __LINE__ << fp << endl;

	if (fp == NULL)
	{
		fprintf(stderr, "Couldn't open file for reading\n");
		exit(1);
	}

	LoadHeader(*fp);
	LoadProperties(*fp);
	delete fp;
}

const void WXR::LoadHeader(FILE *hFile)
{
    cout << __LINE__ << hFile << endl;
	cout << "headsize: " << sizeof(struct _HEADERFILEINFO_) << endl;
	fread(&m_FileHeader, sizeof(struct _HEADERFILEINFO_), 1, hFile);
	cout << __LINE__ << " " << m_FileHeader.Version << endl;
	cout << __LINE__ << " " << m_FileHeader.Signature << endl;
	cout << __LINE__ << " " << m_FileHeader.core << endl;
	cout << __LINE__ << " " << m_FileHeader.editmode << endl;
}

const void WXR::LoadProperties(FILE *hFile)
{
	cout << __LINE__ << hFile << endl;
    cout << "properties: " << sizeof(struct _PROPERTIES_) << endl;
	fread(&m_properties, sizeof(struct _PROPERTIES_), 1, hFile);
	cout << __LINE__ << " " << m_properties.LTL << endl;
	cout << __LINE__ << " " << m_properties.trans << endl;
	cout << __LINE__ << " " << m_properties.wcountry << endl;
	cout << __LINE__ << " " << m_properties.wlanguage << endl;
	cout << __LINE__ << " " << m_properties.count << endl;
	cout << __LINE__ << " " << m_properties.WIDTH << endl;
	cout << __LINE__ << " " << m_properties.HEIGHT << endl;
}

 =====================================================================================
Result:
                    windows(vscode) |   Linux 
 *fp               000002990A254F80    0x66f1a10
 hFile             000002990A3307A0    0x66f1a10
 headsize: (size)          8              8
                           1              0
                          42              0
                          10              0 
                           1              0

 hFile: (size)     000002990A3307A0   0x66f1a10
 properties                15             15
                                          ^@
                                          ^@
                                          ^@
                                          ^@
                           15              0
                          1920             0
                          1080             0
Posted
Updated 7-Apr-21 22:45pm
v3
Comments
Chopin2001 8-Apr-21 4:01am    
fp and hFile value is different on windows. but Linux isn't.

1 solution

The issue is most likely due to data alignment in your structures. In windows the default is to align everything on 16 or 32 bit boundaries. This means that your data structure will most likely take more space than you think. If you then read it on a system that does not enforce this alignment then the data will not be correctly positioned. You can avoid this by using the pack pragma | Microsoft Docs[^] in your application.
 
Share this answer
 
Comments
Chopin2001 8-Apr-21 4:30am    
You mean like this
window  [ ][ ][ ][ ][*][*][*][*]|[ ][ ][ ][*][*][*][*][*]
linux   [*][*][*][*][ ][ ][ ][ ]|[*][*][*][*][*][ ][ ][ ]
Richard MacCutchan 8-Apr-21 4:36am    
Possibly, although I do not know what those diagrams represent. You can check the sizes of your structures in each environment by using the sizeof operator.
Chopin2001 8-Apr-21 4:44am    
Sorry! This original code has a #pragma
size header : 8
size properties : 15.
same both.
Richard MacCutchan 8-Apr-21 5:04am    
Looking again at your output suggests that there is no data in the file you are reading on the Linux system. But since you do not check the result of your call to fread it is difficult to be certain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900