Question:
C++ how to copy a txt file, copy back into the same file?
Izzy M
2013-05-06 14:02:04 UTC
Hello, I need to copy a txt file, and with that same file, write to it back the same thing. Only thing I need to do is iterate the only number in it by one every time the it happens.
This is what the text has:
Times this function has written to this file: 1

When I run this function again, the same text file should now say:
Times this function has written to this file: 2

Next time I run it, the 2 should be a 3.

I know how to open close, output to file. Just need help on what functions I can use to copy the string, find the digit, iterate it.
Thank you.
Three answers:
_Object
2013-05-06 16:47:50 UTC
This process is called serialization.

What you'll want to do is deconstruct the string and store it in a file as binary with C++'s standard ofstream.

After this you can reconstruct the string class and use it again whenever you want, even once you close the program.



Declare a template function accepting a constant reference to the type.

template void SerializeNonPointerType(const T & Object)

Assign the address of Object to a void * Iterator, and now the pointer arithmetic is the hard part.

Cast it to a char* and iterate through until the value is greater than the initial value of void * Iterator + sizeof T;

For each, use ofstream to store the value into a file you create, like this:

void * Iterator = &Object;

void * InitialPosition = Iterator;

for( ; Iterator < (InitialPosition+sizeof(T); (char*)Iterator++)



And than just use ofstream's overloaded operator<<() to output the value at iterator into the filestream.



I'll provide a complete, but limited example via pastebin:

http://pastebin.com/DuAiGY9S

For one, this will NOT handle any container of pointers, any pointers at all, no derived classes, polymorphic classes (remember vtbl pointers), and the string object here only works by a function of it's assignment.

In order to get this to work for more complex classes (this will work fine for normal values, and even some assignable types), it becomes necessary to serialize all objects and references to them, (what a pointer might point to), and if it's a derived class, a copy of the base class too.

For more information on serialization, (or a more capable example), check out boost::serialization here:

http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html

----

But I haven't answered your question yet. Whether or not you've stored a cstring or a std::string makes no difference.

Find the location of the digit by either using a constant value or iterate through with the isdigit() function and convert to atoi() or some variant, as the same function exists for different types but isn't overloaded. (e.g., there's atoll() too, or atof(), atoull(), etc.,)
Timothy
2017-01-18 18:10:14 UTC
1
chappell
2016-12-15 16:50:23 UTC
i think of its have been given something to do with the getline() function. something like this maybe. std::ofstream fileOutput("Filename", and loacation); if (fileOutput.is_open()) { fileOutput << handle << something like that would artwork.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...