Jump to content
  • Advertisement

Archived

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

Dharma

Load file

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

What would you suggest as a simple way of loading strings from a file into names[][] ? I''m working on a simple highscore table in "dos", got the sort and saving routine working but I fail when trying to load the names. Any suggestions? I''m saving the names like this and it works fine
    

    for (count = 0; count < 11; count++)
	fprintf(out, "%s\n", namn[count][0]);


    
Thanks in advance. Mvh Mario..

Share this post


Link to post
Share on other sites
Advertisement
what you might need to do is allocate space for the names first, altough looking at your source, you seem to have done this. assuming you have code that looks like this:

char names[11][16]; /* eleven names, up to 16 characters */

then you can rely on the fact that you have 176 bytes of text in the file, and that can be loaded all in one go, by doing this:

fread(&names[0][0], sizeof(char), 11 * 16, pTheFileYouAreUsing);

this will read the charecters straight into your array in one go.

howvever, if you have variable length names, like

char* names[11]; /* eleven POINTERS to string */

you can''t simply load them up from the file, because there is no space for them. if your using c++, do this:

char buffer[255];
for(int i=0; i<11; i++)
{
fscanf(pSomeFileOrOther, "%s\n", buffer);
names = new char[strlen(buffer)];
strcpy(names[i], buffer);
}

this reads eleven strings, allocates space for them.

if this doesn''t work, try using the ''string'' class from the standard library, it is so well written, it can be FASTER than char* strings!

Share this post


Link to post
Share on other sites
CString is well written, still it won''t come up to the speed of normal char *. (The standard library for string functions is well written too :p)... So, I think it depend''s on what''s more confortable to somebody.. It''s char * for me :o)

cya,
Phil


Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states

Share this post


Link to post
Share on other sites
quote:
Original post by davekerr
if this doesn't work, try using the 'string' class from the standard library, it is so well written, it can be FASTER than char* strings!


Even better, use the STL first .

// note that these are C++98 headers!
#include <iostream>
#include <string>
#include <vector>

using namespace std;
vector<string> names;
ifstream file;

void ReadNamesFromFile(char *filename)
{
char temp[80]; // this should be the max line length
file.open(filename);
int j = 0;
while(!file.eof())
{
file.get(temp, 80);
names[j++] = tmp; // increments counter after statement.
}
}


Edit: counter, pointer.

Edited by - Oluseyi on October 19, 2001 5:30:41 PM

Share this post


Link to post
Share on other sites
quote:
Original post by phueppl1
CString is well written...

CString is part of MFC; I don''t think that''s what davekerr was talking about.

CString does have certain very nice features that I wish STL string had, though.

Share this post


Link to post
Share on other sites
dunno if this is what your after, but this is the method i generally use.....

  
void ReadData(char Filename[20])
{
std::ifstream DATA(Filename);
if(DATA)
{
for(int x=0; x<MAX_SCORES; x++)
{
std::string data;
DATA >> Score[x]
}
}
else
{
cout<<"could not load score data...";
}
}



might not be the best way, probably isnt, but hey! i works for me.

Alan

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!