CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    236

    Arrow Is there a way to read from a file, without delcaring a massive bufffer?

    Hello everyone..

    currently i have the following code that seems to work fine from reading the lines of a text file and storing that information in an array named filebuffer. THe problem is, I made the length of the file buffer 1000 characters. Because if the user enters more data I don't want it to just stop working and get a bad stack area because of it.

    So is there a way to figure out the length of the text file bofore hand and allocate that much memory into the file buffer rather than just making a guess?

    Here is my code:
    Code:
    // Buffers
    
    	char LocalPart[1000];
    	char Domain[1000];
    	char FileBuffer[10000];
    
    
    fstream file_op("C:\\EmailTest.txt",ios::in);
    	while(file_op.getline(FileBuffer,1000))
    	{
    		if(IsValidEmail(FileBuffer))
    			Parse(FileBuffer,LocalPart,Domain);
    		else
    			cout <<"0: " << FileBuffer << endl;
    	
    		if(IsLocalValid(LocalPart) && IsDomainValid(Domain))
    			cout <<"1: " << FileBuffer << endl;	
    		else
    			cout <<"0: " << FileBuffer << endl;
    
    	}
    		file_op.close();
    }



    Thanks!
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there a way to read from a file, without delcaring a massive bufffer?

    Quote Originally Posted by voidflux
    So is there a way to figure out the length of the text file bofore hand and allocate that much memory into the file buffer rather than just making a guess?
    Just append onto a std::string for each line read.
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void foo()
    {
        // Buffers
    
        char LocalPart[1000];
        char Domain[1000];
    
        fstream file_op("C:\\EmailTest.txt",ios::in);
        std::string fbuf;
        std::string FileBuffer;
        FileBuffer.reserve(10000);
    
        while(getline(file_op, fbuf))
            FileBuffer += fbuf;
    
          if(IsValidEmail(fbufall.c_str()))
                Parse(FileBuffer.c_str(),LocalPart,Domain);
          else
    	   cout <<"0: " << FileBuffer << endl;
        //....
         file_op.close();
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2004
    Posts
    236

    Re: Is there a way to read from a file, without delcaring a massive bufffer?

    Thanks for the help Paul!

    I was alittle confused on a few things though,

    This line:

    Code:
        while(getline(file_op, fbuf))
            FileBuffer += fbuf;
    is the 2nd argument of getline fbuf just saying your using fbuf to store whatever is in file_op?

    And FileBuffer.reserve(10000) is this saying, well we will reserve 10,000 character placeholders but if you don't need them we will terminate with a '\0'? and free the space but if we need 10000 we'll use it?

    And
    Code:
    FileBuffer += fbuf
    I'm assuming this is just taking whatever fbuf read from the file and storing it now into the FileBuffer string.


    all my functions use char *x parameter data types, so is
    Code:
    FileBuffer.c_str()  converting the string into a c string?

    Thanks alot for the help that looks much nicer!

    oops one last question...
    Why would you need to include <vector> btw?
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there a way to read from a file, without delcaring a massive bufffer?

    Quote Originally Posted by voidflux
    Thanks for the help Paul!

    I was alittle confused on a few things though,

    This line:

    Code:
        while(getline(file_op, fbuf))
            FileBuffer += fbuf;
    is the 2nd argument of getline fbuf just saying your using fbuf to store whatever is in file_op?
    It gets a line of data from the file, just like the getline() function you were using before. Each line is then appended on the FileBuffer string.
    And FileBuffer.reserve(10000) is this saying, well we will reserve 10,000 character placeholders but if you don't need them we will terminate with a '\0'?
    No. What it does is reserve memory for the appending to FileBuffer. The 10,000 is the number of bytes to reserve before having to get memory from the heap again. It is used to speed up the processing of the append.
    And
    Code:
    FileBuffer += fbuf
    I'm assuming this is just taking whatever fbuf read from the file and storing it now into the FileBuffer string.
    The operator is +=, not just =. So it appends onto the FileBuffer string, not just assign.
    [quote]all my functions use char *x parameter data types, so is
    Code:
    FileBuffer.c_str()  converting the string into a c string?
    The c_str() returns a const char * representation of the string that is null-terminated.
    oops one last question...
    Why would you need to include <vector> btw?
    You don't need to include it, it's there by mistake.

    Also, I would suggest getting a C++ book, since all of this is discussed in a good book. The code I have posted is nothing more than using the C++ standard library.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2006
    Posts
    42

    Re: Is there a way to read from a file, without delcaring a massive bufffer?

    As an alternative, you can get the file-size like this, after you opened the file:
    Code:
    file_op.seekg(0, ios_base::end);
    int length = is.tellg();
    is.seekg (0, ios::beg);
    After that, you can "new" a buffer to hold the whole file or mmap it into memory for very easy and fast access.

  6. #6
    Join Date
    Sep 2004
    Posts
    236

    Re: Is there a way to read from a file, without delcaring a massive bufffer?

    thanks for the help guys!
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured