Solved

FILE MEMORY HANDLING

Posted on 1998-08-27
19
186 Views
Last Modified: 2010-04-10
HI,
I am doing a getline and string compare for a text file usinf ifstream.But after it reads certain number of lines
the application is crashing. Why? If it is a memory problem how do I allocate memory?
0
Comment
Question by:ardney
  • 10
  • 6
  • 2
  • +1
19 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1171343
make sure no lines are longer than the string buffer (array) you are readin into.  Can you post the code?
0
 
LVL 3

Expert Comment

by:xyu
ID: 1171344
You don't need to read the entire file into memory.... If You'll specify more details (Compiler, OS, Libraries used) i can try to suggest the optimal solution
0
 

Author Comment

by:ardney
ID: 1171345
Hi,
At First I was doing like
ifstream ifs;
ifs.open();
while(!eof)
{
      getline(into buffer)
      and string compare;
}
It was reading till 855 bytes only.
Now I am doing like this

  ifstream *ifs;
   ifs = new ifstream[somesize];
   ifs->open("siris.txt",ios::in);                                                    
   cout << "Date" << endl;                            
   cin >> str;
 
   while(!ifs->eof())                                           
   {

            ifs->getline(string,512);
            
               if (ifs->eof())
            {
                  break;
            }
                            string compare;
}
Now it is taking 93799 bytes.
How should I make it a memory independent one?

0
 

Author Comment

by:ardney
ID: 1171346
Hi,
At First I was doing like
ifstream ifs;
ifs.open();
while(!eof)
{
      getline(into buffer)
      and string compare;
}
It was reading till 855 bytes only.
Now I am doing like this

  ifstream *ifs;
   ifs = new ifstream[somesize];
   ifs->open("siris.txt",ios::in);                                                    
   cout << "Date" << endl;                            
   cin >> str;
 
   while(!ifs->eof())                                           
   {

            ifs->getline(string,512);
            
               if (ifs->eof())
            {
                  break;
            }
                            string compare;
}
Now it is taking 93799 bytes.
How should I make it a memory independent one?
Now I am working in Dev Studio. But finally it is to work on UNIX.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171347
Wait a second.  The line

   ifs = new ifstream[somesize];

creates an array of file stream objects.  That is, it creates a number (somesize) of filestream objects.  I doubt you want that.  You seem to use only the first one anyways.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171348
ifstream ifs;
 ifs.open("siris.txt",ios::in);      
const int maxstrlen = 512;
 char string[maxstrlen];

 cout << "Date" << endl;      
 cin >> str;
         
 while(!ifs.eof())
 {
      ifs.getline(string,maxstrlen);
      if (ifs->eof())
         break;
     string compare;
 }

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171349
Now you haven't shown how you are comparing the string or what you are comparing with.  You might have a problem there as well.  The code above will work with lines up to "maxstrlen" (currently set at 512) characters long.  If the file has  lines longer than than that, the lines will be read "in pieces" as if they were broken up into lines shorter than 512.
0
 

Author Comment

by:ardney
ID: 1171350
Hi,
Each line in the file I am presently working is less than 512 .
If I am going to handle one file...May be it is not the correct way of using like this.
ifstream *ifs;
Then how do I do it?

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171351
how?  The way I showed you.  use

ifstream ifs;

etc.  Is there a reason you didn't like that?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ardney
ID: 1171352
The problem is the app is crashing after the file size more than 855 bytes if I use ifstream ifs.
My lines are always less than 512 bytes.
I am doing a string compare only for certain characters in the line using strtok.
Then why is it not crashing till the size 93799 bytes if I use ifstream *ifs;
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171353
I don't know, but I can assure you that using the ifstream * didn't fix anything.  It temporarily covered up the problem.  The problem is still there and will probably resurface again.  It would help if we could see your code.  If it is too large to post, you could e-mail it to me at nietod@theshop.net.
0
 

Expert Comment

by:billcav
ID: 1171354
we have to see how you are using strtoc. It's a tricky function and you might be doing something to your buffer.

0
 

Author Comment

by:ardney
ID: 1171355
char *str;
char *string;

