Solved

How do I ensure that my program's output is complete?

Posted on 2011-03-10
23
451 Views
Last Modified: 2013-12-14
I created a program that iterates through the records of 5000 students using a For...Next loop.  

For each student the program creates a summary of all of the classes he/she has taken, their grades in each class, their quarterly progress reports from all of their teachers, and then prints them out into a text file, C:\Output.txt.

Now, some students have much longer summaries than others, especially if teachers write long progress reports and leave lots of comments.

I am finding that the C:\Output.txt is often incomplete....how do I ENSURE that the program WAITS for a student's profile to be OUTPUTTED to the text file BEFORE moving on to the other functions that reports the students' grades?

I do NOT want to set a Sleep() that is a constant number because then some students may have profiles that are super short, and the sleep function ends up taking more time than is needed.  ...I want the function to take as long as it NEEDS to finish its analysis and as long as it NEEDS To output that analysis to the text file.  Sleep, pause, delay do not seem to solve the problem.

I need some kind of "wait...until" function.  As in "wait..until" the output function finishes outputting to text...or "wait...until" the calculation function is done calculating a student's score.   How do I do this?
0
Comment
Question by:shaolinfunk
  • 11
  • 5
  • 4
  • +2
23 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35099410
How exactly are you iterating through the records and wirting the data? Unless you do that multithreaded, an effect like the one you described should not happen...
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35099678
I am pretty sure that I'm not doing it multi-threaded since I don't even know what that is.  

I iterate through the data using loops and output the data using:

      std::ofstream os("c:\\output.txt", std::ios_base::out | std::ios_base::app);

      os << (LPCTSTR) csMessage << std::endl;

      os.close();

however, some students have a LOT of data to be outputted and i noticed that it is not being outputted but rather skipped over.

is there a way to do WAIT....UNTIL?  i'd like to implement something like this to see if it solves my problem...if it doesn't then i will continue debugging to see if the problem is something else.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35099752
Well, in that case you seem to not be writing the complete data, since outputting it to a file that way guarantees that  the call only returns when everything was outputted o a file. Do you happen to have any NULL bytes in 'csMessage'? How are you filling it? Is your application using UNICODE?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35099873
I don't know what NULL bytes are.

Can you explain what you mean by how am I filling it?  Do you mean, how am I filling csMessage?

No, I don't use UNICODE.  
0
 
LVL 86

Expert Comment

by:jkr
ID: 35099946
Yes, the code how are you filling 'csMessage' would be helpful.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35100121
This is devolving into specifics...I really just want to know if there is something like "Wait...Until" that I can use.

CString csMessage;
csMessage.Format("\n %s's Record:\n %s ", strStudentName, strStudentCompleteRecord);
MessageLog(csMessage);
0
 
LVL 35

Expert Comment

by:mccarl
ID: 35100167
> I really just want to know if there is something like "Wait...Until" that I can use.

Then, no, there isn't because as the other experts have already said, the "Wait, until" is built in to the functions that output that data to the file.

It is devolving into specifics because we are trying to solve your problem (as well as answering your original question)
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35100186
The problem is that I cannot post the code here...there is just waaaaay too much.  I am trying to learn general concepts and apply them to my situation.  

Thanks for the answer, knowing that there is no such thing as "Wait...Until" is what I was looking for.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 35100243
I know you said in the original question that you don't like the idea of putting sleep() calls in the code, and I agree with that, but as an exercise to satisfy yourself that this is not a timing issue, you could temporarily insert some sleep() calls and you will see that it should make no difference to the ultimate result (other than making it take a little longer to run!)
0
 
LVL 86

Expert Comment

by:jkr
ID: 35100269
OK, so how are you filling 'strStudentName' and 'strStudentCompleteRecord'?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35100340
jkr...this is what I was afraid of.... with many different functions, all of which do calculations and  add data to the strings strStudentName and strStudentCompleteRecord...there are about 15-20 functions at play here...is this really the route we want to go?

