Solved

Readfile Question...

Posted on 2004-04-12
5
352 Views
Last Modified: 2010-04-01
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
Comment
Question by:billcch
  • 3
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
ID: 10806439
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
 

Author Comment

by:billcch
ID: 10806463
Thank you khkremer....
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10806556
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
 

Author Comment

by:billcch
ID: 10806595
hi, khkremer:
is this your email address?
eexchange@khk.net
Thanks!
billcch
0
 

Author Comment

by:billcch
ID: 10807782
Again, Thank you very much khkremer....
Billcch
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

13 Experts available now in Live!

Get 1:1 Help Now