Load file

Started by
4 comments, last by Dharma 22 years, 6 months ago
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..
Warm regardsMario..
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, buffer);<br>}<br><br>this reads eleven strings, allocates space for them.<br><br>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! </i>
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
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
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 &ltiostream>#include &ltstring>#include &ltvector>using namespace std;vector&ltstring> 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
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.
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

This topic is closed to new replies.

Advertisement