Jump to content

  • Log In with Google      Sign In   
  • Create Account

Load file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Dharma   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 09:42 AM

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

Sponsor:

#2 davekerr   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 10:01 AM

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!


#3 phueppl1   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 10:11 AM

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


#4 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 19 October 2001 - 10:22 AM

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

#5 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 19 October 2001 - 10:25 AM

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.

#6 Bezzant   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 12:00 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS