Link to home
Start Free TrialLog in
Avatar of cwh
cwh

asked on

CFile to open stream file

Do MFC has a function like fread and fwrite in C? I would like to read and write a file using stream I/O, but the MFC CFile write text.

The purpose of writing using fread and fwrite is to prevent other from viewing the file.

Please suggest.

cwh
Avatar of migel
migel

Hi!
What do you mean under:
"The purpose of writing using fread and fwrite is to prevent other from viewing the file."
CFile can be used to write binary dat not only text:
just use CFile::typeBinary and you will write read binary data
Hi

You can use CFile::typeBinary as in:

CFile file;
file.Open("C:\\whatever.dat",CFile::modeWrite | CFile::typeBinary);
file.Write(lpBuf,nCount);
file.Close();

Hope this steers you in the right direction
Avatar of cwh

ASKER

I tried using the CFile::typeBinary, and write some thing to a file..., but when I open up with NOTEPAD, I can still see what I write.

I want to write some data to a file, and when some body open the file using notepad or other text editor, they see rubbish. Just like when we use fread and fwrite in C. That is what I mean.

Any suggestion? Please help.

cwh
Hi!
of course if you will write strings everybody can read it.
Show your code to write data.
binary fields can be writed by this:
CFile fl (..., CFile::typeBinary);
int nValue = 100;
//
fl.write(&nValue, sizeof(nValue));
Avatar of cwh

ASKER

CFile mPwd(pPwdFile, CFile::modeReadWrite | CFile::typeBinary);    
               
//reading from file
mPwd.SeekToEnd();
mPwd.Read(buf, (int)mPwd.GetLength());
     
sprintf(strTemp,"%d",mc_Pwd.mf_GetAccessLevel());
strBuf+=strTemp;
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetUser()->GetBuffer(mc_Pwd.mf_GetUser()->GetLength()));
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetPwd()->GetBuffer(mc_Pwd.mf_GetPwd()->GetLength()));          
strBuf+=13;
                         
mPwd.Write(strBuf,strBuf.GetLength());    

This is the portion of my code to write to file.

Any problem? Or I should write something else.

Please also suggest any good example in the internet that has password sample.

Please suggest    

cwh
Avatar of cwh

ASKER

dear migel,

If I write code with &strBuf, then how do I read it from the file?

Also, how to write a true carriage return to the file?

Please reply. thanks.

cwh
Hi!
I think you mistarget us.
Do you want crypt your password and userName??

so code can be like this:
// very simple algh just XOR src with magic value
char Buffer[32]; // length of the pwa and user rn;
strcpy(Buffer, m_strPwd);
for (int i = 0; i < sizeof (Buffer); i++)
{
Bufer[i] ^= 0xde;//magic number
}
mPwd.Write(Buffer, sizeof(Buffer) )// always store entire buffer
// for read you can use:
mPwd.Read(Buffer, sizeof(Buffer) )// always store entire buffer
// reconvert it back
for (int i = 0; i < sizeof (Buffer); i++)
{
Bufer[i] ^= 0xde;//magic number
}

m_strPwd =  Buffer;

to write carriage return you must write 2 byte CR and LF:
nPwd.Write("\r\n", 2);
Avatar of cwh

ASKER

THanks for the codes, migel, and it works fine.

I am not trying to mistarget you guys, the true question is, I am trying to find out whether can we write to a file without any encryption, and other people still cannot read the file.

Just like in C, if we use fprintf, people can read the file, but if we use fwrite, nobody can read the file anymore.

Anyway, if there is no such method in MFC, is ok.

cwh
Hi!
Is using fwrite tends to anybody else can`t read this file????
I am surprised!
It isn`t true.
fwrite just writes memory contents to the file if memory contain ASCII chars then ANY can read it after you finish write operation and close the file.
Avatar of cwh

ASKER

but when you open up the file, you see some funny charaters but not the content that you write into the file right?

I mean people can use the same program to read out the pointer, but still people can't see the file by double click the file name in windows explorer, if we write using fwrite, isn it?

The problem of using CFile, as you suggested is, after encryption, people can count number of character you converted, though they might not know what is that. But by using fwrite and fread, other people just simply can't see anything by openning the file from Windows Explorer.

cwh
Hi!
You misunderstood file read write mechanism.
CFile and fwrite (and etc) uses same OS API ! there are no differencies between both methods from OS point of view.
People can`t see anything - it isn`t true. People can see any data in that file if they use advanced file managers(such as Windows Commander and so on) that allow view data in the Hexadecimal format.
So to prevent reading data in the your file you must or compress it or encrypt it during writing.
Good luck!
Avatar of cwh

ASKER

ohh.. now I understand.

Thanks migel, for your effort in explaining to me.

So, in my case, I have to work on the encryption method to make sure that people will not know the number of character entered as password or login name.

Can you suggest what should I do to make sure that number of character for login name and password, is not known even if someone open up the file?

Thank you, migel.

cwh

ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cwh

ASKER

Thanks. ANd this is what I am doing, and the method that I can think of.

THank you so much for you answer. You are certainly an experts!!!