🔒 Locked
Loading text files
Advertisement
Original Post
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
If I understand what you want to do try this:
That should help you out.
Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on November 29, 2000 11:55:16 AM
|
That should help you out.
Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on November 29, 2000 11:55:16 AM
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
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
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
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
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
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
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
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
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
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
Yesterday (as anon poster) I mentioned fopen and scanf. Here is some code to demonstrate what I was (vaguely) talking about.
In order to use fopen,fscanf and fclose you need to include stdio.h
Good luck.
--------------------------------
Screw you guys! I''m going home.
|
In order to use fopen,fscanf and fclose you need to include stdio.h
Good luck.
--------------------------------
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.
Advertisement