Skip to main content
gamedev.net
🔒 Locked

Loading text files

Started by cxi2 Nov 28, 2000 at 12:55 PM 12 replies 700+ views
Original Post
cxi2
cxi2
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
to Code, or Not To Code
Dire.Wolf
Dire.Wolf
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
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Dire.Wolf
Dire.Wolf
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
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
cxi2
cxi2
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
to Code, or Not To Code
xtrmntr
xtrmntr
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
Dire.Wolf
Dire.Wolf
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
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Dire.Wolf
Dire.Wolf
BTW Cxi2, did the code I posted work for you (i.e. answer your question)?

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
cxi2
cxi2
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
to Code, or Not To Code
Demitri
Demitri
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.
-Tim Elliot (Demitri)
HitScan
HitScan
you could also try playing with strtok and atoi(and atod, etc) if you want to do the "direct" approach

and if you want to get real cute and change the first coeff by line number then you could use strtok_r and strtok in conjunction, that''ll take some thinking

Take heart, it can be done! (I''ve done something similar with arbitrary string values, if you want I can bust out some sample code for your situation.)

Good Luck!


--
HitScan
rational_rose
rational_rose
file >> i; //int i
file >> ch; //char ch
file >> str; //string str

will stop at any whitespace(space, tab, endline), the data is automatically converted

file.get(ch);

reads only 1 char

getline(file, str);

reads 1 line

getline(file, str, ''*'');

if you want to use a delimiter other than ''\n''

note: you can replace all the "files" with "cin" too if you want to read console-input
murph
murph
Yesterday (as anon poster) I mentioned fopen and scanf. Here is some code to demonstrate what I was (vaguely) talking about.

  
#include <stdio.h>

#define MAP_X 10
#define MAP_Y 10

int main()
{
int map[MAP_X][MAP_Y]; /*map array*/
int x,y; /*position counters for loading map array*/
FILE *fp; /*pointer to the file with map data*/

fp = fopen("io_exmpl.txt","r");/*open file with read-only access*/

if (!fp) return 0; /*if couldn''t open the file,shut program down*/

/*read the text file, convert numeric characters to integers*/
/*and load them into the array*?
for(x = 0; x < MAP_X; x++)
{
for(y = 0; y < MAP_Y; y++)
{
if (fscanf(fp,"%d",↦[x][y]) == EOF)break; /*EOF == -1*/
}
}

/*we''re done with the text file, close it*/
fclose(fp);

/*that''s all you need to load your file into an array of integers*/
/*the code below just allows you to look at array elements*/


int another = 1;

while(another != 0)
{
printf("\nEnter x: ");
scanf("%d",&x);
printf("Enter y: ");
scanf("%d",&y);

if ((x >= 10) || (x < 0) || (y >= 10) || (y < 0))
{
printf("Invalid array position. Shutting it down.\n");
return 0;
}
else
{
printf("\nArray element at this position is %d.",map[x][y]);
printf("\n\nEnter 1 check another position or 0 to quit: ");
scanf("%d",&another);
}
}

return 0;
}


In order to use fopen,fscanf and fclose you need to include stdio.h

Good luck.

--------------------------------
Screw you guys! I''m going home.
--------------------------------Screw you guys! I'm going home.
murph
murph


I guess the message board doesn''t like C style comments.

Sorry ''bout that.

--------------------------------
Screw you guys! I''m going home.
--------------------------------Screw you guys! I'm going home.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.