Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An error occurs while reading the file.
I used cout for check file pointer.
In the readCTE function, The pointer value is 0.
Why does fp become 0 in the readCTE function?

This error is on just Linux. This is normal on Windows.

What I have tried:

#pragma pack(push, 1)
typedef struct _CTE_
{
	unsigned char Vrsion : 3;
	unsigned char Signature : 3;
	unsigned char core : 1;
	unsigned char editmode : 1;
	unsigned int : 4;
} CTE;
#pragma pack(pop)

CTE m_cte;
dir = "data/foo/data 1.tui";

FILE *fp = fopen(dir, "rb+,ccs=UNICODE");

if (fp)
{
    cout << fp << endl; //result 0x66cefe0
	readCTE(*fp);		
}
else
{
	cout << " no file pointer. NULL";
}

void readCTE(FILE *hFile){
   cout << hFile <<  endl;                 // result 0
   fread(&m_cte, sizeof(struct _CTE_), 1, hFile);
}
Posted
Updated 6-Apr-21 18:46pm
v3
Comments
Dave Kreskowiak 6-Apr-21 23:48pm    
Did the file open? From the path you specified, I doubt it. Check the value of fp BEFORE and AFTER the fopen statement. If the value does not change, changes are really good the file did not open because the file or path to it was not found.
Chopin2001 8-Apr-21 1:48am    
I didn't considered a empty space of filename. Thanks so much

The code, as presented will not compile, even taking into account that you have omitted much for sake of your question. e.g.
C++
FILE *f = fopen(/* ... */);
readCTE(*fp); // Compiler error : cannot convert _IO_FILE to FILE *

void readCTE(FILE *hFILE)

Check your function declaration, and how you are passing the file pointer to readCTE()
 
Share this answer
 
Comments
Chopin2001 7-Apr-21 0:35am    
That's right. Many parts have been omitted for the sake of the question, and these are the parts that have been checked on Windows. The code above is the class implementation.
And compiling is no problem.

void TESTADD::readCTR(FILE *hFile)
It was my fault. I didn't think of whitespace in the file.
There was no problem with whitespace handling on Windows.
Before the function, the file pointer was a garbage value.
dir = "data/foo/data 1.tui";
dir = "data/foo/data1.tui";


if (fp)
{
    cout << fp << endl; //result 0x66cefe0 << garbege value
	readCTE(*fp);		
}
 
Share this answer
 
v2

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