type = 1!
file = "gun.bmp"!
effect = 30!
type = 2!
file = "ammo.bmp"!
effect = 20*
🔒 Locked
File reading
Advertisement
Original Post
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: 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
//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,
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).
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:
Code (not tested):
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 20Code (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.
Advertisement