Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

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

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
shaolinfunk
Asked:
shaolinfunk
  • 11
  • 5
  • 4
  • +2
1 Solution
 
jkrCommented:
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
 
shaolinfunkAuthor Commented:
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
 
jkrCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shaolinfunkAuthor Commented:
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
 
jkrCommented:
Yes, the code how are you filling 'csMessage' would be helpful.
0
 
shaolinfunkAuthor Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
> 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
 
shaolinfunkAuthor Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
jkrCommented:
OK, so how are you filling 'strStudentName' and 'strStudentCompleteRecord'?
0
 
shaolinfunkAuthor Commented:
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
 
jkrCommented:
>>is this really the route we want to go?

There is no other route.
0
 
shaolinfunkAuthor Commented:
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
 
shaolinfunkAuthor Commented:
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
 
sarabandeCommented:
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
 
sarabandeCommented:
if you make a diff on two output files with different size you easily should see which statistics were missing or corrupted.

Sara
0
 
shaolinfunkAuthor Commented:
//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
 
shaolinfunkAuthor Commented:
hi sara,

what is a "diff"?
0
 
sarabandeCommented:
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
 
shaolinfunkAuthor Commented:
csMessage is a CString..defined in the class StudentReport....
0
 
shaolinfunkAuthor Commented:
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
 
DanRollinsCommented:
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
 
sarabandeCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now