GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 6 votes, 5.00 average.
  #1  
Old 27-May-2005, 19:03
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road

Determing size of a binary file


Hi,

I am trying to read some binary files and write them in another location.
so first i tired to find the size of the file, so that i would know how many bytes i need to read and write.
Here is what i got:
CPP / C++ / C Code:
#include <ifstream>
#include  <iosstream>
#include <dirent.h>
#include <string.h>

using namespace std;

int main(int argc, char* argv[])
{

//
//

  struct dirent* ent;
  DIR* dir = opendir(argv[1]); 
  char* fileName;

  //copying the files from the input dir to field subdirectories--10 in each
   while ((ent = readdir(dir)) != NULL)
   {
	if ((strcmp((char*)ent->d_name, ".") != 0) && (strcmp((char*)
	    ent->d_name, "..") != 0))
	{
	  fileName = ent->d_name;
	  cout << "input file name: " << fileName << endl;
	  cout << "fieldDirNames: " << fieldDirNames << endl;
	  char filePath[100];
	  strcpy((char*)filePath, fieldDirNames); 
         //fieldDirNames is part of the path of the output file
	  cout << "filePath: " << filePath << endl;
	  strcat((char*)filePath, "/");
	  strcat((char*)filePath, fileName);
	  cout << "output file path: " << filePath << endl;

	  ifstream inFile(fileName, ios::in|ios::binary);
	  cout << "opens the file" << endl;
	  long beg,end;
	  inFile.seekg(0, ios::beg);
	  cout << "puts the pointer at the beg of the file" << endl;
	  beg = inFile.tellg();   //*************RETURNS -1
	  cout << "beg: " << beg << endl;
	  inFile.seekg(0, ios::end);
	  end = inFile.tellg();
	  cout << "end: " << end << endl; //**************RETURNS -1
	  long size = (end - beg);
	  cout << "builds size var" << endl;
 	  cout << "size: " << size << endl;     // 0 is printed out
	  char* buffer = new char[size];
	  inFile.read( (char*) &buffer, size );
	  cout << "reads the file" << endl; 
	  ofstream outFile((char*)filePath, ios::out | ios::binary);
	  outFile.write((char*) &buffer[0], size);
	  delete[] buffer;
         }
     } 
}

I get back -1 for the output of tellg() and 0 for the file size.
I looked at a few tutorials and books. i do not know what i am doing wrong here?

Thank you,

Nina
  #2  
Old 27-May-2005, 22:53
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I've never used tellg so I can't help you with that. The way I do this is set up a buffer as large as I wish (256 bytes, 1024, 2Meg, whatever). Define the buffer size as BUFSIZ. Then:
CPP / C++ / C Code:
char buffer[BUFSIZ];
...
Open both files (read and write)
...
do
{
    Read a bufferful (save the number of bytes read in nbyt)
    Write that buffer
} while (nbyt == BUFSIZ);

Then you don't need to figure out how big the file is, which is different on each compiler and OpSystem anyway. And this difference makes your code non-portable.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 31-May-2005, 08:28
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
First things first... The code you posted will not compile, let alone run, due to some typos/mistakes. I am not sure how you tested this and got a -1 from tellg.

The standard library headers are "fstream" not "ifstream", and "iostream" not "iosstream". Also, the variable "fieldDirNames" has not been declared in your program.

If I fix these things (assuming "fieldDirNames" is char* since it is used in strcpy), the program does compile using g++ version 3.3.5. Of course, I don't know what the expected input is (that is, what the input directory should contain). However, running the program on a random directory gives strange output for the strings filePath and fieldDirNames (that is, unprintable characters), and tellg always returns either zero (at file beginning) or a positive non-zero value (at file end), never a -1.

In short, while you have some other problems in your program, there is nothing wrong with your method to find the file size, except that it is unnecessary to use tellg for the beginning of the file, since that will always be zero. The following simpler code also works:
CPP / C++ / C Code:
inFile.seekg(0, ios::end);  // position get-ptr 0 bytes from end
ios::pos_type size = inFile.tellg();  // get-ptr position is now same as file size

Try working through those other bugs in your program before expecting help with parts of your code that occur after the errors.

Matthew
  #4  
Old 31-May-2005, 10:14
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by QED
First things first... The code you posted will not compile, let alone run, due to some typos/mistakes. I am not sure how you tested this and got a -1 from tellg.

