Jump to content
  • Advertisement

Archived

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

Tsu

(insert nifty title here)

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

say, for instance, i have a text file: ------- start text file contents ------ 100 200 300 400 500 ------- end text file contents -------- i also have an array of, for example, 5 integers (we''ll call it list[]). my question is, how to i insert the numbers from the text file into the array, so that list[1] = 100, etc. The c/++ book i have doesnt cover how to get anything else but strings and characters out of a text file... i suppose i could translate the text file''s numbers into characters, and then just convert again (to / from ascii code or something) to read them, but thats alot of work... is there an easier way? The way i have it set up is, it reads in about 6 different text files, and assigns values to arrays based on that data. the program reads each line (of each text file) and inserts the data it finds (up until it reaches a new line) into a string. _________
Tsutomegi

Share this post


Link to post
Share on other sites
Advertisement
I think you mean list[0]=100
File i/o in C++ is easy. Just do something like this:
ifstream infile("file.txt",ios::in);
infile >> list[0];
Simple as that. Put it in a loop to read in all the numbers.


==================
/* todo: insert cool sig */
Martee
Magnum Games.NET

Share this post


Link to post
Share on other sites
Here's the C way to do it (stdio.h is often faster, and always smaller than iostream.h and fstream.h and any other stream.h file you would have to include):
    
#include <stdio.h>

/* code */

int list[5];
FILE *fp = fopen("myfile.txt","rt")
if(fp!=NULL) {
for(int a=0; a<5; a++) {
fscanf(fp,"%i",&list[a]);
}
fclose(fp);
}

/* code */



There you go

[EDIT: fixed a typo]



http://www.gdarchive.net/druidgames/


Edited by - Null and Void on January 14, 2001 9:04:34 PM

Share this post


Link to post
Share on other sites
Thanks...
The list of commands in the book i have doesnt include fscanf... that will help...

re: I think you mean list[0]
Yeah, i was thinking of strings in turbo pascal(Where the first digit is stringname[1]...) (we''re learning about them in computer science class for the past week)

What library do i need to include for those to work? just stdio.h (or the c++ equivilent(I dont remember it right now...))

_________
Tsutomegi

Share this post


Link to post
Share on other sites
Thanks again...

_________
Tsutomegi


EDIT: Fixed Spellering mistake (i spellered that wrong on purpose)

Edited by - tsu on January 15, 2001 2:13:46 PM

Share this post


Link to post
Share on other sites
I believe that you should buy a better book The one I'm recommending (and I believe that everyone agrees with me) is "C++: The Complete Reference" by Herbert Schildt. It's the best book you'll get, and it covers alot of things, including binary/text file I/O.

HTH,
Adrian

Edited by - cearny on January 15, 2001 2:41:25 PM

Share this post


Link to post
Share on other sites
BTW, does anyone know how to solve this problem:


#include

class foo
{
float bar;
};

foo* my_foos[500];

int main()
{
FILE* fi = fopen("INPUT.TXT", "r+t");

for( int i=0; i<500; i++ )
my_foos = new foo;

// this is where the problem lurks...
fscanf(fi, "%f", &my_foos[0]->bar);

fclose(fi);

return(0);
}


Can I solve ths without using an auxiliary variable to hold the data?

TIA,
Adrian



Edited by - cearny on January 15, 2001 2:52:12 PM

Share this post


Link to post
Share on other sites
Your not allocating your memory correctly. Instead of :
for( int i=0; i<500; i++ )
my_foos = new foo;

You should forget about the for loop and put:
my_foos = new foo[500];

Or you can use the C funtion malloc.

-SirKnight

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!