Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File related question

Posted on 2002-04-22
8
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 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
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!

 

Author Comment

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

Thanks
prain
0
 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

705 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