Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

FILE MEMORY HANDLING

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
ardney
Asked:
ardney
  • 10
  • 6
  • 2
  • +1
1 Solution
 
nietodCommented:
make sure no lines are longer than the string buffer (array) you are readin into.  Can you post the code?
0
 
xyuCommented:
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
 
ardneyAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ardneyAuthor Commented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
ardneyAuthor Commented:
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
 
nietodCommented:
how?  The way I showed you.  use

ifstream ifs;

etc.  Is there a reason you didn't like that?
0
 
ardneyAuthor Commented:
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
 
nietodCommented:
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
 
billcavCommented:
we have to see how you are using strtoc. It's a tricky function and you might be doing something to your buffer.

0
 
ardneyAuthor Commented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
billcavCommented:
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
 
ardneyAuthor Commented:
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
 
nietodCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now