can't read int from file

Started by
13 comments, last by vaneger 14 years, 9 months ago
Quote:Original post by vaneger
how will it effect the compiled code? I don't know much about how compilers use various types of loops.


I can't answer that because I'm not familiar with how compilers work to generate object code.

I suggested it because it's a more direct way of expressing what you're already expressing.
Advertisement
Quote:Original post by vaneger
how will it effect the compiled code? I don't know much about how compilers use various types of loops.


The code outputted by the compiler will most likely be the same for this example.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Quote:Original post by vaneger
*** Source Snippet Removed ***

Thats the finished function. It reads a kilobyte at a time, but can be adjusted via sizeOfRead.

Any suggested improvements ?



Depending on the intended use of this function you may want to:

Change it to accept an istream instead of manually opening the file each time.

Validate that the stream has all the data you are attempting to read: sizeOfRead * sizeof(int) + offset.

Reserve memory in the vector before beggining the looping push_backs. (vector.size() + sizeOfRead). This will avoid multiple reallocations and copies in the vector.

clear the vector before use, unless the intent is to append to the vector.

Consider using iterators, between the file stream and the vector all operations can be done via iterators so that your function doesn't car what type of containers it is working with. You can then use things like std::copy. I do not know if you will still need to do the reserve call or not or if the std::copy effeciently resizes the output iterator's matching class. I, honestly do not use the stl very often.
// Full Sail graduate with a passion for games// This post in no way indicates my being awake when writing it
Some suggestions:

// Return your data with the return value of the function.// Modern compilers have a pretty good idea how to optimize this for vectors.// Use std::string to represent text data, such as file names.// Be explicit with variable names: "name" doesn't say what is named.// Think about the order of default arguments: if the user passes 3 arguments,// it makes more sense to interpret the 3rd as the number of values, rather than// the offset, agreed?// We can generalize the approach for any primitive data type.template <typename T>std::vector<T> get_values_from_file(const std::string& filename, int how_many = 256, int offset = 0) {	std::vector<T> result;	T value;	// Specifying ios::in is redundant for an *i*fstream.	ifstream myfile(filename.c_str(), ios::binary);	myfile.seekg(offset);	// As mentioned, use a for loop.	for (int i = 0; i < how_many; ++i) {		// Use C++-style casts. Don't make assumptions about type sizes		// if you don't have to.		myfile.read(reinterpret_cast<char*>(&value), sizeof(T));		vec.push_back(value);	}	// There is no need to .close() explictly; the destructor does that.	return result;}
for the most part ill be reading a kilobyte at a time, and then handling the other 1023 or less bytes at the end of the file seperately. I plan to make sure if stream will only have a given amount of data prior to calling this function.

Once I get a single threaded version working I may attempt to make it multithreaded, but that is a ways down the road.

This topic is closed to new replies.

Advertisement