C++ Change File Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumpybanana247
    New Member
    • Apr 2007
    • 134

    C++ Change File Name

    Right now, the output of my file automatically ends in ".txt" (which i want it to)
    But, i also want the name to start with "C:/Documents and Settings/All Users/Desktop/"
    How can I add that so "filename" automatically has that directery?
    (Right now, if in the program, i manually type in C:/Documents and Settings/All Users/Desktop/NAMEOFFILE then it will work fine, but i want it to be in the program so i only have to type in NAMEOFFILE

    Thanks,
    Nate



    int main()
    {
    char str[10];
    char FileName[500];
    char FileContents[5000];
    cout<<"Enter The File Name\n";
    cin.getline ( FileName, 500 );
    cout<<"Enter The File Contents\n";
    cin.getline ( FileContents, 5000 );




    strcat( FileName, ".txt");
    ofstream a_file ( FileName );
    a_file<<FileCon tents;
    a_file.close();
    cout<< str <<"\n";
  • Sebouh
    New Member
    • Feb 2007
    • 77

    #2
    Originally posted by lumpybanana247
    Right now, the output of my file automatically ends in ".txt" (which i want it to)
    But, i also want the name to start with "C:/Documents and Settings/All Users/Desktop/"
    How can I add that so "filename" automatically has that directery?
    (Right now, if in the program, i manually type in C:/Documents and Settings/All Users/Desktop/NAMEOFFILE then it will work fine, but i want it to be in the program so i only have to type in NAMEOFFILE

    Thanks,
    Nate



    int main()
    {
    char str[10];
    char FileName[500];
    char FileContents[5000];
    cout<<"Enter The File Name\n";
    cin.getline ( FileName, 500 );
    cout<<"Enter The File Contents\n";
    cin.getline ( FileContents, 5000 );




    strcat( FileName, ".txt");
    ofstream a_file ( FileName );
    a_file<<FileCon tents;
    a_file.close();
    cout<< str <<"\n";
    How about you concatenate the "C:\..." with the filename before you pass it in a_file. But you need to be carefull to do it in the right order.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Why not:

      string root("C:/documents and Settings/All Users/Desktop/");

      string name;

      cin >> name;

      string fullname = root + "/" + name + ".txt";


      ofstream a_file(fullname .c_str());

      ??

      Comment

      • lumpybanana247
        New Member
        • Apr 2007
        • 134

        #4
        i didnt try the first one, but Weaknessforcats fixed the problem for me.
        thanks a lot,
        Nate

        Comment

        Working...