confusing input and output stream behavior

Discussion in 'C++' started by scigeek, Sep 8, 2009.

  1. scigeek

    scigeek Guest

    Hello All,

    I am trying to write a c++ program for converting a certain data
    format into another format. In the process, I have to open and close
    multiple output files depending on a particular value in the input
    file.
    Here is a toy example of the input file:
    14
    5 33 0 0 45 47
    5 41 1 1 46 49
    5 39 1 1 25 47
    6 14 1 1 33 39 41
    4 16 2 1 32
    3 17 2 2
    7 93 3 3 47 46 49 87
    4 49 3 3 32

    explanation: the first line contains the number of nodes in a graph.
    From second line onwards, we need to look at the 3rd entry of each
    line: lets call it "iteration number". For each iteration number, we
    need to open a file, write the data into the file, and when a new
    iteration number is encountered, all the data is to be dumped into the
    currently opened file, close it and then open another output file for
    the new iteration number. So for this particular toy example, we need
    to have 4 out put files; one for iteration number 0, one with
    iteration number 1, for iteration number 2 and iteration number 3.

    I am using ofstream object outStream for manipulating writing into
    output files and ifp for reading input stream. I find that the program
    aborts with a message " double free or corruption "
    (the program compiles alright) after reading in the final line of the
    file and nothing is being written in the 4th output file that I opened
    (by the way the other files contain all the data in the proper format
    and they are perfect, except the final file). While checking, I found
    out, that after reading in the last of the line from the input file,
    the ifp becomes 0 all of a certain and similarly the outStream object
    shows a value of 0. It is confusing and with my moderate programing
    ability, I couldn't figure out why this is happening.

    I know it is difficult to say anything from this terse description of
    my program. So if any body is interested to help me out I can send the
    source code (with proper comments) to him/her.

    It would be of immense help to me if I can get this program to run and
    know where I am faulted. PLEASE NOT, IT IS NOT RELATED TO ANY
    CLASSROOM WORK at all. It is a small part of my research project. If I
    can run it, it would save me considerable time and effort for
    otherwise, i have to run 200 iterations manually, corresponding to
    each iteration number for each input file.

    Thanks and regards,
     
    scigeek, Sep 8, 2009
    #1
    1. Advertising

  2. scigeek wrote:
    > [..]
    > I am using ofstream object outStream for manipulating writing into
    > output files and ifp for reading input stream. I find that the program
    > aborts with a message " double free or corruption "


    So, you have some dynamic memory management, and you're screwing it up
    somehow. Right? Or, possibly, you have a buffer of sorts (probably in
    the freestore as well), and you're writing beyond its boundaries. It
    would seem there is nothing else that would cause "corruption".

    > [..]
    > I know it is difficult to say anything from this terse description of
    > my program. So if any body is interested to help me out I can send the
    > source code (with proper comments) to him/her.


    I charge $200/hr for private consultations (1 hour minimum). Are you
    prepared to pay that much? I take Paypal.

    Another way for you to get help is to follow the recommendations of the
    FAQ 5.8. Do you know where to find it?

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 8, 2009
    #2
    1. Advertising

  3. scigeek

    mzdude Guest

    On Sep 8, 11:10 am, scigeek <> wrote:
    > Hello All,
    >
    > I am trying to write a c++ program for converting a certain data
    > format into another format. In the process, I have to open and close
    > multiple output files depending on a particular value in the input
    > file.
    > Here is a toy example of the input file:
    > 14
    > 5 33 0 0 45 47
    > 5 41 1 1 46 49
    > 5 39 1 1 25 47
    > 6 14 1 1 33 39 41
    > 4 16 2 1 32
    > 3 17 2 2
    > 7 93 3 3 47 46 49 87
    > 4 49 3 3 32
    >
    > explanation: the first line contains the number of nodes in a graph.
    > From second line onwards, we need to look at the 3rd entry of each
    > line: lets call it "iteration number". For each iteration number, we
    > need to open a file, write the data into the file, and when a new
    > iteration number is encountered, all the data is to be dumped into the
    > currently opened file, close it and then open another output file for
    > the new iteration number. So for this particular toy example, we need
    > to have 4 out put files; one for iteration number 0, one with
    > iteration number 1, for iteration number 2 and iteration number 3.
    >
    > I am using ofstream object outStream for manipulating writing into
    > output files and ifp for reading input stream. I find that the program
    > aborts with a message " double free or corruption "
    > (the program compiles alright) after reading in the final line of the
    > file and nothing is being written in the 4th output file that I opened
    > (by the way the other files contain all the data in the proper format
    > and they are perfect, except the final file).  While checking, I found
    > out, that after reading in the last of the line from the input file,
    > the ifp becomes 0 all of a certain and similarly the outStream object
    > shows a value of 0. It is confusing and with my moderate programing
    > ability, I couldn't figure out why this is happening.
    >
    > I know it is difficult to say anything from this terse description of
    > my program. So if any body is interested to help me out I can send the
    > source code (with proper comments) to him/her.
    >
    > It would be of immense help to me if I can get this program to run and
    > know where I am faulted. PLEASE NOT, IT IS NOT RELATED TO ANY
    > CLASSROOM WORK at all. It is a small part of my research project. If I
    > can run it, it would save me considerable time and effort for
    > otherwise, i have to run 200 iterations manually, corresponding to
    > each iteration number for each input file.
    >


    At a guess, you are probably trying to do too much.

    A quick review of your algorithm should help.

    // Make sure the following works before proceeding
    std::vector<std::string> lines = ReadLines("myInputFile");

    std::eek:fstream ofs;
    // Set up an iterator to go through the "lines"
    for( ... ; i != lines.end(); ++i)
    {
    if( HasIteratorNumberChanged(*i) )
    OpenNewOutput(*i,ofs);
    else // continue with logging
    ofs << *i;
    }

    Now the program can be easily debugged in parts.
    Get the reading done and correct before proceeding.
    Can you accurately detetect when column 3 changes?
    Can you accurately open a new output file?
     
    mzdude, Sep 8, 2009
    #3
  4. scigeek

    scigeek Guest

    On Sep 8, 11:24 am, Victor Bazarov <> wrote:
    > scigeek wrote:
    > > [..]
    > > I am using ofstream object outStream for manipulating writing into
    > > output files and ifp for reading input stream. I find that the program
    > > aborts with a message " double free or corruption "

    >
    > So, you have some dynamic memory management, and you're screwing it up
    > somehow.  Right?  Or, possibly, you have a buffer of sorts (probably in
    > the freestore as well), and you're writing beyond its boundaries.  It
    > would seem there is nothing else that would cause "corruption".
    >
    > > [..]
    > > I know it is difficult to say anything from this terse description of
    > > my program. So if any body is interested to help me out I can send the
    > > source code (with proper comments) to him/her.

    >
    > I charge $200/hr for private consultations (1 hour minimum).  Are you
    > prepared to pay that much?  I take Paypal.
    >
    > Another way for you to get help is to follow the recommendations of the
    > FAQ 5.8.  Do you know where to find it?
    >
    > > [..]

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    No...i don't know where to find FAQ 5.8. Also, I thought the solo
    purpose of usenet groups are to provide help. Didn't know you guys
    charge now a days.

    Well...thanks for your help though. I will try to figure out more.
     
    scigeek, Sep 8, 2009
    #4
  5. scigeek

    scigeek Guest

    On Sep 8, 11:38 am, Pete Becker <> wrote:
    > scigeek wrote:
    >
    > > I am using ofstream object outStream for manipulating writing into
    > > output files and ifp for reading input stream. I find that the program
    > > aborts with a message " double free or corruption "
    > > (the program compiles alright) after reading in the final line of the
    > > file and nothing is being written in the 4th output file that I opened
    > > (by the way the other files contain all the data in the proper format
    > > and they are perfect, except the final file).

    >
    > My crystal ball says that the code tests for the end of input with
    > infile.eof() instead of infile.
    >
    > --
    >    Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
    > "The Standard C++ Library Extensions: a Tutorial and Reference"
    > (www.petebecker.com/tr1book)


    Well...I was using eof() but have removed it since but getting the
    same result. Thanks for your help.
     
    scigeek, Sep 8, 2009
    #5
  6. scigeek

    scigeek Guest

    On Sep 8, 12:03 pm, mzdude <> wrote:
    > On Sep 8, 11:10 am, scigeek <> wrote:
    >
    >
    >
    > > Hello All,

    >
    > > I am trying to write a c++ program for converting a certain data
    > > format into another format. In the process, I have to open and close
    > > multiple output files depending on a particular value in the input
    > > file.
    > > Here is a toy example of the input file:
    > > 14
    > > 5 33 0 0 45 47
    > > 5 41 1 1 46 49
    > > 5 39 1 1 25 47
    > > 6 14 1 1 33 39 41
    > > 4 16 2 1 32
    > > 3 17 2 2
    > > 7 93 3 3 47 46 49 87
    > > 4 49 3 3 32

    >
    > > explanation: the first line contains the number of nodes in a graph.
    > > From second line onwards, we need to look at the 3rd entry of each
    > > line: lets call it "iteration number". For each iteration number, we
    > > need to open a file, write the data into the file, and when a new
    > > iteration number is encountered, all the data is to be dumped into the
    > > currently opened file, close it and then open another output file for
    > > the new iteration number. So for this particular toy example, we need
    > > to have 4 out put files; one for iteration number 0, one with
    > > iteration number 1, for iteration number 2 and iteration number 3.

    >
    > > I am using ofstream object outStream for manipulating writing into
    > > output files and ifp for reading input stream. I find that the program
    > > aborts with a message " double free or corruption "
    > > (the program compiles alright) after reading in the final line of the
    > > file and nothing is being written in the 4th output file that I opened
    > > (by the way the other files contain all the data in the proper format
    > > and they are perfect, except the final file).  While checking, I found
    > > out, that after reading in the last of the line from the input file,
    > > the ifp becomes 0 all of a certain and similarly the outStream object
    > > shows a value of 0. It is confusing and with my moderate programing
    > > ability, I couldn't figure out why this is happening.

    >
    > > I know it is difficult to say anything from this terse description of
    > > my program. So if any body is interested to help me out I can send the
    > > source code (with proper comments) to him/her.

    >
    > > It would be of immense help to me if I can get this program to run and
    > > know where I am faulted. PLEASE NOT, IT IS NOT RELATED TO ANY
    > > CLASSROOM WORK at all. It is a small part of my research project. If I
    > > can run it, it would save me considerable time and effort for
    > > otherwise, i have to run 200 iterations manually, corresponding to
    > > each iteration number for each input file.

    >
    > At a guess, you are probably trying to do too much.
    >
    > A quick review of your algorithm should help.
    >
    > // Make sure the following works before proceeding
    > std::vector<std::string> lines = ReadLines("myInputFile");
    >
    > std::eek:fstream  ofs;
    > // Set up an iterator to go through the "lines"
    > for( ... ; i != lines.end(); ++i)
    > {
    >    if( HasIteratorNumberChanged(*i) )
    >      OpenNewOutput(*i,ofs);
    >    else // continue with logging
    >      ofs << *i;
    >
    > }
    >
    > Now the program can be easily debugged in parts.
    > Get the reading done and correct before proceeding.
    > Can you accurately detetect when column 3 changes?
    > Can you accurately open a new output file?


    Thanks a lot. I will try to implement your suggestion and see what I
    get. Yes, I could see correctly when col 3 changes and when a new file
    is opened.
     
    scigeek, Sep 8, 2009
    #6
  7. scigeek wrote:
    > [..]
    > No...i don't know where to find FAQ 5.8. Also, I thought the solo
    > purpose of usenet groups are to provide help. Didn't know you guys
    > charge now a days.


    Huh? You didn't know personal, one-to-one, consultation isn't free?
    What country do you live in?

    And, no, we don't charge people when we answer questions here. I am
    referring to your offer to "send the source code" to those "interested
    to help" you out. I mean, think about it, what would anybody be
    "interested" by? Gaining you as a friend? A reference? Do you see
    such individual "help" as an act of charity? Or are *you* offering
    something? What?

    When questions asked and answered in a Usenet forum, at least there are
    benefits to others who will come and read those afterwards - the
    knowledge is shared, and such a solution would get a thorough peer review.

    The FAQ is here: http://www.parashift.com/c -faq-lite/

    > Well...thanks for your help though. I will try to figure out more.


    Read the FAQ. It helps. If only by disillusioning you about what you
    know, or what to expect from [the members of] this newsgroup.

    Welcome to the Real World!

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 8, 2009
    #7
  8. scigeek

    scigeek Guest

    On Sep 8, 1:45 pm, Victor Bazarov <> wrote:
    > scigeek wrote:
    > > [..]
    > > No...i don't know where to find FAQ 5.8. Also, I thought the solo
    > > purpose of usenet groups are to provide help. Didn't know you guys
    > > charge now a days.

    >
    > Huh?  You didn't know personal, one-to-one, consultation isn't free?
    > What country do you live in?
    >
    > And, no, we don't charge people when we answer questions here.  I am
    > referring to your offer to "send the source code" to those "interested
    > to help" you out.  I mean, think about it, what would anybody be
    > "interested" by?  Gaining you as a friend?  A reference?  Do you see
    > such individual "help" as an act of charity?  Or are *you* offering
    > something?  What?
    >
    > When questions asked and answered in a Usenet forum, at least there are
    > benefits to others who will come and read those afterwards - the
    > knowledge is shared, and such a solution would get a thorough peer review..
    >
    > The FAQ is here:http://www.parashift.com/c -faq-lite/
    >
    > > Well...thanks for your help though. I will try to figure out more.

    >
    > Read the FAQ.  It helps.  If only by disillusioning you about what you
    > know, or what to expect from [the members of] this newsgroup.
    >
    > Welcome to the Real World!
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks for taking time to write.
     
    scigeek, Sep 8, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG
    Replies:
    4
    Views:
    618
    clintonG
    Oct 17, 2005
  2. Replies:
    9
    Views:
    665
    Alex Buell
    Apr 27, 2006
  3. shapper

    GridView ... confusing behavior

    shapper, Mar 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    296
    Greg9Strat
    Mar 9, 2007
  4. Kashif Ur Rehman
    Replies:
    2
    Views:
    888
    Tom Hawtin
    May 17, 2007
  5. paul

    confusing thread behavior

    paul, Dec 3, 2009, in forum: Python
    Replies:
    2
    Views:
    452
Loading...

Share This Page