Archived

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

Arrays

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

Ok so this might come as a real newb question, but that''s why this is the beginners forum right? i have been around in programming a lot, made the step over to c++ about a year ago from Visual Basic, which is ofcourse a bad language for 3d coding(cause: memory speed lacks) anyway in Visual Basic, you could predefine an array, dim array(0) as string and later on redefine it, or resize it, how you want to call it.. by making this action: redim preserve array(size you want it) i was wondering, is there anyway to do this in C++? this code(in vb) actually makes it happen that the original array is copied, the new array will be made(the larger one) and the old array content will be placed back, and more arrays are back to fill. i heard something about array=new STRING[num] (tho string is not a variable) but i did not got this working yet?? thanks for your time! Jan

Share this post


Link to post
Share on other sites
hm that was not what i was looking for i think,

what i look for is resizing an array without losing the data,
so that i don''t have to be limited to a starting point of the array..

if you get it

thank you!

jan

Share this post


Link to post
Share on other sites
Really, the clean way to do what you want is to uses std::vector - that works like arrays in VB. An array in C is more primitive.

Here you can resize it by calling realloc() but this may change its position (i.e. invalidate pointers).

In VB when you used the "preserve" keyword and increase the array size a new array is made and the content of the old one is copied to it. In C you can either do this manually or use std::vector to get it done for you.

Share this post


Link to post
Share on other sites
well so i searched, but cannot find a clear exlpaning of the functions and that kind of stuff :S

is there any reference you would prefer me to read?
where it is explained clearly?

thank you

Jan

Share this post


Link to post
Share on other sites
quote:
Original post by felonius
Really, the clean way to do what you want is to uses std::vector - that works like arrays in VB. An array in C is more primitive.

Here you can resize it by calling realloc() but this may change its position (i.e. invalidate pointers).



Realloc() is nasty - like felonius says, you may well move the position of the array thus invalidating pointers. Also, it only works on dynamically allocated arrays.

Ideally, you would either:

a) Make your arrays big enough to store everything they will need to store, right from the beginning.

b) Use std::vector.




Share this post


Link to post
Share on other sites
thank you all
i will try to find the solutions !!


another question, so i won''t post a new topic...

i want to make a small file load loop

and i got everything i needed,
eccept to find out when i am at the end of the file..

is anyone at the knowledge to do this??

Share this post


Link to post
Share on other sites
hm ok sorry for keep on bothering you
i now understand the base of vectors :D thanks for that m8s!!

so now back to the txt file loading
this doesnt work for a reason.. after the first line it already says it is the end of thefile..

#include "stdafx.h"
#include "stdio.h"

void readstr(FILE *f,char *string)

{
do
{
fgets(string, 255, f);
} while ((string[0] == ''/'') || (string[0] == ''\n''));
return;
}


int main(int argc, char* argv[])
{
FILE *filein;
char oneline[255];
bool fileend;

fileend=false;

if((filein=fopen("hello.txt", "rt"))==NULL)
printf("File does not exist");

while(fileend==false){
if(oneline[0]=EOF){
fileend=true;
printf("EINDE FILE
}
readstr(filein,oneline);
printf(oneline);
}

fclose(filein);

return 0;
}

this is the txt file:
hello

bla
blalba


thanks!

Jan
(btw yes a part of it is from the nehe tutorial.. kept on the way they open and read files )

Share this post


Link to post
Share on other sites
I think you should start your debugger and learn to find these errors yourself instead of posting here immediately. YOu will never get better if we tell you the answer everytime.

You wrote:

if(oneline[0]=EOF){

I think you mean:

if(oneline[0]==EOF){

Oh, and remember to check for EOF in readstr() also.

[edited by - felonius on June 6, 2002 8:54:23 AM]

Share this post


Link to post
Share on other sites
well,

actually i am busy on finding the answer for about 4 days now.. i am new on this forum, and i thought if i cannot find it myself the nask..

besides..
the code you gave doesn't work
it keeps repeating the last sentence of the file until i break it by shutting the program down...

[edited by - Fluid FX on June 6, 2002 9:47:56 AM]

Share this post


Link to post
Share on other sites
Hey Fluid,

Here''s a simple example, note syntax has not been checked with a compiler so I may be wrong here in a few places.


#include <cstdio>
.
.
.
char Buffer[1024];
char* FileName = "testfile.dat";
FILE *FilePtr = NULL;

FilePtr = fopen(FileName, "r+w");
if ( NULL == FilePtr )
{
printf("Error Opening File %s", FileName);
exit(1);
}

while( !feof(FilePtr) )
{
fgets(Buffer, sizeof(Buffer), FilePtr);
}

fclose(FilePtr);

Share this post


Link to post
Share on other sites
quote:
Original post by Fluid FX
actually i am busy on finding the answer for about 4 days now..


I apologize. You just posted that question very quickly after the other one so it just seemed that you just had run into the problem and didn''t bother trying to figure it out for yourself.

Share this post


Link to post
Share on other sites
no offence m8.. :-) i mean i could have posted another topic..

thanks for the help, i will check if this works!!

thank you all
sorry to bother you

Cheers
Jan

Share this post


Link to post
Share on other sites
articles & resources section of this site: search "File i/o"

results:
click here

start there. good tuts. everything you ever wanted to know about file i/o in c++

learn to use the Articles & resources section. it is the best source of information for most problems.

-me

[edited by - Palidine on June 6, 2002 4:45:03 PM]

Share this post


Link to post
Share on other sites
i went thro a few some days ago,
i got to nexe.gamedev.net
those are good tutorials for 3d,
but the i/o tutorials are a bit ehm well "undressed" they contain minimized information so you can do the most little..

you see?
so thats why i asked

sorry
tanks for the link!

i will get far with the vector + i/o info!!
till next time...

Jan

Share this post


Link to post
Share on other sites
heh. sorry.

these are also a good set of links:
click here

the first one is particularly good and contains a full example of reading a text file line by line. it''s what i used to get going on File I/O. the rest of the links on that goole page range from somewhat to not at all helpful. they contain some filler info that supports what the first link contains

-me

Share this post


Link to post
Share on other sites
ok so i tried the vector function
it works all fine,
accept for the resize function :/

i have been sitting on it the whole day but i can''t figure it out,

when i do this (i.e.):
struct Point
{
int x,y,z;
};

int i;
Point Points;
main()
for(i=0;i<5;i++){
Points.resize(i);
Points.x = i;
Points[i].y = i;
Points[i].z = i;
}

it produces an error(Tho winxp doesn''t show me which :/)

well thanks if you still wanna help,

Greetz

Fluid FX

Share this post


Link to post
Share on other sites
Ermm, that code is nonsense. You make one point and then try call resize() on it and index it as an array. And I see no declaration of a vector anywhere. I think you posted something wrong.

BTW, vector is not a function, it is a template.

[edited by - felonius on June 7, 2002 9:29:51 AM]

Share this post


Link to post
Share on other sites
sorry
Point Points should be:

vector Points;
it was about the code i wrote but the real one is to big,
also it has the #include files and stuff...

only the resize doesn''t work :''(

Share this post


Link to post
Share on other sites