Solved

CFile to open stream file

Posted on 2001-06-29
14
763 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:cwh
  • 7
  • 6
14 Comments
 
LVL 12

Expert Comment

by:migel
ID: 6237898
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
0
 

Expert Comment

by:Fortress_Initiative
ID: 6238451
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
0
 

Author Comment

by:cwh
ID: 6243367
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
0
 
LVL 12

Expert Comment

by:migel
ID: 6243812
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));
0
 

Author Comment

by:cwh
ID: 6247185
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
0
 

Author Comment

by:cwh
ID: 6251013
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
0
 
LVL 12

Expert Comment

by:migel
ID: 6251448
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);
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:cwh
ID: 6258051
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
0
 
LVL 12

Expert Comment

by:migel
ID: 6258473
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.
0
 

Author Comment

by:cwh
ID: 6263730
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
0
 
LVL 12

Expert Comment

by:migel
ID: 6263882
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!
0
 

Author Comment

by:cwh
ID: 6264106
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

0
 
LVL 12

Accepted Solution

by:
migel earned 150 total points
ID: 6264691
Hi!
you can use fixed size buffer for both:
for example allow it be 128 byte size
you can fill this buffer by random chars and put in the middle pwd string (let it lie at 80 byte offset from the start).
then use XOR operation and store entire buffer.
So people can see that your data file is 128 bytes long but it will not shure that all 128 bytes is password.
0
 

Author Comment

by:cwh
ID: 6267450
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!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now