#### Archived

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

# (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.

## 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 on other sites
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 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 /* 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 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 on other sites
Stdio.h is all you need to do file i/o, it works in both C and C++. Iostream/Fstream/_Stream.h are just wrappers for it, essentially. I''ll email you a help file that can list the contents of most standard C/C++ libraries.

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

##### 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 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,

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

##### 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,

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

##### Share on other sites
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 on other sites
Cearny, that''s the same book (except I got the version with added info. for Borland) I keep around for reference (i.e. really weird stuff I don''t do often), though I didn''t learn C++ from it.

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

1. 1
2. 2
frob
20
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632210
• Total Posts
3004815

×