Error reading in file in binary mode

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #11
Jul 14th, 2005
That works nicely - I guess rdbuf reads the entire contents in one shot? (If so, it's nice to not to have to do the size calculation and to just use string's dynamic allocation)... How big a file you think I need to try this with to really test it out?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #12
Jul 14th, 2005
This took about 30 seconds for the 1 Meg file. (Made a little change, too.)
#include <iostream>
#include <fstream>
#include <sstream>

int main ()
{
   static const char filename[] = "file.txt";
   std::ifstream file(filename, std::ios::binary);
   std::ostringstream text;
   text << file.rdbuf(); // slurp
   std::cout << "okay" << std::endl;
   return 0;
}

/* my output
C:\Test>"TestPP.exe"
okay

C:\Test>dir file.txt
 Volume in drive C has no label.
 Volume Serial Number is 3BA1-7549

 Directory of C:\Test

07/14/2005  01:08p           1,179,648 file.txt
               1 File(s)      1,179,648 bytes
               0 Dir(s)   7,913,684,992 bytes free
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #13
Jul 14th, 2005
Dave,

I ran it on a SUN box (4 cpu machine), a 1 mb file took 7 seconds in your new way (with the stringstream) and basically 0 seconds in the 'C' way you provided earlier... Guess which one I'm going to stick with ...
14-JUL-05 14:30:47->Program Start, HoffLib version: 2.3.5 JULY 14, 2005
14-JUL-05 14:30:47->STARTING stringstream WAY
14-JUL-05 14:30:54->ENDING 1229678
14-JUL-05 14:30:54->STARTING C/FILE * WAY
14-JUL-05 14:30:54->ENDING 1229678

(1229678 was the size read, just to confirm I'm getting the same data)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #14
Jul 14th, 2005
Yay C! :p

I'm not sure if it would make any noticeable difference, but sometimes the platform-specific functions can be quicker, too. Such as maybe using stat. But maybe even the platform's read may be speedier too. (I remember a contest on another forum a while back.) If speed is a real issue.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #15
Jul 14th, 2005
Speed is definitely an issue as this function is part of my new 'library' that I'm building. The performance difference is HUGE with 3.8 MB...

14-JUL-05 14:41:58->Program Start, HoffLib version: 2.3.5 JULY 14, 2005
14-JUL-05 14:41:58->STARTING stringstream WAY
14-JUL-05 14:42:56->ENDING 3807956
14-JUL-05 14:42:56->STARTING C/FILE * WAY
14-JUL-05 14:42:57->ENDING 3807956
14-JUL-05 14:42:57->SAME
14-JUL-05 14:42:57->Program Completion, Total Runtime: 59 SECOND(S). Return Code: [0]

By the way (somewhat unrelated), is there any way in C/C++ to keep track of milliseconds? the time_t structure only seems to go up to seconds. (Since java has milliseconds, I was hoping there would be a way in C++, but I suspect no..)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Error reading in file in binary mode

 
0
  #16
Jul 14th, 2005
>is there any way in C/C++ to keep track of milliseconds?
No, the smallest portable measurement is a second. However, most systems provide a way to have sub-second measurements. Check your man pages.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #17
Jul 14th, 2005
I primarily use Sun solaris. I'm not even sure what to search for in the man pages...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Error reading in file in binary mode

 
0
  #18
Jul 14th, 2005
How about:
  1. $ man -k second
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #19
Jul 14th, 2005
man -k second
/usr/dt/man/windex: No such file or directory
/usr/man/windex: No such file or directory
/usr/openwin/share/man/windex: No such file or directory


whereas any other command like ,
man whodo
Reformatting page. Please Wait... done
System Administration Commands whodo(1M)
NAME
whodo - who is doing what
SYNOPSIS
/usr/sbin/whodo [-h] [-l] [user]
DESCRIPTION
The whodo command produces formatted and dated output ...etc..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC