473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ifstream::read and pointers to std::vector


Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo...73f4d1a05cfbd1
http://groups.google.com/group/sourc...a9b6f91239c909
contain several algorithms including the following ones.

==================================
ifstream ifs; // input file stream
string ret_str;

### CPP-21: std::vector and std::copy
------------------------------*------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ostream_iterator<char> out(oss);
copy (&v[0], &v[v.size()], out);
ret_str = oss.str();
------------------------------*------------------
### CPP-23: std::vector and istream::read()
------------------------------*------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ret_str = (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------*------------------
==================================

Eljay Love-Jensen wrote me in private message:
std::vector does not guarantee that the memory is one contiguous block.
Your particular implementation may-or-may-not provide that guarantee -- but
reliance upon that particular implementation behavior is not portable.


So, my algorithms CPP-21 and CPP-23 are not legal because the following
two line are not legal (?).

vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);

So, we can't use read() with pointers to std::vector (?).
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 10 '05 #1
1 3288

Alex Vinokur wrote:
Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo...73f4d1a05cfbd1
http://groups.google.com/group/sourc...a9b6f91239c909
contain several algorithms including the following ones.

==================================
ifstream ifs; // input file stream
string ret_str;

### CPP-21: std::vector and std::copy
------------------------------*------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ostream_iterator<char> out(oss);
copy (&v[0], &v[v.size()], out);
ret_str = oss.str();
------------------------------*------------------
### CPP-23: std::vector and istream::read()
------------------------------*------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ret_str = (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------*------------------
==================================

Eljay Love-Jensen wrote me in private message:
std::vector does not guarantee that the memory is one contiguous block.
Your particular implementation may-or-may-not provide that guarantee --but
reliance upon that particular implementation behavior is not portable.


as of 2003, the C++ standard explicitly states that a std::vector uses
one contiguous block:

ISO/IEC 14882:2003, section 23.2.4-1:

"The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocator> where T is some type other than bool, then it
obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."
So, my algorithms CPP-21 and CPP-23 are not legal because the following
two line are not legal (?).

vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);

So, we can't use read() with pointers to std::vector (?).
as of 2003, the above code is legal.


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Aug 10 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. Len | last post by:
Hi all! Given that I have a binary file which contains 4-byte integer values in sequence and I know in advance how many there are, does the C++ standard have any algorithm or like method to read...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
1
by: ngaloppo | last post by:
Hi, I sometimes convert std::vector::iterators to pointers by the (afaik) legal construct &v, e.g. in the construct below. In debug build, the program terminates, because of what seems to be...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
2
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.