Copy not copying the whole file

  • Thread starter Thread starter m
  • Start date Start date
M

m

Hi All

I have an C++ application which writes to a log file.
I tried to do a copy of the log file.

The original log file has 20 lines, however when I copy (while the
program that feeds the log file file is still running) I find in the
copied file has only 18 lines.

Note: I can open the log file in Notepad only while the program that
feeds the log file is running. If I open in Wordpad it gives me
sharing violation.

I want all the 20 lines in the copied file as well, what can I do.
Please let me know.

Thanks
M
 
m said:
Hi All

I have an C++ application which writes to a log file.
I tried to do a copy of the log file.

The original log file has 20 lines, however when I copy (while the
program that feeds the log file file is still running) I find in the
copied file has only 18 lines.

Note: I can open the log file in Notepad only while the program that
feeds the log file is running. If I open in Wordpad it gives me
sharing violation.

I want all the 20 lines in the copied file as well, what can I do.
Please let me know.

Thanks
M

I think this is a feature of your C++ program, rather than Windows. I'll
take a guess at the problem:

You are not seeing all the data because your C++ application hasn't written
all of the data to the file at the point you are copying it. I'm not overly
familiar with C++ but make sure you are explicitly closing the file handle
before you take your copy - this may cause the application to finish writing
to the file.

Notepad does not attempt to lock the file when opening it - the file
contents is held in memory, and other applications will still be able to
write to it.

Wordpad does lock the file when opening it. As you still have the file open
in your C++ application Wordpad will fail when attempting to lock the file -
hence the error message you are receiving.

Ed Metcalfe.
 
Add a line to flush the buffers after you write a line to the file.

Here's a short program, for example:

#include <stdio.h>
void main()
{
FILE* fp;
fp=fopen("C:\\temp\\test.txt", "a+");
fprintf(fp, "Test line 1\n");
fflush(fp);
fprintf(fp, "Test line 2\n");
fflush(fp);
//...add more fprintf and fflush statements here
fclose(fp);
}



Hope this helps.

MD
 
Back
Top