Solved

File related question

Posted on 2002-04-22
8
460 Views
Last Modified: 2008-02-01
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
Comment
Question by:prain
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6959669
What about turning the buffering off using

setvbuf( fptr, NULL, _IONBF,  0);
0
 
LVL 30

Expert Comment

by:Axter
ID: 6959860
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
 
LVL 30

Expert Comment

by:Axter
ID: 6959864
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
 

Author Comment

by:prain
ID: 6960146
Well it works. This will give some light to my actual problem.

Thanks
prain
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

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

Author Comment

by:prain
ID: 6960336
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
 
LVL 30

Expert Comment

by:Axter
ID: 6960470
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
 
LVL 86

Expert Comment

by:jkr
ID: 6960507
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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

12 Experts available now in Live!

Get 1:1 Help Now