ClanKiller.com
http://forums.clankiller.com/

C++ and stuff
http://forums.clankiller.com/viewtopic.php?f=24&t=1267
Page 1 of 1

Author:  Satis [ Mon Jun 06, 2005 3:55 pm ]
Post subject:  C++ and stuff

ok, well, now I have more questions. I've searched online and can't find the answer. Spoke with some friends of mine who took c++ once (a long time ago, in a galaxy far, far away) and they can't remember.

When you open a file, you use a file handle, a la:
Code:
ifstream filelist (myfile.txt);

In this case, my file handle is 'filelist'. I can close the file:
Code:
filelist.close();

but, if I try to open another file using the same handle later on, I get an error.
Code:
"redeclaration of 'class ifstream filelist'"

which implies you can't reuse file handles. This can't be right. There's probably some way to clear the handle entirely so you can reuse it. Anyone know how the hell to do that?

also, I have a question about parsing an input.....

let's say I have a variable along the lines for "blah, blah blah blah, etc etc, blah" and I want to parse it into an array broken up on the commas. In php, I'd just $thevariable = explode(",",$thevariable). What's the best way of doing this in c++? I hacked together a way of parsing it using .find and .substr, but it's not very pretty. However, it does work. Any pointers?

completed code:
(an example line from the file would be 'afilename.asp,c:\program files\etc').
Code:
getline (filelist, readfile, '\n');
if (readfile == ""){
         continue;
}
begin_comma = readfile.find(",",1);
filepath = readfile.substr(begin_comma+1,255);
filename = readfile.substr(0,begin_comma);

Author:  RB [ Tue Jun 07, 2005 8:25 am ]
Post subject: 

One example for reuse of a stream.

Code:
#include <fstream>
#include <iostream>
using namespace std;

main()
{
   ifstream in;
   char c;

   in.open("a");

   while(true)
   {
      in.get(c);
      if(!in) break;
      cout.put(c);
   }

   in.close();   // closing
   in.clear();   // reset flags

   in.open("b");   // opening

   while(true)
   {
      in.get(c);
      if(!in) break;
      cout.put(c);
   }

   in.close();
   
   return 0;
}


The error you get imply that you are trying not just to reuse the stream, but as well to create it again. That is no-no because it alredy exist. I have marked the keyrows with comments. The first, which close the file clear the stream but it is not really ready for the use if its past resource reached EOF or got some error. You have to reset the flags of the stream with the method clear. Then it is as totaly new one and ready for use. You can use the method open to open the new file. :)

It can be noted that

ifstream in("abc");

and

ifstream in;
in.open("abc")

will give the same result.

Now on the parsing. Well, let this be one solution. I hope you do well with the vector template. This is NOT the best solution but it is simplest coming to the mind. Ok?

Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

const unsigned int maxwordsize = 1024;

void f(const char*,char,vector<string>&);

main()
{
   vector<string> rc;
   
   f("abc",',',rc);

   for(int i=0;i<rc.size();i++)
   {
      cout << '[' << rc[i] << ']' << endl;
   }
   
   return 0;
}

void f(const char* fn,char c,vector<string>& v)
{
   ifstream in(fn);
   char buff[maxwordsize];
   unsigned int i=0;

   while(true)
   {
      buff[i] = in.get();
      if(!in) break;

      if(buff[i]==c)
      {
         buff[i] = 0;
         v.push_back(buff);
         i=0;
      }
      else i++;
   }

   buff[i] = 0;
   v.push_back(buff);
}

Function F solve your problem, I belive. Arguments are the filename, char which is parsesymbol and the reference to the vector<string> resource. Just take care that variable maxwordsize be greater than the longest string to be parsed out.

Author:  Satis [ Tue Jun 07, 2005 11:05 am ]
Post subject: 

thanks gfree. The file thing makes total sense to me, good work explaining it. :) The other thing I'll need to pull apart and analyze for a bit, but I think I understand what's going on. If I have any more questions, I'll let you know.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/