Solved

File related question

Posted on 2002-04-22
8
459 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

706 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

18 Experts available now in Live!

Get 1:1 Help Now