The standard library headers are "fstream" not "ifstream", and "iostream" not "iosstream". Also, the variable "fieldDirNames" has not been declared in your program.

If I fix these things (assuming "fieldDirNames" is char* since it is used in strcpy), the program does compile using g++ version 3.3.5. Of course, I don't know what the expected input is (that is, what the input directory should contain). However, running the program on a random directory gives strange output for the strings filePath and fieldDirNames (that is, unprintable characters), and tellg always returns either zero (at file beginning) or a positive non-zero value (at file end), never a -1.

In short, while you have some other problems in your program, there is nothing wrong with your method to find the file size, except that it is unnecessary to use tellg for the beginning of the file, since that will always be zero. The following simpler code also works:
CPP / C++ / C Code:
inFile.seekg(0, ios::end);  // position get-ptr 0 bytes from end
ios::pos_type size = inFile.tellg();  // get-ptr position is now same as file size

Try working through those other bugs in your program before expecting help with parts of your code that occur after the errors.

Matthew

Dear QED,

Thank you for the suggestion. But your assumption is wrong. The typos happend when i tried to copy and paste the code. Also, this is a part of a very long code, i do not think anyone would appreciate if i post the entire (~700 lines) of code. I do truly appologize for the inconvenience that the typos caused (I will be very careful next time when i post some codes), but please either do not post a reply or if you do, be polite. (your tone of voice did not feel to be respectful)

Thank you very much,

Also, i will take the advice of WaltP and do as he said to determine the size of the file. Thank you WaltP

Nina
  #5  
Old 31-May-2005, 10:29
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Did not mean to sound curt or rude. Some of us are at work (let's not debate the ethics of this ), finding bits of time here and there to scan these boards and help out. Perhaps in haste I came across as harsh. My apology is given.

The central point I was trying to make is that using tellg to determine file size works (in fact, it requires much less overhead than actually reading in the bytes; if you're not using those bytes than why read them?)

It could be that the compiler version you are using does not properly implement these standard library functions. (In which case, if you have a choice, I reccommend that you consider an alternative compiler.)

Or, there may be some other bug in your program that causes this problem as a side effect. A problem that will likely still be there even if you use another method for finding the file size. And, it might rear its ugly head later on, at a more sensitive time.

Hope this helps.

Matthew
  #6  
Old 31-May-2005, 11:10
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by QED
Did not mean to sound curt or rude. Some of us are at work (let's not debate the ethics of this ), finding bits of time here and there to scan these boards and help out. Perhaps in haste I came across as harsh. My apology is given.

The central point I was trying to make is that using tellg to determine file size works (in fact, it requires much less overhead than actually reading in the bytes; if you're not using those bytes than why read them?)

It could be that the compiler version you are using does not properly implement these standard library functions. (In which case, if you have a choice, I reccommend that you consider an alternative compiler.)

Or, there may be some other bug in your program that causes this problem as a side effect. A problem that will likely still be there even if you use another method for finding the file size. And, it might rear its ugly head later on, at a more sensitive time.

Hope this helps.

Matthew


There was a minor error in defining the path of the input file. Everything is fixed now and i get the correct value back by using tellg().

Thanks for the help

Nina
  #7  
Old 31-May-2005, 14:06
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Nina,

You can add/subtract to/from the "reputation" of a poster (see the button next to the post number for the post you want to comment about). A nice feature of many forums, including this one, that allows one to reward helpful responses, and of course to feel a bit more empowered when encountering unpleasant responses .

Matthew

Edit: forums -> fora
  #8  
Old 01-Jun-2005, 10:10
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by QED
Nina,

You can add/subtract to/from the "reputation" of a poster (see the button next to the post number for the post you want to comment about). A nice feature of many forums, including this one, that allows one to reward helpful responses, and of course to feel a bit more empowered when encountering unpleasant responses .

Matthew

Edit: forums -> fora

Thank you for letting me know about this feature of the forum--though i would only use it to reward good comments. By making a disrespectful comment and/or an impolite tone of language, one disrespect him/herself, and i think that is enough punishment! ;-)
I am thankful to those who post good suggestions and help me to solve my codes' errors (considering that their response and help is totally voluntary, and how inexperienced i am in programming!)

Thanks

Nazanin
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:11.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.