Advertisement

(insert nifty title here)

Started by January 14, 2001 05:36 PM
11 comments, last by Tsu 24 years ago
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
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
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
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
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
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/
Thanks again...

_________
Tsutomegi


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

Edited by - tsu on January 15, 2001 2:13:46 PM
Advertisement
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
[ Libraries - STLport | boost | SDL | wxWindows ]
[ Manuals - MSDN | STL Docs ]
[ Compilers - VS.NET | MingW | DJGPP ]
[ Editors/Tools - EditPlus 2 | Anjuta | Dev-C++ ]
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
[ Libraries - STLport | boost | SDL | wxWindows ]
[ Manuals - MSDN | STL Docs ]
[ Compilers - VS.NET | MingW | DJGPP ]
[ Editors/Tools - EditPlus 2 | Anjuta | Dev-C++ ]
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
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/

This topic is closed to new replies.

Advertisement