Thread: Little help with reading a file.

  1. #1
    C++ Newbie
    Join Date
    Jul 2005
    Location
    Plovdiv, Bulgaria
    Posts
    11

    Little help with reading a file.

    Hello, I'm having troubles figuring out how to make my program display only sertain parts of a file. Here's what I mean:
    The file I lead is this:
    Code:
    14x80
    %
    Home
    Syntax:
    [LET]variable = value
    Description:
    The asignment operator assigns the variable its left the value of the express
    ion on its right. The equal sign symbol is used both for assignment, and cond
    itional equivalence operator. To avoid confusion the optional LET keyword may
     be used.
    &index&
    #
    
    %
    index
    blah blah this is another text
    #
    So, what I need to do is load the the file but display only the first part of it between the % and the # symbol. I can't think of a way to do it, any ideas will be appreciated.
    to explain the first line of code, it's what tells the program the size of the file.

    Here is the code I use for loading and dysplaying the file:
    Code:
    int LoadMap(char mapFile[])
    {
    	ifstream file(mapFile,ios::in);
    	if(!file)
    		return -1;
    	char * sizestr = new char[10];
    	file.getline(sizestr,10,'\n');
    	string strsize = sizestr;
    	delete[] sizestr;
    	
    	MAPHEIGHT = atoi(strsize.substr(0,strsize.find("x")).c_str());
    	strsize.erase(0,strsize.find("x")+1);
    	MAPWIDTH = atoi(strsize.c_str());
    	
    	int ycounter = 0;
    	while(ycounter < MAPHEIGHT)
    	{
    		file.getline(map[ycounter],1000,'\n');
    		ycounter++;
    	}
    	file.close();
    	return 0;
    }
    
    void DrawMap(int mapheight, int mapwidth)
    {
    	color(FWI|BNULL);
    	for(int ycounter = 0; ycounter < mapheight; ycounter++)
    	{
    		gotoxy(2,ycounter+1);
    		for(int xcounter = 0; xcounter < mapwidth; xcounter++)
    		{
    			if(map[ycounter][xcounter] == '#')
    			{ycounter=mapheight;}
    			else
    				cout << map[ycounter][xcounter];
    		}
    	}
    }
    Thanks in advance.

    --aroticoz

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by aroticoz
    display only the first part of it between the % and the # symbol
    You can just use "getline" the version for use with the "string class". Just use the '%' and '#' as delimiters. You could also use "getline" with 'x' to get the numbers to.
    Code:
    #include <fstream>
    #include <iostream>
    #include <ostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream file(mapFile,ios::in);
    	if(!file)
    		return -1;
    
    	string astring;
    	getline(file,astring,'x');
    	string astring2;
    	getline(file,astring2);
    
    	int number1;
    	int number2;
    
    	stringstream ss;
    	ss << astring;
    	ss >> number1;
    
    	stringstream ss2;
    	ss2 << astring2;
    	ss2 >> number2;
    
    	string strsize1;
    	getline(file,strsize1, '%');
    	file.ignore();
    
    	string strsize2;
    	getline(file,strsize2, '#');
    
    	cout << strsize2 << endl;
    	cout << "number1 = " << number1 << endl;
    	cout << "number2 = " << number2 << endl;
    	//file.close();
    	return 0;
    }

  3. #3
    C++ Newbie
    Join Date
    Jul 2005
    Location
    Plovdiv, Bulgaria
    Posts
    11
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM