• Advertisement

Archived

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

(insert nifty title here)

This topic is 6247 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
i think you may have misinterpreted his intent, SirKnight, or i have...

what i think you want to do is start your own thread on GD about the topic, heh, instead of stepping on TSU''s toes

anyway, try this instead

for( whatever)
myfoos = new foo;

cuz

T* Name[x]

creates x pointers to type T, each one referenced by
Name[i].

dont forget to delete them all, either!

for( whatever )
delete mfoos[i];


as for TSU''s topic, I recommend the books by BJARNE STROSTRUP. I have one of them, and they''re great. He''s the guy who WROTE c++. really, he was the first to make it.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have no name that you may call me. I am merely Succinct.
~Succinct Demos Online~

-(Drop me a line here n''at)-

Share this post


Link to post
Share on other sites
Well, you were both wrong, and I was too.

I wrote the code incorrectly, I ment
      my_foos[i] = new foo      
(what else the loop for??? just to eat up processor time??? ). And it still doesn't work (I'm using Borland C++ 3.1 dor DOS (need it in CS class)). Just try it!

And I'm not trying to take over anyone's thread, ok? >

UPDATE: The problem was that I didn't realize that the forum app. will change my post from
  [i]  
to italic (forum code... )

Edited by - cearny on January 16, 2001 1:54:28 PM

Share this post


Link to post
Share on other sites

  • Advertisement