Archived

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

2COOL4-U

fstream

Recommended Posts

For my project I''ve written data to a file, only containing numbers from 0 to 5 seperated by a space and I want to read all numbers in it and put it in an array. The text file contains 150 lines each with 150 numbers with a space between two numbers. Like this: 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 I want to read it and put it back into an integer array. Does someone know how, thanks. Jeroen "2COOL4-U" de Haas - DefenceAlliance Project Leader/Coder - TacticalOps Linux Beta Tester

Share this post


Link to post
Share on other sites
this should work, its probably not the best method but should be ok to use...

    
void ReadData(char ReadDataFilename[20])
{
std::ifstream DATA(ReadDataFilename);
if(DATA)
{
for(int i=0; i<MAX_DATA; i++)
{
std::string data;
DATA >> Num[i];
}
}
else
{
cout<<"could not load data..."<<endl;
}
}


then in your main function call ReadData("filename.dat") (or whatever you called it)

if this is not the best way for the task, please tell me, as i use it often.

thanks

EDIT: if you want to put the data back into a 2d array: Num[x][y] (or whatever) just add another 'for' loop and so then you have:
  
for(int i=0; i<MAX_DATA_X; i++)
{
for(int o=0; o<MAX_DATA_Y; o++)
{
std::string data;
DATA >> Num[i][o];
}
}

that should work, probably wont... ho hum!


Edited by - Bezzant on October 20, 2001 12:08:57 PM

Share this post


Link to post
Share on other sites
It works! thx I am using it for a map system for my RTS: http://2cool.free.fr/rts.html

Jeroen "2COOL4-U" de Haas
- DefenceAlliance Project Leader/Coder
- TacticalOps Linux Beta Tester

Share this post


Link to post
Share on other sites