tags:

views:

75

answers:

4

Hello,

I'm writing a brute-force program for enumerating a certain kind of integer sequence (not important what this sequence actually is).

In order to get my results, I create a file and I'd like to write all program output to this file. However, it seems that the file "stagnates" at 524 kb even if the program should write something into it. I.e. the file is continously written until a file size of 524,0 kb is reached, then nothing more is written into the file.

I know that something should be written to the file because everytime I write something to this file, I printf a message on screen so that I know something is going on.

I am using C (pretty low-level, i.e. only including stdio.h, stdlib.h, time.h, string.h) on ubuntu 10.04.

I create the file the following way:

  time_t rawtime;
  char timeStamp[20];
  struct tm* clocktime;
  time(&rawtime);
  clocktime = localtime (&rawtime);
  strftime (timeStamp, 20, "%Y%m%d_%H%M%S", clocktime);
  char dimStamp[80];
  sprintf(dimStamp, "d%dl%d_", DIMENSION, MAXDEPTH);
  strcat(dimStamp, timeStamp);
  strcat(dimStamp, ".txt");
  FILE *output = fopen(dimStamp, "w");

Has anybody encountered a similar problem and/or does anybody know what this could come from?

Edit:

Could it be that output to a file in C is somewhat asynchronous, i.e. that I have to flush a buffer or something in order to get the file written instantly?

A: 

You didn't show your actual writing code, but in case you're doing fwrite (making raw byte output), you may have to open your file in a binary mode ("wb").

Otherwise you could face a situation when some of your data that is okay for binary stream, may be treated as EOF for plain text stream (such as one in your code sample).

HardCoder1986
+2  A: 

Calling fflush sometimes could solve this problem, you also should check if your data can be binary data and you have to use binary mode ("wb" instead of w) which prevents EOF characters to stop I/O.

schnaader
Thank you. `fflush` did the job.
phimuemue
+1  A: 

If none of the other suggestions work you can try running strace on your program and examine the locations just before and after the place where the file writes stop to see if the stdio library has quit issuing the write calls for the file or if the kernel is issuing an error.

strace -o my_program.strace  ./myprogram

You should also be checking the return values from your IO functions for errors and examining errno for what error did occur.

nategoose
A: 

Use gdb to attach to your program and see live what its doing: gdb -p [PID]

zvrba