I'm trying to open a template file write information to it and then save that template with a different name.
My issue is that i don't know how to save it with a different name while keeping the information in the template file. I'm currently using Microsoft Visual C++.
Thanks in advance for any help or comedic relife provided.
I know, there are templates, but I never have heard of template files. Could be you mean temporary, but what ever is meant, let it simply call a file. You can rename a file before it is open. You can rename a file after it is closed. While it is open, you cannot do it. Indeed, it would be possible, possible is much, but you will not really have this in your mind. If you would need this for some reason, then you should clearly and detailed explain, why. Maybe we can find another solution.
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
ifstream fin ("template.txt"); // Input a template
ofstream fout("output.txt" ); // Output to a different file
string line;
while( getline(fin, line) ) // Read the template line by line
{
string response;
cout << line; // Prompt the user for a response to this line
getline(cin, response); // Get the response
line += response; // Append the response to the line
fout << line; // Write the line to our output file
}
fin.close(); // cleanup
fout.close();
return 0;
}
Thanks Stewbond,
They only real issue with copying the template file is was if there is a picture in the template file? would the getline function copy over the picture or just characters?
This would only work for ascii (text) files. If you have things like pictures, you'll need to parse it as a binary file. The parsing method will depend on the type of file it is.
When you say template, do you mean a .pdf, .rtf, or another standard?
Oh, then you're probably out of luck. I think .doc file are proprietary microsoft standards.
The publisher of a format will choose whether or not to release the standard to the public. If the publisher wishes to do so, then programmers can make their own software to open and save that type. Microsoft has not elected to do this so you cannot read the file without microsoft office.