How to write EOF during "r+" file access w/ fprintf

Need to write EOF to get rid of the last few lines in a text file.

nepigidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Sorry standard C++ provides no features for truncating a file at a specific offset.  

The only way to do this within standard C++ is to open a file and truncate it and then write to it.  You can use the "w" or "w+" modes to do this.

However, most OSs have procedures for truncating files.  For example, in Windows you can use SetEndOfFile(). to set a file's length.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nepigidAuthor Commented:
Hi - thanks for the quick responce, however, I am a bit confused:

On one hand you say, "standard C++ provides no features for truncating a file" and then you say, "open a file and truncate it."

Could you clarify how to truncate in C?
0
nietodCommented:
You left out the importatn part of what I said "at a specific offset".   You have two choices--you can use truncate the file at its start and then write to it or you can open it as it is and write to it  (in this case you can make the file longer, but not shorter).

This is due to C's legacy, many types of storage devices, like punch card machines, would not provide a way to shorten a file.

So C does not allow you to truncat to a specific spot. If you need to shorten a file, you have to copy the contents that you want to keep to another location (like another file)  Then close and re-open the file and truncate it ("w" mode) and write the information back.

Or use an OS-specific method, like SetEndOfFile().
0
nepigidAuthor Commented:
Ok - that is what I had guessed.  The only way to shorten a file is to re-create the file.

However, I will leave this question open until next week to see if anyone else has input.

Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.