It is currently Thu Mar 28, 2024 5:22 am



Reply to topic  [ 3 posts ] 
C++ and stuff 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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);

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Mon Jun 06, 2005 3:55 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
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.

_________________
++


Tue Jun 07, 2005 8:25 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Jun 07, 2005 11:05 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.