CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jun 2011
    Posts
    17

    Crash Reading & Writing More Files

    Hi. I have a program that reads from a couple large files and extracts select smaller files. The code below was working until I went from 22 extractions to 56. And this number may increase in the future.

    At first, I thought one (or more) of the new entries had an error. However, the program works when I removed either the original half or the new half of entries.

    Since it crashes with all entries, this leads me to believe that the problem is the volume of extractions and that the code needs to be optimized in some way.

    I tried added pauses between extractions, but it still crashes immediately upon running. So I'm not sure if that indicative of something else.

    Here is a snippet of the code...

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main(){
    
    //Still crashes if pause is before all this code.
    //std::cin.get();
    
    ifstream infile001 ("Source1.xxx", ifstream::binary);
    ofstream outfile001 ("001.yyy", ofstream::binary);
    infile001.seekg(0x2a00000);
    char buffer001[15000];
    infile001.read (buffer001,15000);
    outfile001.write (buffer001,15000);
    outfile001.close();
    infile001.close();
    
    ifstream infile002 ("Source1.xxx", ifstream::binary);
    ofstream outfile002 ("002.yyy", ofstream::binary);
    infile002.seekg(0x3f00000);
    char buffer002[27000];
    infile002.read (buffer002,27000);
    outfile002.write (buffer002,27000);
    outfile002.close();
    infile002.close();
    
    //And so on...
    
    return 0;
    
    }
    I've also tried making a extract call out of this code, but haven't been successful. Unless the code to fix this is more complex than the above code, I'm okay with not making an extract call as long as everything works. Any help is appreciated. Thanks!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    1. How does it crash?
    2. What is the total size of data you have read in the moment of crash?

    3. Would something change if you updated your code like
    Code:
    ifstream infile001 ("Source1.xxx", ifstream::binary);
    ofstream outfile001 ("001.yyy", ofstream::binary);
    infile001.seekg(0x2a00000);
    {
          char buffer001[15000];
          infile001.read (buffer001,15000);
          outfile001.write (buffer001,15000);
    }
    outfile001.close();
    infile001.close();
    and so on...
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Crash Reading & Writing More Files

    You're running out of stack space. Either make a function to process the files or wrap each file section in braces {}

  4. #4
    Join Date
    Jun 2011
    Posts
    17

    Re: Crash Reading & Writing More Files

    Thanks for the replies. Unfortunately, after trying both suggestions on using braces, running the compiled file still crashes.

    Code:
    ifstream infile001 ("Source1.xxx", ifstream::binary);
    ofstream outfile001 ("001.yyy", ofstream::binary);
    infile001.seekg(0x2a00000);
    {
          char buffer001[15000];
          infile001.read (buffer001,15000);
          outfile001.write (buffer001,15000);
    }
    outfile001.close();
    infile001.close();
    Code:
    {
    ifstream infile001 ("Source1.xxx", ifstream::binary);
    ofstream outfile001 ("001.yyy", ofstream::binary);
    infile001.seekg(0x2a00000);
    char buffer001[15000];
    infile001.read (buffer001,15000);
    outfile001.write (buffer001,15000);
    outfile001.close();
    infile001.close();
    }
    Like before, the compiled file crashes immediately. I don't think it executes any of the extraction since the pause I added previously didn't seem to get executed since it crashed before it paused. Any other thoughts on what could cause a crash beforehand?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    1. Define "immediately".
    2. You have not explained yet, how it "crashes".
    3. Did you try to debug your code? You have to!
    Last edited by VictorN; July 18th, 2015 at 02:19 PM. Reason: typos
    Victor Nijegorodov

  6. #6
    Join Date
    Jun 2011
    Posts
    17

    Re: Crash Reading & Writing More Files

    I'm not sure how to define "immediately" without repeating my earlier posts relating to the early pause feature not seeming to be executed.

    I'm also not sure how to answer "how" it crashes. It seems to be a run-time error where I click on the compiled program and it crashes before reaching the pause feature added to help troubleshoot.

    The debugger seems to confirm earlier suspicions that it's a stack overflow issue. Are there any other strategies to handle stack overflow issues since the braces didn't seem to solve it?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    If you debugged your code then you have to be able to answer my other question:
    2. What is the total size of data you have read in the moment of crash?
    Victor Nijegorodov

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith View Post
    Are there any other strategies to handle stack overflow issues since the braces didn't seem to solve it?
    yes, of course!
    It is what GCDEF already wrote you:
    "make a function to process the files"
    Victor Nijegorodov

  9. #9
    Join Date
    Jun 2011
    Posts
    17

    Re: Crash Reading & Writing More Files

    Thanks for pointing me in the right direction. I did try making a call function before posting, as I mentioned. However, I wasn't able to get the syntax right.

    Trying again, I found the specific stumbling block was having a variable length buffer array.

    Code:
    char buffer[length];
    So I removed the variable and used the buffer integer needed by the largest file. Everything appears to be working. Thanks again for the pointers.

    (If there is a more elegant solution, I'd be interested. The alternatives I was able to find didn't seem as simple as that.)

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith View Post
    ...
    (If there is a more elegant solution, I'd be interested. The alternatives I was able to find didn't seem as simple as that.)
    Yes. Using MFC CString or std::string instead of the plain char array.
    Victor Nijegorodov

  11. #11
    Join Date
    Jun 2011
    Posts
    17

    Re: Crash Reading & Writing More Files

    I've tried using std::string, but the code won't compile. Maybe I'm using the incorrect syntax or wrong variable type for length (int). I've been searching for examples online for a bit, but haven't had much success. What should this line of code look like?

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith View Post
    I've tried using std::string, but the code won't compile.
    Show your code.
    What errors and where?
    Victor Nijegorodov

  13. #13
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith
    Are there any other strategies to handle stack overflow issues...?
    Quote Originally Posted by Mr. Smith View Post
    (If there is a more elegant solution, I'd be interested. The alternatives I was able to find didn't seem as simple as that.)
    The best strategy to avoid stack overflow is to NOT allocate large arrays on the stack. Are you familiar with the new[] operator?
    Also, your original code sample has comment
    Code:
    //And so on...
    Does it mean that you have multiple buffers defined? Like that?
    Code:
    char buffer001[15000];
    char buffer002[27000];
    char buffer003[XXXXX];
    Since you write the content of those buffers to the output file, couldn't you have reused just ONE buffer?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith View Post
    Here is a snippet of the code...

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main(){
    
    //Still crashes if pause is before all this code.
    //std::cin.get();
    
    ifstream infile001 ("Source1.xxx", ifstream::binary);
    ofstream outfile001 ("001.yyy", ofstream::binary);
    infile001.seekg(0x2a00000);
    char buffer001[15000];
    infile001.read (buffer001,15000);
    outfile001.write (buffer001,15000);
    outfile001.close();
    infile001.close();
    
    ifstream infile002 ("Source1.xxx", ifstream::binary);
    ofstream outfile002 ("002.yyy", ofstream::binary);
    infile002.seekg(0x3f00000);
    char buffer002[27000];
    infile002.read (buffer002,27000);
    outfile002.write (buffer002,27000);
    outfile002.close();
    infile002.close();
    
    //And so on...
    
    return 0;
    
    }
    Don't you see a repeating pattern in your code? It's time to think about introducing a function, something like that:
    Code:
    void pipe_data(ifstream& infile, ofstream& outfile, size_t readSize)
    {
        char* buf = new char[readSize];
        infile.read(buf, readSize);
        outfile.write(buf, readSize);
        delete[] buf;
    }
    Besides, I'd think about re-using in-stream and out-stream objects instead of creating a bunch of one-shot dummies. Or maybe about a loop like this:

    Code:
    typedef struct task {
        char* src;
        char* dst;
        size_t size;
    } Task;
    
    Task tasks[] = {
        { "Source.001", "Destination.001" 15000 },
        ...
        { "Source.055", "Destination.055", 27000 },
        { NULL, NULL, 0}
    };
    
    for (Task* theTask = tasks; theTask->src != NULL; theTask++)
    {    
        ifstream infile (theTask->src, ifstream::binary);
        ofstream outfile (theTask->dst, ofstream::binary);
        pipe_data(infile, outfile, theTask->size);
        infile.close();
        outfile.close();
    }
    Last edited by Igor Vartanov; July 24th, 2015 at 02:50 AM.
    Best regards,
    Igor

  15. #15
    Join Date
    Jul 2015
    Posts
    5

    Re: Crash Reading & Writing More Files

    I think it is a compiler problem.If your compiler is ok then you can try this code

    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;

    int main ()
    {
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What is your favorite team? ";
    getline (cin, mystr);
    cout << "I like " << mystr << " too!\n";
    return 0;
    }

Page 1 of 2 12 LastLast

Tags for this Thread

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