-
March 24th, 2009, 01:06 PM
#1
Create a file and insert text.
Hello, all!
How can I create a file in a speficied directory?
And then add alot of text infomation into it?
Or can I include the file(it's a .cfg and .bat) insite the main program? And then extract a single file to a directory?
I use VC++ 2008. And Windows Vista.
Regards,
realchamp.
-
March 24th, 2009, 03:35 PM
#2
Re: Create a file and insert text.
Code:
string FileSavePath = "C:\\Folder1\\Folder2\\FileName.txt"
string WhatIWantToPutIntoFile = "abc";
fstream myoutputfile(FileSavePath.c_str(),ios::out);
myoutputfile << WhatIWantToPutIntoFile << "\n";
-
March 24th, 2009, 03:45 PM
#3
Re: Create a file and insert text.
Last edited by realchamp; April 5th, 2012 at 11:47 AM.
-
March 24th, 2009, 03:56 PM
#4
Re: Create a file and insert text.
Originally Posted by realchamp
And basicly I have to put my information at the myoutputfile << Here??? << "\n";
That was a variable name, you could put whatever text you wanted into the variable..
Originally Posted by realchamp
And I use // in it?
I know this // Info would effect something in my C++ program. How can I do so, it doesn't? Example: /// INFO.
// are comments
\\ is needed to specify your file path as it will not store string FileLocation = "C:\folder1\folder2\text.txt"; properly.
-
March 24th, 2009, 04:38 PM
#5
Re: Create a file and insert text.
Last edited by realchamp; April 5th, 2012 at 11:46 AM.
-
March 24th, 2009, 05:52 PM
#6
Re: Create a file and insert text.
Originally Posted by realchamp
And doesn't I have to se a file for it?
While the ofstream class has open() and close() methods, generally speaking it's best to simply specify the file location when the ofstream (myoutputfile in this case) is constructed, and allow it to close when the ofstream goes out of scope.
And this is where my file will be saved?
You can use either an absolute or relative path to specify the file location. The path can *either* use forward-slashes or back-slashes; doesn't matter. However, as mentioned be aware that if you use back-slashes, you'll need to double them up or else the string won't be what you expect.
-
March 27th, 2009, 11:45 AM
#7
Re: Create a file and insert text.
Originally Posted by Lindley
While the ofstream class has open() and close() methods, generally speaking it's best to simply specify the file location when the ofstream (myoutputfile in this case) is constructed, and allow it to close when the ofstream goes out of scope.
You can use either an absolute or relative path to specify the file location. The path can *either* use forward-slashes or back-slashes; doesn't matter. However, as mentioned be aware that if you use back-slashes, you'll need to double them up or else the string won't be what you expect.
Thanks! It works!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|