void main( void )
{
   int k =0;
   int i =0;
   int count = 0;
   int index =0;
   int  result;
   str = new char[100];                                                                     
   string = new char[512];

   ifstream *ifs;
   ifs = new ifstream[5000];//some number I am giving.
   ifs->open("siris.txt",ios::in);                                                    
   cout << "Date" << endl;                            
   cin >> str;
 
   while(!ifs->eof())                                           
   {

            ifs->getline(string,512);

               if (ifs->eof())
            {
                  break;
            }
            for ( i=0; i<6; i++)
                  
                  {                                                                          
                        string = strchr(string,',');
                        string++;    
                  }
            string = strtok(string,",");
            result = stricmp(string,str);
            
            if (result == NULL)
                  
                  {
                         index++;
                                                                                
                  }
   
        }
 

   cout <<  str <<  "\t" << index << endl;
   
}  
Now I am using like this. If I use ifstream ifs rather than ifstream *ifs, The app crashes after 855 bytes.
 
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1171356
The code

for ( i=0; i<6; i++)
{    
   string = strchr(string,',');
   string++;    
}
string = strtok(string,",");
result = stricmp(string,str);

looks very dangerious.  It looks for 6 comas on a line.  But if it doesn't find them, it will cause a crash.  I suspect that is what is happening.  You could test that with.

for ( i=0; i<6; i++)
{    
   string = strchr(string,',');
   if (string == NULL)
      cout << "NO COMMA.";
   string++;    
}
string = strtok(string,",");
result = stricmp(string,str);

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171357
A 2nd problem,

You allocate 512 characters for "string", but you use much more than that.  The problem is you don't "reuse"  the space allocated for string on each line of the file.  Instead each line is read into "string" after the previous line.  Before long you will be reading a line in that goes past the 512, characters allocated for "string".  The reason this happens is as follows.  You allocate 512 characters for "string" and get a pointer to the start of these 512 character.  But you don't "permenantly" save a pointer to these 512 characters.  The "string" pointer starts at this spot, but you keep changing it, advancing it past each string read, so eventually it points past the end of the 512 characters.  Make sense?  This is a serious problem.   Also I don't see you deleting this memory.  Another serious problem.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171358
void main( void )
{
         int k =0;
         int i =0;
         int count = 0;
         int index =0;
         int  result;
         str = new char[100];      
         // Linbuf always points to the start of the 512 char array.
         char * LinBuf = new char[512];

         ifstream ifs;
         ifs.open("siris.txt",ios::in);      
         cout << "Date" << endl;      
         cin >> str;
         
   while(!ifs.eof())
   {
      ifs.getline(LinBuf,512);  // Get into LinBuf.
     
      if (ifs.eof())  // This test you put int prevents processing of the last line.  
                             // If you want to process the last line let me know.
      {
          break;
      }
     string = LinBuf;  // Reset string to point to start of the array.  
      for ( i=0; i<6; i++)
      {    
         string = strchr(string,',');
         string++;    
      }
      string = strtok(string,",");
      result = stricmp(string,str);
      if (result == NULL)
      {
           index++;
      }
      // clean-up.
      delete [] str;
      delete [] LinBuf;          
  }
  cout <<  str <<  "\t" << index << endl;
}  


This should be better.  It might not be perfect yet.  Try it and let me know.
0
 

Expert Comment

by:billcav
ID: 1171359
what is the logic here? why are you using strchr() in a loop? assuming there are six commas, you're pointing to the byte after the sixth. as soon as strchr() fails, string == NULL, so string++ == NULL++.

Why aren't you using strtok() the way it was intended?

first call:
string = strtok ( string, ',' ) ;
more calls in a loop:
string = strtok ( NULL, ',' ) ;

0
 

Author Comment

by:ardney
ID: 1171360
Hi Nietod,
It works well. Though My Last will be an empty line, I would like to know how to process the last line.
Thank U,
ardney
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171361
Just remove the if (ifs.eof()) test from the loop.  With it there, on the last line of the file, you read the line, find you are at the end of the file and abort the loop before processing the line.

I suspect it was there because the last line of the file was empty and your code will crash on an empty line.  This is because it will crash on any line that doesn't have 6 coma's  You might want to improve that code to make your program safer.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now