I didn't see the issue, but WRT a C version I think it would be something like this.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "rb");
if ( file )
{
char *buffer;
long size;
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
buffer = malloc(size);
if ( buffer )
{
if ( fread(buffer, size, 1, file) == 1 )
{
/* puts(buffer); */
}
}
fclose(file);
free(buffer);
}
return 0;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
First, I rarely start by looking at the compiler; I'd say about 99% of the time it is the code. Just because I didn't see what you saw doesn't mean there wasn't an issue with it.
I think in part what is missing is what is missing. How do you see what you see? Where is some sort of output statement?
One of the reasons I put the puts line in comments is that it probably isn't what you are doing and it's probably not the correct thing to do. And perhaps that is the issue -- on the output side, not the input side.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Hmm. When I run either C or C++ version in the command shell in SlickEdit (my usual location that helps discover things like the need for fflush(stdout) and such) I get a blank area where the text should be. To me this would tell me that I might be missing something. When I run it in a cmd shell this does not happen. [edit]Ah the CR of the CR-LF was erasing the line for the SlickEdit output window. A fallout of binary mode reading.[/edit]
I am using Windows, so I might suspect my version would have issues with the binary vs text modes. But I wouldn't expect such things on Solaris. Outputting one at a time from 0 to size would have been my suggestion, so I'm a little miffed.
What is the "big picture" of what you are trying to do?
[edit2]This is the code I am using.
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include <fstream>
using std::ifstream;
const char filename[] = "file.txt";
int main ()
{
ifstream file(filename, ios::in | ios::binary | ios::ate);
long size = file.tellg();
file.seekg(0, ios::beg);
char *buffer = new char [size];
file.read(buffer, size);
file.close();
for ( long i = 0; i < size; ++i )
{
cout << buffer[i];
}
cout << endl;
delete[] buffer;
return 0;
}
/* file.txt (no newline at end)
SAM LOVES TO TEST FILES
*/
/* my output
SAM LOVES TO TEST FILES
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
A vector of strings for C++?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Another stray thought:
#include <iostream>
#include <fstream>
#include <sstream>
int main ()
{
static const char filename[] = "file.txt";
std::ifstream file(filename);
std::ostringstream text;
text << file.rdbuf(); // slurp
std::cout << text.str() << std::endl;
return 0;
}
/* my output
SAM LOVES TO TEST FILES
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
How about:
$ man -k second
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401