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
The purpose of writing using fread and fwrite is to prevent other from viewing the file.
Please suggest.
cwh
Hi
You can use CFile::typeBinary as in:
CFile file;
file.Open("C:\\whatever.da t",CFile:: modeWrite | CFile::typeBinary);
file.Write(lpBuf,nCount);
file.Close();
Hope this steers you in the right direction
You can use CFile::typeBinary as in:
CFile file;
file.Open("C:\\whatever.da
file.Write(lpBuf,nCount);
file.Close();
Hope this steers you in the right direction
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
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));
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));
ASKER
CFile mPwd(pPwdFile, CFile::modeReadWrite | CFile::typeBinary);
//reading from file
mPwd.SeekToEnd();
mPwd.Read(buf, (int)mPwd.GetLength());
sprintf(strTemp,"%d",mc_Pw d.mf_GetAc cessLevel( ));
strBuf+=strTemp;
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetUser ()->GetBuf fer(mc_Pwd .mf_GetUse r()->GetLe ngth()));
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetPwd( )->GetBuff er(mc_Pwd. mf_GetPwd( )->GetLeng th()));
strBuf+=13;
mPwd.Write(strBuf,strBuf.G etLength() );
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
//reading from file
mPwd.SeekToEnd();
mPwd.Read(buf, (int)mPwd.GetLength());
sprintf(strTemp,"%d",mc_Pw
strBuf+=strTemp;
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetUser
strBuf+='\t';
strBuf+=(mc_Pwd.mf_GetPwd(
strBuf+=13;
mPwd.Write(strBuf,strBuf.G
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
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
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);
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);
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
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.
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.
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
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!
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!
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!!
THank you so much for you answer. You are certainly an experts!!!
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