| | |
Error reading in file in binary mode
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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)
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)
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.
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
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..)
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..)
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..
/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..
![]() |
Other Threads in the C++ Forum
- Previous Thread: int to vector<>::iterator
- Next Thread: View all running processes, C++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






