Change size file fstream




















Andrew Hancock - VMware vExpert. See if this solution works for you by signing up for a 7 day free trial. What do I get with a subscription? With your subscription - you'll gain access to our exclusive IT community of thousands of IT pros.

We can't always guarantee that the perfect solution to your specific problem will be waiting for you. If you ask your own question - our Certified Experts will team up with you to help you get the answers you need. Who are the certified experts? How quickly will I get my solution? In very simple and technical words we can say it has ability to do dual work which means it has ofstream and ifstream. So in case if the file is not there on which we are going to write some contents then the fstream has capability to write the contents on the file and at the same time it also allows us to open the file and display the contents of the files.

We should use this if we know that we are going to create, read and write contents on the file. In the below example first we are getting or creating a file, we can give any name to file which we are creating here. Second we are writing some contents to the file.

In the same way we can read the file content with help of the getline function in while loop. Also for 6, why not simply get full size and substract ? If the file is below bytes you could be setting the position below beginning of the stream.

I tested this out and for a smaller file the return size is -1 which I believe is failbit error set. Add a comment. Active Oldest Votes. OK, here we go: No. Essentially, a std::streampos converts to the number of characters from the first position to the current position.

You can go backward using a negative offset. See 3. If you know how many characters are in the file, you subtract from that number. You used absolute positioning using the whence values as position. Lots of boost functions are portable but needs to compile explicitly. Show 1 more comment. Active Oldest Votes. Improve this answer. Stefan Spyros Spyros Based on jterm suggestion, opening the stream would be std::ifstream in filename, std::ios::binary std::ios::ate ; Just to ease everybody's life ; — jmpcm.

WillingGood the file will be closed after returning of the function. This answer to a question on SO says that tellg does not report the size of the file, nor the offset from the beginning in bytes. As mentioned above I don't think tellg is guaranteed to return size, although it always has for me on linux systems I've done it on so use at your own risk.

Show 10 more comments. Add a comment. Do we really need to canonicalize the filename first?! IMO the best answer to this.



0コメント

  • 1000 / 1000