#### Archived

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

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

## Recommended Posts

I cant find any books or articles on how to do this simple task. i want to load text file data into an array. the text file looks like this: 1 2 3 4 4 4 4 4 4 4 4 4 2 3 3 4 4 4 4 4 3 4 4 4 4 4 4 4 4 4 4 4 4 3 4 4 4 4 4 4 basically just a number, then space, then number. I want the numbers to go into an array like this: array[1][1] = 1 array[1][2] = 2 array[1][3] = 3 array[1][4] = 4 array[1][5] = 4 array[1][6] = 4 etc... Its for reading a tile map into an array. does anyone have some sample code, or a place where I can get sample code for this??? thanks guys to Code, or Not To Code

##### Share on other sites
Hey there,

You don''t mention what programming language you are using. If you are using C/C++ like alot of the folks here then you can use fopen to open the text file and fscanf to read it. fscanf will also format the numeric characters into integers if that is required. I don''t have a c/c++ manual on me right now but hopefully this can point you in the right direction.

Once again this is only useful if you are using c/c++. If you aren''t post again and be a little more specific. Someone will help you out. People in this forum love to show off... uhh... help each other out that is.

By the way you look at programming books online at informit.com for free.

Good luck.

##### Share on other sites
If I understand what you want to do try this:

  #include #include int main(){ using namespace std; const int MAX_XBOUND = 10; const int MAX_YBOUND = 10; // declare and initialise the array to 0's int array[MAX_XBOUND][MAX_YBOUND]; memset(array, 0, sizeof(array)); // open the file fstream myfile("C:\\textfile.txt", ios::in); // read the values from the text file using the whitespace as // a delimiter if(!myfile.fail()) { for(int i = 0; (i < MAX_XBOUND) && !myfile.eof(); ++i) { for(int j = 0; (j < MAX_YBOUND) && !myfile.eof(); ++j) { myfile >> array[j]; } } } // close the input file myfile.close(); // print out the values for(int i = 0; i < MAX_XBOUND; ++i) { for(int j = 0; j < MAX_YBOUND; ++j) { cout << array[i][j] << " "; } cout << endl; } return 0;}

Dire Wolf
direwolf@digitalfiends.com

Edited by - Dire.Wolf on November 29, 2000 11:55:16 AM

##### Share on other sites
ARGH!

The ubb code changed something in my source code:

myfile >> array< i >[ j ];

myfile >> array[ i ][ j ]; // note the square bracket around the i

Hope this doesn't get buggered up too

Dire Wolf
direwolf@digitalfiends.com

Edit - The stupid ubb code did change this post too.

Edited by - Dire.Wolf on November 28, 2000 3:07:20 PM

##### Share on other sites
I am actually using VC++ thanks for the help guys I still dont understand exactly how you tell the system "dont read in the spaces, but take each number in between each space"..

or am i just extemely dull here????

to Code, or Not To Code

##### Share on other sites
Hello,

To "tell" the system to stop when it sees a whitespace or end of line character just do this:

file.open();

int j;
file >> j;

file.close();

using the >> operator will automatically stop when it encounters either character... test it out reading in integers and even names to get comfortable with it.

I''m in between classes right now and ran into this just last night. Sorry if there are some errors but im kinda rushing it, i''ll modify this if i found out differently.

good luck

yesterday is history, tomorrow is a mystery, today is a gift... -TapRoot

AIM screen name: StarCraftIsEvil

##### Share on other sites
No you are not extremely dull

Streams can be a complex topic in C++. There are many options and formatting parameters that many people do not know about.

By default, the extraction operator >> automatically reads information from a stream and stops at the first whitespace. This is a common mistake many new C++ programs make when trying to read input from the console (aka Dos-prompt)

std::string mystring;
cin >> mystring;

Now if I was to type: "Hello, World" while this program is running mystring would contain "Hello,". If you were to call cin >> mystring again, mystring would then contain "World".

Dire Wolf
direwolf@digitalfiends.com

##### Share on other sites
BTW Cxi2, did the code I posted work for you (i.e. answer your question)?

Dire Wolf
direwolf@digitalfiends.com

##### Share on other sites
ok ok now i understand ( slaps head) it treats whitespace as end of line cool

sweet now how do i tell the code:

" read in the data as a integer type NOT as a character type"

because I want it to be the number 45 not 4 and 5 in different elements....

to Code, or Not To Code

##### Share on other sites
Have you tried storing the values in binary, or is that even an option? If so, it would be MUCH easier.

For this, you are going to need to read in the numbers in a character array, then convert them to integers using the atoi function.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×