473,779 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream::read and pointers to std::vector


Testsuites "Comparativ e 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_byt es);
ifs.read(&v[0], no_of_file_byte s);
ostream_iterato r<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_byt es);
ifs.read(&v[0], no_of_file_byte s);
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_byt es);
ifs.read(&v[0], no_of_file_byte s);

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 3319

Alex Vinokur wrote:
Testsuites "Comparativ e 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_byt es);
ifs.read(&v[0], no_of_file_byte s);
ostream_iterato r<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_byt es);
ifs.read(&v[0], no_of_file_byte s);
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_byt es);
ifs.read(&v[0], no_of_file_byte s);

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
16929
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 them into a std::vector container with a single command? Right now I'm using a temporary variable and push_back(), but this is bit clumsy way IMO: int tmp; std::vector<int> someVector; std::ifstream in("somefile", std::ios_base::in |
12
11666
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 currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include <iomanip> #include <fstream> #include <iostream>
32
69696
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: std::vector<int> fun(){ //build the vector v; return v; }
1
2843
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 part of the new checked iterators in VC8. OTOH, I thought that the code is perfectly legal. Is it safe to temporarily disable _HAS_ITERATOR_DEBUGGING here? I feel bad about just shutting up the compiler. Thanks!
20
5152
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 to do it word by word. I am confused as to how to do this. Typically, I would think it would make sense to try and input the words into strings, but for this application I need to use character arrays and pointers. So what's the best way to go...
2
1519
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
5519
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 filled with a tab delimited list of 10 columns (floats) and read it into a 2D array. The length of the columns are all the same, however this will be variable from text file to text file. Any help (starter code or where to read) would be much...
15
5871
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 its coontents into a vector of strings, storinig each line as separate
6
5716
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.