Renaming A File In C++ Based
DY357LX
Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
in Off-Topic
<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.
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
// 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-->
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-->
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
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?
<!--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-->
char newfilename[256];
sprintf(newfilename, "%s.txt", name);
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-->
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.
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.
edit: std is like sexually transimitted disease... once it's there, you can't get rid of it.
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.