delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/28/01:27:51

Message-Id: <199804280525.BAA00183@mail.cgocable.net>
From: "Glen L. Bowes" <bowes AT mail DOT cgocable DOT net>
To: djgpp AT delorie DOT com
Date: Tue, 28 Apr 1998 01:25:17 +0000
MIME-Version: 1.0
Subject: ifstream & repositioning file pointers
Reply-to: bowes AT cgocable DOT net

Hi,

Would someone be kind enough to explain how to reposition a file file
pointer in C++ when using ifstream? I'm trying to write a class to
deal with .ini type files as below (very incomlete!!):

#include <fstream.h>
#include <stdlib.h>
const int LINE_SIZE = 100;

class fio
{
 public:
    fio(char* filename);
      int getNumSec()                                   {return
      secCount;}
      void findSec(char*); 
      void findVar(char*, char*);
   private:
    ifstream infile;
      int secCount;
};

fio::fio(char *filename)
{
 infile.open(filename);
      if(!infile)
   {
    cerr << "Could not open " << filename << ", aborting... ";
      exit(1);
   }

   secCount = 0;
   char buffer[LINE_SIZE];
 while(infile)
   {
    infile.getline(buffer, LINE_SIZE);
      if(buffer[0] == '[')
       secCount++;
   }
}

void fio::findSec(char* secName)
{
 char buffer[LINE_SIZE];
   bool found = false;
   while(infile && found == false)    // <-- Problem here - file
   pointer {
    infile.getline(buffer, LINE_SIZE);
      int size_left = strlen(secName) - 2;
      if(buffer[0] == '[')
      {
       int count;
         for(count = 0; count < size_left; count++)
         {
          if(!(buffer[count] == secName[count]))
             found = false;
            else
             found = true;
         }
      }
   }
   if(found == true)
    cout << endl << "found \"" << secName << " \"";
   else
      cout << endl << "\"" << secName << "\" not found or bad code"; }

void fio::findVar(char* secName, char* varName)
{
   fio::findSec(secName);
   char buffer[LINE_SIZE];
   while(infile)
   {
    infile.getline(buffer, LINE_SIZE);
      if(buffer[0] == '[')
      {
       cout << "Variable " << varName << " not found in this section "
              <<  "aborting ";
         exit(1);
      }
   }
}

void main()
{
 fio test("fiotest.ini");
 cout << "Number of sections: " << test.getNumSec();
   test.findSec("test section");
//   test.findVar("test section", "test1");
}



Many many thanks in advance!

Glen L. Bowes

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019