Go Premium for a chance to win a PS4. Enter to Win

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

File related question

OK. I am creating a humongous size BINARY file using fcreate(), fwrite() and fclose() functions in C. When I call the close() only I see the actual data is flushed into the disk.
How would I see the file size GROWING when I do a dir or (ls on UNIX), without waiting until close() is called. I have used fflush() etc. But did not work.
0
prain
Asked:
prain
  • 3
  • 3
  • 2
1 Solution
 
jkrCommented:
What about turning the buffering off using

setvbuf( fptr, NULL, _IONBF,  0);
0
 
AxterCommented:
Do fseek(stream_obj, 0, SEEK_END)

Use fseek and ftell.

Example:
long GetMySize(FILE *stream)
{

}

Use ftell.  ftell will give you the current file position.

If your file position is not at the end of the file already, then you can use a combination of ftell and fseek.
Example:
long GetMySize(FILE *MyStream)
{
  long OldPos = ftell(MyStream);
  fseek(MyStream, 0, SEEK_END);
  long CurLenFile = ftell(MyStream);
  fseek(MyStream, OldPos, SEEK_SET);
  return CurLenFile;
}

0
 
AxterCommented:
Oops!! I forgot to delete the top part of my comment before submitting it.

Ignore above comment:

Correction:
Use ftell.  ftell will give you the current file position.

If your file position is not at the end of the file already, then you can use a combination of ftell
and fseek.
Example:
long GetMySize(FILE *MyStream)
{
 long OldPos = ftell(MyStream);
 fseek(MyStream, 0, SEEK_END);
 long CurLenFile = ftell(MyStream);
 fseek(MyStream, OldPos, SEEK_SET);
 return CurLenFile;
}
0
Industry Leaders: 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!

 
prainAuthor Commented:
Well it works. This will give some light to my actual problem.

Thanks
prain
0
 
jkrCommented:
Thanks! Hmm, it seems that this answer is suitable for http://www.experts-exchange.com/cprog/Q.20291562.html also :o)
0
 
prainAuthor Commented:
jkr,

Here is the small progam I wrote. One problem is that when I read the data back, I do not see printing the values I gave while writing the file. I wrote an array of value 25.
(Please see code). Then I close the file. Open it again in "rb" mode. Prints the read values. Can you see what the problem is? Thanks.

#include <stdio.h>
#include <iostream.h>

#define DATA_BUFFER_SIZE 1000

int main()
{
     int data_array[DATA_BUFFER_SIZE];
     int read_data_array[DATA_BUFFER_SIZE];
     
     FILE *pFile;


    //Data Buffer. Always set all elements to zero.
     //Debug only. Actual data comes later.
     for (int i = 0; i < DATA_BUFFER_SIZE; i++)
          data_array[i] = 25;

     //Open the file.
     pFile=fopen ("myfile.dat","wb");
     
     //Set the buffer for the file  
      setvbuf ( pFile , NULL , _IONBF , 0);

   //write Large amount of data.
   for (long index = 0; index < 1; index ++)
    fwrite(data_array, 1, DATA_BUFFER_SIZE ,pFile);
     
   //File operations here
   fclose (pFile);


   //==============================================================
   // Now Read the Information Back and see if it was written right.

   //open the file for the second time for reading
   pFile=fopen ("myfile.dat","rb");

   //Read data.
   for (index = 0; index < 1; index ++)
   {
    fread(read_data_array, DATA_BUFFER_SIZE, 1 ,pFile);
     for(int x = 0; x < DATA_BUFFER_SIZE; x++)

      cout << read_data_array[x] << " ";
   }

   //close the file
   fclose(pFile);

   return 0;
}
0
 
AxterCommented:
FYI:
IMHO, If all you want to do is find out the current size of a file, ftell is a better method for your requirements.
0
 
jkrCommented:
Th eproblem is the way you read and write - you are using 'fread()' and 'fwrite()' incorrectly. Make it read

 //write Large amount of data.
     fwrite(data_array, sizeof ( int), DATA_BUFFER_SIZE ,pFile);

and

  //Read data.
  for (index = 0; index < 1; index ++) // you do not need the outer loop, acutally...
  {
   fread(read_data_array, sizeof ( int), DATA_BUFFER_SIZE ,pFile);
    for(int x = 0; x < DATA_BUFFER_SIZE; x++)

     cout << read_data_array[x] << " ";
  }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now