Advertisement Jump to content
Sign in to follow this  

txt files and arrays

This topic is 4906 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

There are many ways to go about this, we need to know the format of the txt file but here is a one general method:

#include <iterator> // std::istream_iterator
#include <vector> // std::vector
#include <fstream> // std::ifstream

std::ifstream ifs("foo.txt");

std::vector<int> v((std::istream_iterator<int>(ifs)),

Share this post

Link to post
Share on other sites
This is what the file contains. there is whitespace surrounding
the block of numbers. This has to be without vectors. That is still
really new to me.
11111111111111111111111111111111111111111111111111111111111111111111111 10000000000000000000000000000000000000000000000000000000000000000000001 10000000000000000000000000000000000000000000000000000000000000000000001 10011110011001110011111111001111111111111000000000000000000000000000001 10011110011001110000000000001111111111111000000000000000000000000000001 11111110011001110000000000001111111111111000000000000000000000000111111 00000000000001110011100011000000000000000000000000000000000000000000000 00000000000001110011000011000000000000000000000000000000000000000000000 11111100111001110011000011001111100001111000000000000000000000000111111 10000000011001110011110011001000000000001000000000000000000000000000001 11111000011001110011110011001000000000001000000000000000000000000000111 00111001111001110011000011001000000000001000000000000000000000000000100 11111001111001110011000011001111111111111000000000000000000000000000111 10000000111001110000000000000000000000000000000000000000000000000000001 10000000111001110000000000000000000000000000000000000000000000000000001 11111100000001111111110011001111111111111000000000000000000000000000111 00001100000001111111110011001111111111111000000000000000000000000000100 11111100111000000000000000001111111111111000000000000000000000000000111 10000000000000000000000000001111111111111000000000000000000000000000001 10000000000001111111110011000000111110000000000000000000000000000000001 11111110011001111111110011000000111110000000000000000000000000000111111 00000000011001110000000011001100000000011000000000000000000000000000000 00000000011001110000000011001100000000011000000000000000000000000000000 11111110011001111111110000001111111111111000000000000000000000000111111 10011110011001111111110000001111111111111000000000000000000000000000001 10000000000000000000000011000000000000000000000000000000000000000000001 10000000000000000000000011000000000000000000000000000000000000000000001 11111111111111111111111111111111111111111111111111111111111111111111111

Share this post

Link to post
Share on other sites
Do you want to put it into some kind of 2D array? and do you want to the read it in as it is or ignore 0s or 1s along the way? what do 0s & 1s represent i assume tiles of a tile map or something similar.

Share this post

Link to post
Share on other sites
I assume each individual 0 or 1 represents a tile; in that case, you will want to read individual characters, skipping whitespace characters "as you go". The formatted IO operations are a lot friendlier for reading space-separated items; we can't read this into individual numbers with >> into an int, because the boundaries between numbers aren't present.

1) Space out the values.
2) Remove all the spaces and returns from the file completely, and use something like snk_kid's snipped, except with istream_iterator<char> instead of istream_iterator<int>. This still puts things into a 1d array rather than a 2d array. (I assume you originally intended that each "line" of the file is a "row" of the "map" array :/ ) It also means that the characters will be interpreted as, well, characters: thus, the 0s will be read as '0', which is actually 48 (learn your ASCII).
3) Leave the spaces as is, and parse things more carefully. One way is to build a vector of vectors of int: read a line at a time, and find the digits in each line, storing them in a vector of int; then put all of those 'rows' into the final nested vector.
4) Use a binary file instead. However, you'll probably end up with a chicken and egg problem like this, because binary files aren't terribly easy to construct "from scratch".

#3 is what I'd recommend at this point, and it looks something like this:

// Your homework: look up what headers are needed, check the namespacing
// and do general debugging. ;)

string fileName = getTheFileNameAlready();
vector<vector<int> > theMap;
ifstream file(fileName.c_str());
// Now we have an open file. While it is possible to read lines of the file...
string currentLine;
while (getline(file, currentLine)) {
// ... loop over the line, and store 1s and 0s as we encounter them.
vector<int> currentRow; // so we have storage space.
for (string::iterator it = line.begin(); it != line.end(); ++it) {
// '*it' gives the current character in the string.
switch(*it) {
case '0': // read a character 0; put a number 0 into the vector.
case '1': // Similarly for 1s.
default: break; // ignore any other character.
// Now that we've stored all the 0's and 1's into the current "row", append
// that row to the map.

This approach will accomodate a file with non-rectangular map data (i.e. it doesn't care if all the rows are the same length) - that could be a bad thing; maybe you'd prefer to report an error in that case, and there is some extra space overhead in storing the data using the nested vectors (each row is represented by a vector object, which adds a bit of its own data for information like "how long is this row?". Also, the vectors may over-allocate the space used for speed-optimization reasons - this is OK for a plain vector normally, but gets worse as you nest them). However, there is also the clear benefit here that you don't have to indicate the map dimensions ahead of time, as you would with arrays (unless you basically go about re-implementing std::vector - badly).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!