Skip to main content
gamedev.net
🔒 Locked

File reading

Started by Axesor 7 replies 1k views
Original Post
Axesor
Axesor
I know reading files is simple. My file I want my game to read is a bit more complicated. Here's what the file looks like:

type = 1!
file = "gun.bmp"!
effect = 30!
type = 2!
file = "ammo.bmp"!
effect = 20*


I need a program that will read the first line and search for "type = %d" and it will copy the number after the '='. The ! means that is all for the first line and it needs to skip down and search for the word "file = " and the program will copy don the file location. It will once again skip down to "effect = " and do the same thing. I need the program to keep looping until it hits the symbol '*', then it exits the app. Does anyone know how to do this or has a better way of doing so? Help is very much appreciated. Axesor
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Tjaalie
Tjaalie
//VarsFILE* File = NULL;char  Buffer[200];bool  Read = true;int   IntData = 0;//Open the fileFile = fopen("Script.txt", "r");if (!File){  return false;}//Loop trough the filewhile (Read){  //Read var type  fscanf(File, "%s", Buffer);  //Check witch type  if (strcmp(Buffer, "type") == 0)  {     SaveName(Buffer);          //Skip the '='     fscanf(File, "%s", Buffer);     //Read data     fscanf(File, "%i", &IntData);     //Exit?     fscanf(File, "%c", Buffer);     if (Buffer[0] == '*')     {        Read = false;     }  }}//Close the filefclose(File);


I hope this helps, I quickly put it togetter and there might be some bugs.

Tjaalie,
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
Axesor
Axesor
Thank you so much. I hope it works. I'll try it out later.
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
giveblood
giveblood
if your file is well-organized, there is no real reason to have the ! or to do searches. If you can count on everything being in the same locations every line, then why go through the trouble of "searching" for it?
-Chris
Tjaalie
Tjaalie
No problem mate, I found out this morning that my rating was getting lower and lower so I tough let me help this poor guy. Lets hope I did :D
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
jkleinecke
jkleinecke
It almost sounds like you are trying to use a very primitive scripting language. You might try looking into one of the free scripting libraries out there, or if you want, look into lexing and parsing text (or just use lex and yacc).
-----------------------------------Indium Studios, Inc.
Axesor
Axesor
I'm using C++ and OpenGL.
The file will change alot, so I need it to be like that.
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Axesor
Axesor
Unfortunately, this is the result:
1

1
"lgrenade.bmp"
90
"lgrenade.bmp"
2
"lgrenade.bmp"
2
"hbuster.bmp"
20
"hbuster.bmp"
Press any key to continue

Not what I expected :)
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Zahlman
Zahlman
If you're using C++, you should (a) really use C++, and (b) design your file format in a way that's easy to deal with from C++.

I will assume the "field names" are arbitrary, and expect a file with two "words" per line. I will use a blank line to separate "records", and store each "record" as a pair of maps: one from string ("name") to int ("value"), and one from string to string. I will interpret the value as an int if possible and a string otherwise.

Input:
type 1file gun.bmpeffect 30type 2file ammo.bmpeffect 20


Code (not tested):

#include <vector>#include <map>#include <string>#include <algorithm> // std::pair is in here IIRC#include <sstream>#include <iostream>typedef std::map<std::string, std::string> str_data;typedef std::map<std::string, int> num_data;typedef std::pair<str_data, num_data> record;typedef std::vector<record> record_set;record_set parse(istream& is) {  string line;  str_data current_strs;  num_data current_ints;  std::vector<record> result;  while (getline(is, line)) {    stringstream ss(line);    string key;    string value_str;    int value_num;    if (!(ss >> key)) {      // blank line      result.push_back(std::make_pair(current_strs, current_ints));      current_strs.clear();      current_ints.clear();    } else {      if (ss >> value_num) {        current_ints[key] = value_num;      } else {        // couldn't read an int; store as string instead        ss.clear(); // allow reading again (clear error flag)        ss >> value_str;        current_strs[key] = value_str;      }    }  }  if (!current_strs.empty() || !current_ints.empty()) {    // handle leftover stuff at end of file    result.push_back(std::make_pair(current_strs, current_ints));  }  return result;}int main() {  record_set parsed = parse(ifstream("foo.txt"));  for (int i = 0; i < parsed.length(); ++i) {    record r = parsed;    std::cout << "RECORD " << i + 1 << "\nTextual fields:" << std::endl;    for (str_data::iterator it = r.first.begin(); it != r.first.end(); ++it) {      std::cout << it->first << " : " << it->second << std::endl;    }    std::cout << "Numeric fields:" << std::endl;    for (num_data::iterator it = r.second.begin(); it != r.second.end(); ++it) {      std::cout << it->first << " : " << it->second << std::endl;    }  }}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.