![]() |
|
#1
|
|||
|
|||
Determing size of a binary fileHi,
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:
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
|
||||
|
||||
|
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:
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. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
||||
|
||||
|
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:
Try working through those other bugs in your program before expecting help with parts of your code that occur after the errors. Matthew |
|
#4
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
Did not mean to sound curt or rude. Some of us are at work (let's not debate the ethics of this
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
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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 08:44 |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 13:17 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 10-Mar-2004 00:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The