We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How can we move the end of file to a specified location in c++ not using MFC

riteshromi
riteshromi asked
on
Medium Priority
332 Views
Last Modified: 2010-04-17
suppose there is a file x and a delta file y. the final file is x and the size is fixed to 10000 lines. In file x the line end with a carriage return. I want to prepend the file y to x and if the resulting file is greater than 10000 lines I want to remove the lines from 10000 onwards. If someone could tell me how to move the END OF FILE to a desired location, I would appreciate that.
for counting the number of line I am using the command
find /v /c "noshuchstring" <file name>

THIS IS IN C++ AND I AM NOT USING MFC

HOW TO MOVE THE END OF FILE TO A DESIRED LOCATION
Comment
Watch Question

In C
#include <stdio.h> provides functions like fseek(), lseek(), rewind(), ... to move the file offset to desired location.
In C++
#include <fstream.h> provides functions like.
seekg(), seekp()

Regards
Prashant Sabnekar

Author

Commented:
I dont want the file pointer to be moved. If I am using the file pointer my cpu utilizes to 100% and I do not want that. I want to just truncate the number of lines from the bottom of file x. The number of lines to be truncated is equal to the number of lines in y

Author

Commented:
If I could move my end of file to a specified location, I would not need to count the number of lines and then delete. If the end of file is moved it marks the end of file... thanks,
Ritesh
Commented:
Are all the lines in the file exactly the same length?

If not, there is no way to know where to move the EOF marker without reading each line along the way, because the file has no concept of lines; this is structure imposed at a higher level by arbitrarily picking a character to mark the end of a line.

If each line is exactly the same  size you can easily calculate how many bytes 10,000 lines takes up, and use _chsize() or _chsize_s() to truncate the file.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
no each line does not have the same size. I am trying to have the file x in memory as a circular file and then when needed create a new file with y prepended. But I think with this also the CPU will sipke to 100%. not sure wat to do
To move the eof to a desired location, simply delete the contents after that line.
1. copy file in some temp file.
2. delete ur file, or open it freshly, not in append mode.
3. copy ur temp file into ur file.

Regards
Prashant Sabnekar
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.