Reading in from a text flatfile.

This is a discussion on Reading in from a text flatfile. within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program that will allow for scripters to plug in text flatfile "modules" to create an RPG. Basically ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Reading in from a text flatfile.

    I'm writing a program that will allow for scripters to plug in text flatfile "modules" to create an RPG. Basically an RPG engine. The biggest problem I am having at this point in time is reading in from the flatfile to run inside of my engine.

    Where I am stuck is moving the file position pointer to the beginning of the line that I need to read data in from. This position will not be static to a file, so I have to be able to dynamically read it.

    For example, here's the (broken) code that I have right now:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    char* GetInfo(int varname);
    
    int main()
    {
    	GetInfo('HP');
    
    	cout << "\n\n";
    
    	return(0);
    }
    
    char* GetInfo(int varname)
    {
    	char stream[256];
    
    	fstream file("test.txt", ios::in | ios::out | ios::nocreate | ios::noreplace);
    	file.ignore(65535, varname);
    	file.getline(stream, 32,'|');
    	file.close();
    
    	return(stream);
    }
    stdlib will be for the atol() command so I can convert some strings to ints when I have to.

    Here's the flatfile that I am trying to read from:

    Code:
    MONSTER
    Name | Wyvern
    HP | 27 | 2 | 5
    MP | 0 | 0
    AGI | 10 | 1
    CHR | 10 | 1
    INT | 10 | 1
    STA | 10 | 1
    STR | 10 | 1
    WIS | 10 | 1
    XP | 120 | 50
    GP | 40 | 10
    Head | Helmet | 1
    Torso | Belt | 3
    Arms | Sleeves | 2
    Legs | Breeches | 2
    Feet | Boots | 1
    What I would like to do is to be able to pass the name of the variable I want (such as "HP") to the GetInfo function, and have it read through the flatfile for that identifier. Then be able to move over past the delimiter and the space (maybe with seekg?) to read the line information.

    Any advice?

    Thank you in advance.



    Edit: The code I am posting is from a seperate module than the RPG itself. I'll integrate it into the actual code when it works.
    Last edited by Lithorien; 08-01-2004 at 10:17 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Quick guess, but shouldn't this be a string

    GetInfo("HP");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Salem
    Quick guess, but shouldn't this be a string

    GetInfo("HP");
    Putting that in fails completely, as in, won't even compile.

    error C2664: 'GetInfo' : cannot convert parameter 1 from 'char [3]' to 'int'

    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    I can't enclose varname in 's, because then it will be considered a literal string and not a variable name. This is where I'm getting confused. I can't pass a string, so I try to pass a constant. It gets recast as an "int" type and then my program can't match to the char* that it pulls from the flatfile -- If I understand what's happening correctly.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well yeah, you have to fix some other stuff as well.

    > file.ignore(65535, varname);
    But since this only ignores single characters, and not strings, I think you need a whole different approach.

    Personally, I would just use getline() to read each line in turn, then decide what to do about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The thing is 'HP' does not produce an int it produces an error. You are only allowed to have one character in a character literal.
    I would suggest accepting a std::string instead of an int and then if you need to convert to an int then do so inside your function.
    Last edited by Thantos; 08-01-2004 at 10:47 AM. Reason: I R Gud spelr

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Salem
    Personally, I would just use getline() to read each line in turn, then decide what to do about it.
    Ok.. I tried this approach. Here's the code I'm coming up with:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* GetInfo(char* varname);
    
    int main()
    {
    	GetInfo("HP");
    
    	cout << "\n\n";
    
    	return(0);
    }
    
    char* GetInfo(char* varname)
    {
    	char stream[256];
    	char* name;
    	int HP;
    
    	fstream file("test.txt", ios::in | ios::out | ios::nocreate | ios::noreplace);
    	file.getline(stream, 64);
    
    	file.getline(stream, 64, '|');
    	file.seekg(1, ios::cur);
    	
    	file.getline(stream, 64);
    	name = stream;
    	cout << name;
    
    	file.getline(stream, 64, '|');
    	file.getline(stream, 64, '|');
    	HP = atol(stream);
    	cout << HP;
    
    	file.close();
    
    	return(stream);
    }
    So essentially, there's no way for me to be able to adjust to users making flatfiles in different formats? There's no way to do an if (strcmp(string, "HP")) { ... } setup, since that always fails. I can't make it not fail.

    I mean, I can probally deal with arbitrary flatfiles, I'd just rather not have to program this interpertation code for every single flatfile type I have set up is all.

    Thanks for the help, though.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    MONSTER
    Name | Wyvern
    HP | 27 | 2
    MP | 0 | 0

    To search the above file for HP and assign 27 to one variable and 2 to another you could do something like this:

    Code:
    char userInput[80] = "HP";
     
    //declare stream to read file and associate stream with file
    //don't use nocreate, noreplace
    ifstream fin("fileToUse");
     
    //check if stream opened and ready for use
    if(!fin)
    {
      cout << "unable to open file" << endl;
      exit(EXIT_SUCCESS);
    }
    //read in first line
    char fileName[80];
    cin >> fileName;
     
    //read in second line
    char temp[80];
    char ch;
    char userName[80];
    fin >> temp >> ch >> userName;
     
    //scan the next two lines for HP. It can only be in the first position of either line
    int i = 0;
    int dummy;
    int variable1, variable2;
    while(i < 2)
    {
      //read in first string from this line
      fin >> temp;
     
      //compare temp with user input
      //if they are the same collect the rest of the line
      //if not discard the rest of the line and go to the next line
      if(strcmp(userInput, temp) == 0)
    	fin >> ch >> variable1 >> ch >> variable2;
      else
    	fin >> ch >> dummy >> ch >> dummy;
    }
     
    //now either leave fin open, if you want to read the rest of the file, or close it if you are done using it or whatever.
    now you can use the information in variable1 and variable2 as desired which have been loaded with information stored in the file based on whatever userInput is.
    Last edited by elad; 08-02-2004 at 03:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text
    By Anator in forum C++ Programming
    Replies: 33
    Last Post: 01-30-2008, 11:13 AM
  2. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 11:27 AM
  3. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 11:51 AM
  4. Reading text file and structuring it..
    By Killroy in forum C Programming
    Replies: 20
    Last Post: 11-19-2004, 07:36 AM
  5. Reading Tab Separted Text files
    By Cathy in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21