• Advertisement

Archived

This topic is now archived and is closed to further replies.

Quick string question

This topic is 5832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the best way to read individual characters from a file and append them to a null-terminated string (i.e. build up a string as you go). Or, what is the best way to read a 0 terminated string from a file? Abs

Share this post


Link to post
Share on other sites
Advertisement
You can also use fstream.getLine() function. It reads in a line and inserts into a char array, which you can then manipulate.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
All good suggestions. "Best" depends on what you are looking for. There is a time vs complexity tradeoff. fread is fastest, reading arbitrarily-sized blocks of data; fgets reads up to a newline (text files only); fscanf reads and parses data, and is the slowest. If speed is paramount, then use fread and interpret the data yourself. If speed is not an issue, fscanf requires the least amount of supporting code.

If absolute speed is very important, stay away from the C++ I/O classes. They are from 10%-60% slower than their C counterparts.

Share this post


Link to post
Share on other sites
quote:
Original post by OldGuy
If absolute speed is very important, stay away from the C++ I/O classes. They are from 10%-60% slower than their C counterparts.

Simply not true. The truth is that many well-established C programmers simply have not wrapped their heads around C++ I/O stream classes. In a functionality/efficiency tradeoff, the difference between Standard C I/O functions and C++ stream I/O is marginal at best - certainly not the 10 - 60% picture you paint.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
The advantage of the C++ I/O routines is that they provide a higher level abstraction on top of the low level I/O routines. They are easier to use, handle more file/data types transparently, and better fit the OO model.

The advantage of the C I/O routines is that they are closer to the OS-specific layer. They do not have any object overhead, and are faster in almost every circumstance. Their downside is that they are harder to use.

If the I/O portion of your code is small, then I agree that the difference is neglibable. However, for programs that read and write a lot of data, I stand by my 10-60% difference.

Being religious about anything, even C++, can blind you to the realities of the everyday tradeoffs in programming.

Share this post


Link to post
Share on other sites
quote:
Original post by OldGuy
The advantage of the C I/O routines is that they are closer to the OS-specific layer. They do not have any object overhead, and are faster in almost every circumstance. Their downside is that they are harder to use.

The truth about the C++ I/O stream classes is that they also have low-level, unformatted functions (get, put, read, write) which essentially mirror the C fread, fwrite, fputs, fgets, fputc, fgetc functions. I conceded that the C functions are slightly more efficient, but I contine to contend the 10-60% assertion.

Being religious about anything, even C, can blind you to the realities of the everyday tradeoffs in programming.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
So, we agree that using the right tool at the right time is best.

I was manager for a 300,000 line C++ project in the area of distributed databases. It used remote batches for data synchronization, and could involve many users and transactions. When we profiled the code, we found a large amount of time in the I/O routines. When we rewrote the key portion in ''C'' I/O routines, we improved throughput by between 40% and 60%, depending on data. MSVC++ 6.0.

When you program in the real world, you can''t afford to be too dogmatic.

Share this post


Link to post
Share on other sites
quote:
Original post by OldGuy
So, we agree that using the right tool at the right time is best.

Absolutely. I agreed with and confirmed your general assertion from the get-go; I just objected to the figures.

quote:
I was manager for a 300,000 line C++ project in the area of distributed databases. It used remote batches for data synchronization, and could involve many users and transactions. When we profiled the code, we found a large amount of time in the I/O routines. When we rewrote the key portion in ''C'' I/O routines, we improved throughput by between 40% and 60%, depending on data. MSVC++ 6.0.

Totally. C++ is still not optimum for truly low-level applications (eg system-level components); it''s one of the things that is being discussed for C++0x.

quote:
When you program in the real world, you can''t afford to be too dogmatic.

True dat!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
I love it when a simple two sentence questions spawns a heated debate. I am writing a 3DS loader btw and it wasn''t too difficult to get one that works. Now I want one that works quickly. I am writing an RTS so the plan is to export my models in 3DS format and load them into a custom tool that sets up the correct ortho-projection, lighting, etc... and then renders them from all views and spits out a custom formatted bitmap (instead of going into PS and cutting/pasting/pixel level editing). Fun stuff! Now, if I can add a little keyframer to it I could automate the entire process.

Abs

Share this post


Link to post
Share on other sites
quote:
When we rewrote the key portion in ''C'' I/O routines, we improved throughput by between 40% and 60%, depending on data. MSVC++ 6.0.

That could be a bad iostream implementation, incorrect setting of buffers, or all sorts of things; there is no good reason to see such a drop in performance from using C++ streams.

Of course, if you were really concerned about I/O performance, you''d use neither C++ nor C I/O routines, you''d use Win32 and overlapped I/O.

Share this post


Link to post
Share on other sites
BTW, the best way to put stuff from a stream into a string is to use the "getline()" function in the string header.

This is *NOT* istream.getline() I am talking about, either.

It is the best mechanism for doing line-oriented I/O in C++.

Share this post


Link to post
Share on other sites
I think a lot of people use iostreams badly, and then claim they are so slow as a result. There are several layers to the iostreams system: the << and >> operators, the get/put/write unformatted functions, and accessing the stream buffer directly. Performance-critical code can use one of the last two methods to improve performance significantly such that it is barely distinguishable from C code.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
How do u go about large text files? they seamed very simple in VB but in C++ its geting down to char arrays and so if you had a 3 page essay would you have to have something like:
char essay[5000]
Or something?

Share this post


Link to post
Share on other sites
Large files?

ifstream theFile("i_am_big.txt");
ostringstream inBuffer;
inBuffer << theFile.rdbuf();
string theFileAsAString(inBuffer.str());

If you want that as a char*, call theFileAsAString.c_str();

And no... this is not necessarily the quickest way to read in a file, but it''s pretty good for 4 lines of code. You might even be able to combine lines 2 and 3 into "ostringstream inBuffer(theFile.rdbuf());".

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
quote:
Original post by elis-cool
How do u go about large text files? they seamed very simple in VB but in C++ its geting down to char arrays and so if you had a 3 page essay would you have to have something like:
char essay[5000]
Or something?


Re-read previous posts to this thread (particularly my own), please, and understand why you are incorrect.

Share this post


Link to post
Share on other sites
quote:
That could be a bad iostream implementation


I''ve heard this argument alot. Can you suggest an implementation that works properly on MSVC and is as quick as using C I/O. There may be bad iostream implementations, but most of the compiler supplied versions do seem considerably slower.

Share this post


Link to post
Share on other sites
quote:
Original post by Sorensen
[quote]That could be a bad iostream implementation

I''ve heard this argument alot. Can you suggest an implementation that works properly on MSVC and is as quick as using C I/O. There may be bad iostream implementations, but most of the compiler supplied versions do seem considerably slower.
I don''t use VC++ 6 any more. I use VC++ 7 (hurrah) and haven''t found any area where its iostreams are measurably slower than C I/O.

To answer better, I would have to know what particular code was causing you a significant slowdown.

Share this post


Link to post
Share on other sites
quote:
I use VC++ 7 (hurrah) and haven''t found any area where its iostreams are measurably slower than C I/O.


You could be right here. I hadn''t tried any comparisons on VC++.NET, but after doing some quick tests it does seem much quicker than VC++ 6.0.

Share this post


Link to post
Share on other sites

  • Advertisement