Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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

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
0
riteshromi
Asked:
riteshromi
  • 3
  • 2
2 Solutions
 
Prashant SabnekarAVPCommented:
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
0
 
riteshromiAuthor 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
0
 
riteshromiAuthor 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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
waysideCommented:
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.
0
 
riteshromiAuthor 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
0
 
Prashant SabnekarAVPCommented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now