opening and re saving with a different name

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.
closed account (4z0M4iN6)
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.
Lets say you have this file:
name:
age:
gender:

Now lets read it, fill it out, and save it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
closed account (4z0M4iN6)
Thank you Stewbond, I didn't know nowadays terms. Yes looks exactly like mrhappystick needs.
Last edited on
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?
Currently all templates are .doc files
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.

EDIT:
OpenOffice.org managed to reverse engineer the standard to some degree. I just read this which is not a licence, but a promise by Microsoft to not take legal action for using the proprietary .doc format.
http://www.microsoft.com/openspecifications/en/us/programs/osp/default.aspx
You can find the published specification here:
http://msdn.microsoft.com/en-us/library/dd773189(v=office.12).aspx
Last edited on
Topic archived. No new replies allowed.