?
Solved

CFile to open stream file

Posted on 2001-06-29
14
Medium Priority
?
859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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 600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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