confusing input and output stream behavior

S

scigeek

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,
 
V

Victor Bazarov

scigeek said:
[..]
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
 
M

mzdude

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?
 
S

scigeek

scigeek said:
[..]
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

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.
 
S

scigeek

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.
 
V

Victor Bazarov

scigeek said:
[..]
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
 
S

scigeek

scigeek said:
[..]
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

Thanks for taking time to write.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top