Thread: How to open a text file in a specific directory

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Question How to open a text file in a specific directory

    I want to create a directory which is taken from the user(like test123) and than open a txt file in it and write some thing on that text. Here is my code. Please help me to solve this problem!!!

    [tag]

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <stdio.h>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char * argv[])
    {
    string str;
    LPSECURITY_ATTRIBUTES attr;
    attr = NULL;
    cout << "Type a Folder Name To Create.\n";
    cout << "Folder=";
    cin >> str;
    CreateDirectory(str.c_str(), attr);
    cout << "\nFolder Created!\n";
    
    
    ofstream outfile;
    outfile.open("c:\\str.c_str()\document1.txt", ios::out);
    outfile<<"Hello!!!!....";
    outfile.close();
    return 0;
    getchar();
    getchar();
    }

    [/tag]

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    The call to str.c_str() is within the string "c:\\str.c_str()\document1.txt" will be treated as a string.

    You need to create the string:

    "c:\\" + str.c_str() + "\\document1.txt"

    And also for the create directory, otherwise it would be created locally:

    "c:\\" + str.c_str()

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Question

    Quote Originally Posted by Scarlet7 View Post
    The call to str.c_str() is within the string "c:\\str.c_str()\document1.txt" will be treated as a string.

    You need to create the string:

    "c:\\" + str.c_str() + "\\document1.txt"

    And also for the create directory, otherwise it would be created locally:

    "c:\\" + str.c_str()

    It gives error in + ; and say invalid pointer addition.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    It gives error in + ; and say invalid pointer addition.
    Yes, that is because c_str() converts the string to a char pointer.
    Tty this -
    Code:
    char cstr[64] = "c:\\";
    strcat(cstr,str.c_str());
    strcat(cstr, "\\document1.txt");
    
    ...
    
    outfile.open(cstr, ios::out);

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Quote Originally Posted by Spidey View Post
    Yes, that is because c_str() converts the string to a char pointer.
    Tty this -
    Code:
    char cstr[64] = "c:\\";
    strcat(cstr,str.c_str());
    strcat(cstr, "\\document1.txt");
    
    ...
    
    outfile.open(cstr, ios::out);
    Thank you Spidey, it works.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It might have worked, but it wasn't a good C++ answer.

    > CreateDirectory(str.c_str(), attr);
    Like this, you should have built your string as a C++ string, then at the last possible moment used the c_str() method to get the result to pass to the function.

    Running back to C-style char arrays isn't good.

    Say
    Code:
    string filename = "C:\\";
    filename += str;
    filename += "\\document1.txt";
    outfile.open(filename.c_str(), ios::out);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, if you want to make your code a little easier to read (and maybe more portable with relative paths) then use / instead of \\ for your path separators.
    The Windows API will always accept / or \\ but UNIX will only accept / for path separators. So just use the thing they both have in common which is /.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by Salem View Post
    It might have worked, but it wasn't a good C++ answer.

    > CreateDirectory(str.c_str(), attr);
    Like this, you should have built your string as a C++ string, then at the last possible moment used the c_str() method to get the result to pass to the function.

    Running back to C-style char arrays isn't good.

    Say
    Code:
    string filename = "C:\\";
    filename += str;
    filename += "\\document1.txt";
    outfile.open(filename.c_str(), ios::out);
    Yup, that would be a much better way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM