Jump to content
  • Advertisement

Archived

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

cxi2

Loading text files

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

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


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

    

#include <iostream>
#include <fstream>

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<i>[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;
}



That should help you out.

Dire Wolf
direwolf@digitalfiends.com

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

Share this post


Link to post
Share on other sites
ARGH!

The ubb code changed something in my source code:

The line that reads:

myfile >> array< i >[ j ];

should read:

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!