problems using <fstream> (STL)

alissre
alissre used Ask the Experts™
on
Hi %)
I'v writen a VC++6 win32 application with a function that reads  a file:

_____________________
#include <iostream.h>
#include <fstream.h>
....
void readFile()
{
  ...
  ifstream input(fileName);
  char name[51];
  ....
  while((!input.eof())
  {
       input.getline(name,50);
       .........
  }
  ....
  input.close();
}
_____________________

the application ran ok,
But then I wanted to use STL so I replaced the #includes lines with:

__________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
______________________


I didn't touch any other part of the code,
But: while the compilation/linking was still succesfull, the program is now stuck, It doesn't respond

What's the problem ?

thanks in advanced %)

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try changing it to:
while(input.getline(name, 50))
{...}
I'm guess that the last line is sticking it.  Good luck!

Commented:
Need more of your code, to make a determination.

Please post all the code to your function.

Try running the program, and when it gets stuck, break into the code by pressing the break button, or selecting it from main menu Debug->Break

Author

Commented:
The weird thing is that it works ok when I use
#include <iostream.h>
#include <fstream.h>

but it stuck when I use STL

__________________________________________________
anyway, this is the code:

bool modelsManager::readFile(char* fileName,char* e)
{
     char name[51];
     ifstream input(fileName);    
     assert(!input.fail());
     int i=0,j=0,length=0;

     while((!input.eof())&&(i<MAX_NUM_OF_MODELS))
     {
        input.getline(name,50);
        length=strlen(name);
         
        if((length>5)&&
             (strcmp(name+length-5,".ms3d")==0))
        {
           modelNames[i]=new char[length+1];
           strcpy(modelNames[i],name);
           i++;
        }
        else if((length==15)&&(strcmp            
                  (name,"collision check")==0))
        {
          if(j<i)
          {
           if(!((j>0)&&
                (modelNumCheckCollide [j-1]==i-1)))
           {                                      modelNumCheckCollide[j]=i-1;
               j++;
           }
          }
        }
         }


     input.close();
     
     if(i>0)
     {
          numOfModels=i;
          numModelCheckCollide=j;
          return true;
     }
     else
          return false;
}
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Commented:
Can you post the declaration for this class (modelsManager)?

What is MAX_NUM_OF_MODELS, modelNames, and modelNumCheckCollide?

Author

Commented:
Ok, I answerd it allready:
removing "using namespace" declarations and adding "std::" in any place where there is a class that usese the namespace solves the problem.

So here are 2 simple question, the one that anwers each one of the two gets the points:

1)Is there any difference in the functions or the parameters for functions between <fstream> and <fstream.h> classes ?

2) Do you know ***good*** tutorial/help on the web about reading/writing files with <iostream>, <fstream> and their functions ?
Commented:
-Except for the issue that fstream is in namespace std and fstream.h isn't there isn't a difference.

-try http://www.cplusplus.com they got a good section with iostream reference where you find examples.

But there are simple rules of the thumb for the streams.

member good() returns a summary of bool of the error flags of the stream, a stream is also signaling !good() if he has reached the end of the stream.

stream << data   is always eating the data into the stream, not the matter what the stream is. Only the datatype must be supported of the operator << . If it isn't you can easely write your own custom <<.

stream >> data brings the data from stream into a variable. Operator must be supported for this to work.

member open() opens streams.
member close() cloeses them.
ios_base:: contains flags to set for streams.

Author

Commented:
oww, http://www.cplusplus.com is good, thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial