CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    201

    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.

  2. #2
    Join Date
    Mar 2009
    Posts
    9

    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";

  3. #3
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a file and insert text.

    Derp
    Last edited by realchamp; April 5th, 2012 at 11:47 AM.

  4. #4
    Join Date
    Mar 2009
    Posts
    9

    Re: Create a file and insert text.

    Quote Originally Posted by realchamp View Post
    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..

    Quote Originally Posted by realchamp View Post
    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.

  5. #5
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a file and insert text.

    Derp.
    Last edited by realchamp; April 5th, 2012 at 11:46 AM.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Create a file and insert text.

    Quote Originally Posted by realchamp View Post
    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.

  7. #7
    Join Date
    Feb 2009
    Posts
    201

    Re: Create a file and insert text.

    Quote Originally Posted by Lindley View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured