Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FILE MEMORY HANDLING

Posted on 1998-08-27
19
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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 200 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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