Advertisement

Loading strings from files?

Started by April 17, 2001 09:03 AM
4 comments, last by MindWipe 23 years, 9 months ago
I''m making an RPG game and I need to load strings from files. I just don''t know how it''s done. I can load itegers using sscanf, that''s easy but how would I load strings. I read a line like this:
  

FILE *filein;
char oneline[255];
filein = fopen("map.txt", "rt");
				
readstr(filein,oneline);

//What should I do here?


	

//readstr looks like this:

void Map::readstr(FILE *f,char *string)
{
	do
	{
		fgets(string, 255, f);
	} while ((string[0] == ''/'') || (string[0] == ''\n''));
	return;
}

  
In my map.txt it looks like this: Name: NAMEOFTHEMAP How would I get NAMEOFTHEMAP into "char * MapName"? I really need help on this one? PS. also loading names with more then one word into one char* would be really great but not nescessary /MindWipe "If it doesn''t fit, force it; if it breaks, it needed replacement anyway."
"To some its a six-pack, to me it's a support group."
%s should work with fscanf, but if not compare each new read letter to ''\n'' or EOF. I think you are just comparing the first letter of the string to null. Try fgetc and then keep adding a char onto the end until the char == null

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
instead of using the old C way of reading files, you could use the easier C++ way.
    //standard input/output#include <iostream.h>//file input/output#include <fstream.h>//strings#include <string.h>//or, if you are using Borland#include <cstring.h>int main(){   ifstream ins;   string buffer;   ins.open("File.txt");   ins<<buffer;   cout>>buffer;//then store the buffer however you want...//don't forget to close the file when you are all done   ins.close();   return 0;}    


ins<<buffer will read in a string until it hits whitespace (space or newline).
cout>>buffer will print the string.
if you use the cstring.h file, you will have to use the cout to print it, printf will not work.

Are you even trying to be intelligent?
'cuz if you are, it ain't workin'
Down with Tiberia!

Edited by - capn_midnight on April 18, 2001 2:18:50 PM

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Um, prolly the best way, is to use ifstreams like cap'n midnight said, but instead of just using the << operator, do it like this:

char input[80];
ins.get_line( input, 80 );

this will give you the entire line (80 characters) or it will stop earlier if there is a return. You can optionally add a 3rd parameter to stop it at a space, or at a ';' (comments?) etc. If you like I can send you some more code... just mail me.

freshya.

[EDIT]sorry I didn't mean the using strings is bad, but if you want to use standard char arrays, which is what MindWipe used in his example, then that is the best way... And if you have a standard null terminated string, then you can use it for all sorts of things, like printf, and GDI drawing[/EDIT]

Edited by - freshya on May 2, 2001 4:24:25 AM
In my map.txt it looks like this:

Name: NAMEOFTHEMAP

Here''s what I''d do.

  FILE *fp = fopen("map.txt","r");char oneLine[255];//read the linefgets(oneLine, 255, fp);//get rid of the ''\n'' at the end of the lineoneLine[strlen(oneLine)] = ''\0'';//get a pointer to the occurance of the character '':''char *temp = strchr(oneLine, '':'');//advance the temp to point to NAMEOFTHEMAPtemp += 2;//copy temp into mapNamechar mapName[255];strcpy(mapName, temp);  


That ought to work. I think...

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
My general solution to reading or writing any container (including strings) to/from files is this:
write the number of items in the container
write the container

For instance:
ostream& operator << (ostream &os, const string &str){  os << str.size (); // output length of string  os.write (str.c_str (), str.size ()); // write data  return os;}istream& operator >> (istream &is, string &str){  int size;  is >> size; // read-in length of string  str.resize (size); // set string size  is.read (str.begin (), size); // read-in string  return is;} 

This will work in general for most containers. You might want to be careful about the null character at the end of string, not sure if it needs to be appended manually in the istream operator >>.

This topic is closed to new replies.

Advertisement