mccarl, yes it should make no difference but i think it does.   let me run it again with the old Sleep() commands I had inserted after the output command.  i believe that i get more data in Output.txt with Sleep (1000) as opposed to Sleep(500).  I will do it again to confirm results.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Expert Comment

by:jkr
ID: 35100503
>>is this really the route we want to go?

There is no other route.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35101042
Ok, jkr I understand what you're saying.

 McCarl, although it should not make a difference it definitely does.  If I put Sleep(1000) immediately after the line that outputs data I get MUCH more data than if I put Sleep(50) immediately after the line that output texts.  Any ideas why that is?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35101234
To be precises...if I use Sleep(1000) my output.txt file is 1.65MB.  If I use Sleep(50) my output.txt is 332kb.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35107784
can you give only a short code snippet with output statements, then the Sleep, and then next output statements?

i agree to all others who said that sleeping or wait-until wouldn't help and the only explanation for the missing parts i could think of, is that a second thread (or second program though that is much less likely) writes parallel to the same file.

oh, wait, i know a further possibility where a wait could help. that is when you were using already freed memory or an array beyond its boundaries for write. when this memory is used by accident for some different purpose - for example by the streaming operator - it easily can get characters with a binary zero (null byte) between some text you want to write to file. that way this text was truncated cause each zero char will terminate the text.

if i am right you work with corrupted memory and there is no other way out as to to check each single output statement whather it operates on correct data.

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35107800
if you make a diff on two output files with different size you easily should see which statistics were missing or corrupted.

Sara
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35108684
//5000 students, lngNumStudents = 5000

      for (x = 0; x < lngNumStudents; x++)
      {      
             csStudentName = names[x];
            StudentReport SR; //class instance      
            SR.CreateReport(csStudentName);      
            SR.OutputReport();   //this function outputs to text
             Sleep(50);  //Sleep(1000)
      }


SR.OutputReport is just:

void StudentReport::OutputReport()
{
      std::ofstream os("c:\\output.txt", std::ios_base::out | std::ios_base::app);

      os << (LPCTSTR) csMessage2 << std::endl;

      os.close();
}

And I have multiple functions throughout that will fill csMessage with something like this:

CString csMessage;
csMessage.Format("\n %s's Record:\n %s ", strStudentName, strStudentCompleteRecord);
MessageLog(csMessage);


Where MessageLog is compiling multiple csMessages....

void StudentReport::MessageLog (CString csMessage)
{
      csMessage2.AppendFormat("%s\n",csMessage);       
}
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35108688
hi sara,

what is a "diff"?
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 35109409
a diff is a comparision of two text files. if you have a version control like source safe or subversion, they have a diff tool included. with visual studio there is windiff included which you'll find in Tools folder.

In the statement

   os << (LPCTSTR) csMessage2 << std::endl;

you probably make an illegal cast which could be respnsible for missing output. generally if you have a function for output you should pass the data for output as arguments and not use any global buffers which might or might not contain valid data. also i am pretty sure taht the cast is not necessary for csMessage2. what type is that variable and where is it defined?

Sara

 
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35109687
csMessage is a CString..defined in the class StudentReport....
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35113110
Sara, do you think you can explain to me what you mean by "you should pass the data for output as arguments and not use any global buffers which might or might not contain valid data"?  I do not know what you mean...can you please show me?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 35125555
Even without a diff utility, you should be able to just eyeball the two files (output.txt) and see what's missing from the short one (display both of them in Notepad.Exe).  That would be a very standard, low-tech way to isolate the problem and learn clues toward diagnosing the underlying cause.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35126848
i mean that statement:

  csMessage2.AppendFormat("%s\n",csMessage);  

where csMessage2 seems to be a global variable???

if it is a member variable it has a poor name.

it is strange that a new output was added to a string rather than written to a logfile or output device.

Sara
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now