• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Readfile Question...

I am trying to read a file into a string and crypt it, I don't want to use getline (since some of file does not have any specific character such as '\n'), read by char and crypt it is too slow, so I would like read amount of characters into a string, and then just crypt it.
I select 3000 character each time I read from file, my code like below...
CAN SOMEONE TEACH ME MORE EFFICIENT WAY TO DO THIS JOB???

THANK YOU SO MUCH....

//======================================================
// The code is work, but I would like to know if any efficient way to do this job... Thanks
//======================================================
const int AMOUNT = 3000;
......
ifstream ifile;
string filename = "c:\\test.txt";
ifile.open(filename.c_str());
if(!ifile.is_open())
    return;

string str = "";
char c;
while(!ifile.eof())
{
   str = "";
   for(int i = 0; i < AMOUNT && !ifile.eof(); i++)
   {
       ifile >> c;
       str += c;
   }
   doCrypt(str);
}
0
billcch
Asked:
billcch
  • 3
  • 2
1 Solution
 
Karl Heinz KremerCommented:
Use ifstream::read(char *buffer, int size), so in your case this would be

char buffer[AMOUNT];
ifile.read(buffer, AMOUNT);

BTW: This is usually done with a "Points for ..." question, not a completely new question. See the EE Help for more information (http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hi50).
0
 
billcchAuthor Commented:
Thank you khkremer....
0
 
Karl Heinz KremerCommented:
On second thought, you should probably delete this question (have it deleted by the Support team) and reopen the first question (support can also do this for you).
0
 
billcchAuthor Commented:
hi, khkremer:
is this your email address?
eexchange@khk.net
Thanks!
billcch
0
 
billcchAuthor Commented:
Again, Thank you very much khkremer....
Billcch
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now