CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2001
    Posts
    8

    ifstream problems with large files

    I am writing an app to read from relatively large log files. Whenever I read from one that is 12MB in size I get an Unhandled Exception Error. If I use a the 2MB files that I have the program has no problem. From what it seems the problem is directly related to the STREAMB1.CPP file that the debugger is trying to load.

    The call stack is as follows :
    streambuf::stossc()
    istream operator>>(char *)

    Is there a max file size when using ifstream and the >> operator? The code I have reading the file is basically this


    char finput[100];
    CString input;
    while (!inFile.eof())
    {
    inFile >> finput; // Dies on this line
    input = finput;
    // Do something with input
    }






  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: ifstream problems with large files

    You're definining finput to be 100 in size.

    if a line is longer than 99characters, it'll write outside of the buffer, corrupting memory, causing the crash.


  3. #3
    Join Date
    Dec 2001
    Posts
    8

    Re: ifstream problems with large files

    But from what I understand >> reads the stuff in a word at a time. Let me try upping the read buffer to something a little large although I don't see any single word that is greater then 99 characters.


  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: ifstream problems with large files

    nah, it'll read stuff out of the file until a linefeed character is detected. it'll read the file a line at a time. Unless each word is on a separate line your assumption isn't correct :-)


  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: ifstream problems with large files

    Why don't you use STL functions and strings to read in the file line-by-line??

    #include <string>
    #include <iostream>
    #include <fstream>

    using std::cout;
    using std::endl;
    using std::string;
    using std::ifstream;

    int main()
    {
    string strLine;

    // Open file
    ifstream ifstrFile("c:\\test.txt");
    if(ifstrFile.is_open() == false)
    return -1; // Could not open file


    // Read line-by-line
    while(getline(ifstrFile, strLine))
    cout << strLine << endl;

    // Close file
    ifstrFile.close();

    return 0;
    }




    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

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