Solved

File related question

Posted on 2002-04-22
8
464 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 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
PDF library for Delphi 2 135
c++ syntax question 9 58
Microsoft C++ code failing in executable that worked 9 123
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

726 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