Renaming A File In C++ Based

DY357LXDY357LX Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
<div class="IPBDescription">On The Users Response Via getline ?</div> FOREWORD: Yes, I have checked Google, but Google'ing for programming stuff can be difficult. And i'm still learning C++ so be nice, my code will hardly be perfect.


Here's my code so far:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// ask the user for a word and then save the text
// as <theirword>.txt
//
#include <string>
#include <fstream>   // removing the .h fixed 4 errors :-/
#include <iostream>

using namespace std;  // using the standard namespace

string name;    // I can't remember how to pass this between
      // functions so I set it up here instead.
void writeTheFile()  
{
ofstream examplefile ("example.txt");
if(examplefile.is_open())
{
 examplefile << "You made this file " << name;
 examplefile.close();
}
}

int main()     // The main function
{
cout << "Enter your name" << endl;
cout << "Then press Return twice." << endl;
// string name; ignore this I moved it to the top of the code.
// when i was adding the writeTheFile() function.
getline(cin, name);  // Get the users name
writeTheFile();   // Call the writeTheFile() function
cout << "Your name is " << name;
cout << " so your file has been saved as " << name << ".txt\n";

rename ("example.txt", ".txt");

return 0;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

Basically, all I want to do is ask the user for their name and then save a text file <theirName>.txt. But I'm struggling to make the code in the last line of the main() work the way I want it to.

I've tried rename ("example.txt", name ".txt"); but that brings up errors.

Can anyone assist me or point me in the direction or a site that can?

Thanks.

Comments

  • CabooseCaboose title = name(self, handle) Join Date: 2003-02-15 Member: 13597Members, Constellation
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// ask the user for a word and then save the text
    // as <theirword>.txt
    //
    #include <string>
    #include <fstream>   // removing the .h fixed 4 errors :-/
    #include <iostream>

    using namespace std;  // using the standard namespace

    string name;    // I can't remember how to pass this between
         // functions so I set it up here instead.

    void writeTheFile()  
    {
    ofstream examplefile("example.txt");
    if(examplefile.is_open())
    {
      examplefile << "You made this file " << name;
      examplefile.close();
    }
    }

    int main()     // The main function
    {
    int result;
    char oldname[] = "example.txt";
    char newname[] = ".txt";
    cout << "Enter your name" << endl;
    cout << "Then press Return twice." << endl;
    // string name; ignore this I moved it to the top of the code.
    // when i was adding the writeTheFile() function.
    getline(cin, name);  // Get the users name
    writeTheFile();   // Call the writeTheFile() function
    cout << "Your name is " << name;
    cout << " so your file has been saved as " << name << ".txt\n";

    result= rename( oldname, newname );
    if (result != 0 )
        perror( "Error renaming file" );
    return 0;
    }<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    at least gets it to write a file named .txt <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • CForresterCForrester P0rk(h0p Join Date: 2002-10-05 Member: 1439Members, Constellation
    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I've tried rename ("example.txt", name ".txt"); but that brings up errors.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Shot in the dark here, since I don't know C++ (But I understand the theory), but how about:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->rename ("example.txt", ""name".txt");<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • CabooseCaboose title = name(self, handle) Join Date: 2003-02-15 Member: 13597Members, Constellation
    <!--QuoteBegin-CForrester+Apr 21 2005, 02:55 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CForrester @ Apr 21 2005, 02:55 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I've tried rename ("example.txt", name ".txt"); but that brings up errors.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Shot in the dark here, since I don't know C++ (But I understand the theory), but how about:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->rename ("example.txt", ""name".txt");<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    that would actually try to rename it to "name".txt
  • CForresterCForrester P0rk(h0p Join Date: 2002-10-05 Member: 1439Members, Constellation
    <!--QuoteBegin-Caboose+Apr 21 2005, 03:59 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Caboose @ Apr 21 2005, 03:59 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-CForrester+Apr 21 2005, 02:55 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CForrester @ Apr 21 2005, 02:55 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I've tried rename ("example.txt", name ".txt"); but that brings up errors.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Shot in the dark here, since I don't know C++ (But I understand the theory), but how about:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->rename ("example.txt", ""name".txt");<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    that would actually try to rename it to "name".txt <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Wouldn't it have to be "rename ("example.txt", "/"name/".txt");" to do that? Or have I got it the other way 'round?
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    edited April 2005
    probably the simplist way would be to use <b>system()</b>:

    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><span style='font-family:Courier'>system("mv oldfile newfile"); // Lunix</span><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->

    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><span style='font-family:Courier'>system("rename oldfile newfile");// Winders</span><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
  • GwahirGwahir Join Date: 2002-04-24 Member: 513Members, Constellation
    for minimal code change just use sprintf

    char newfilename[256];
    sprintf(newfilename, "%s.txt", name);
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    edited April 2005
    Oh wait... I've actually read your post now instead of just the topic:

    In short, you're doing it entirely wrong.

    Ask them for their name THEN make a new file.

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {
      char name[255];
      cout << "Gimmie your name: ";
      cin >> name;
      sprintf(name, "%s.txt", name) // This will turn 'name' into 'name.txt'

      ofstream fp(name);
      fp << "weeeeee";
      close(fp);

      return 0;
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited April 2005
    [Note: written before Doomy's last post]

    No. No no no. Bad. std::system() is BAD - that's using the shell rather than the language (you might as well write a batch file or a bash script if you're going to do that). sprintf() is also bad - it's C, and we're using C++ here. So is getline().

    The C++ way of reading in data is to use std::cin, defined in iostream:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->cout << "Enter your name" << endl;
    std::string name;
    cin >> name;<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Gwahir's got the right idea, but sprintf() is old and bad and evil, and it's much better to use a std::string instead:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->std::string filename(name);
    filename += ".txt";
    std::rename("example.txt", filename.c_str()"); //rename wants a C style string rather than a std::string, and that's what the .cstr() does
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Finally, to pass the filename as a parameter you need to indicate that writeTheFile() accepts a string as a parameter, and when you call it put the string inside the parentheses:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->void writeTheFile(std::string filename)
    {
     //code for writing to the file goes in here
    }

    int main() //good that you're using int main and not void main, btw!
    {
     //...
     writeTheFile(name);
     //...
    }<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    That's the basic idea, although there's a better way of passing string objects (any object, in fact) which I won't go into just yet. It's also usually more efficient to use std::stringstreams than std::strings, but don't worry about that.

    Btw, it's far simpler to just create the file with the user's name in the first place rather than renaming a file to it.
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    Hmm interesting. Everyone has their own methods, and rest assured I will test each one later tonight. (It's like 7:30am here at the moment.)
    And i've emailed myself the code so I can retrieve it at college and ask the tutor (who's obsessed with Java) how he'd do it. And shall post his
    code here.



    Thanks for all the replies guys.
  • GwahirGwahir Join Date: 2002-04-24 Member: 513Members, Constellation
    edited April 2005
    normally I would use strings, if I had cause to bring in namespace std for other reasons, otherwise I prefer to keep that namespace out of things. For simple applications it really isn't worth it. And for huge applications, better to write your own and have full control.

    edit: std is like sexually transimitted disease... once it's there, you can't get rid of it.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin-Gwahir+Apr 22 2005, 04:05 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Gwahir @ Apr 22 2005, 04:05 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> normally I would use strings, if I had cause to bring in namespace std for other reasons, otherwise I prefer to keep that namespace out of things. For simple applications it really isn't worth it. And for huge applications, better to write your own and have full control.

    edit: std is like sexually transimitted disease... once it's there, you can't get rid of it. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Huh? <span style='font-family:Courier'>sprintf()</span> is part of the std namespace as well, and unless you use <span style='font-family:Courier'>#include <stdio.h></span>, which is deprecated in favour of <span style='font-family:Courier'>#include <cstdio></span> you <i>have</i> to deal with the namespace. <i>Everything</i> in the standard library (including STL, strings and the C libraries) is in the <span style='font-family:Courier'>std</span> namespace.

    I don't really understand what you're objecting to, here. It is <i>absolutely not</i> better to write your own implementation of stuff that's in the standard library, under <i>any</i> circumstance other than as a learning exercise.
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    Why on earth would anyone want to write their own implementation of cin and cout? That's just asking for pain...
Sign In or Register to comment.