Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,981 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,257 people online right now. Registration is fast and FREE... Join Now!




Adding a file extension to user input

 
Reply to this topicStart new topic

Adding a file extension to user input

aj32
30 Aug, 2007 - 04:18 PM
Post #1

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
I am writing a program in C++ that needs to create and open files based on user input, right now the file is created by the use typing in the filename and the extension like: filename.ext.
What I need to know is, is there a way to automatically add a file extension to user input, and if there is, how.

-Thanks
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A File Extension To User Input
30 Aug, 2007 - 05:20 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 36 times
Dream Kudos: 25
My Contributions
Assuming you are taking the user input as a string, simply apend the extension t the string the user has entered.

http://www.cppreference.com/cppstring/append.html

User is online!Profile CardPM
+Quote Post

aj32
RE: Adding A File Extension To User Input
31 Aug, 2007 - 01:13 PM
Post #3

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
That's just what I needed, Thanks! smile.gif
User is offlineProfile CardPM
+Quote Post

aj32
RE: Adding A File Extension To User Input
31 Aug, 2007 - 01:50 PM
Post #4

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
Wait, Now it will not let me use a string for ofstream, is there a way I can convert the string back to a char?

This post has been edited by aj32: 31 Aug, 2007 - 01:50 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A File Extension To User Input
31 Aug, 2007 - 05:27 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 36 times
Dream Kudos: 25
My Contributions
Yup...feed it to the function using the c_str() method.

http://www.cppreference.com/cppstring/c_str.html

User is online!Profile CardPM
+Quote Post

aj32
RE: Adding A File Extension To User Input
1 Sep, 2007 - 01:23 PM
Post #6

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
Yes, that works thanks! biggrin.gif
User is offlineProfile CardPM
+Quote Post

aj32
RE: Adding A File Extension To User Input
4 Sep, 2007 - 11:55 AM
Post #7

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
Hey! I just found another way to append something to user input, it is the strcat() function:
CODE
ofstream FILE(strcat(userinput, ".ext") ios::blablabla);

I've think this one works best! wink2.gif
User is offlineProfile CardPM
+Quote Post

aj32
RE: Adding A File Extension To User Input
5 Sep, 2007 - 11:42 AM
Post #8

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
...Well, it C instead of C++, but it still works!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A File Extension To User Input
5 Sep, 2007 - 11:46 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 36 times
Dream Kudos: 25
My Contributions
Very true, although it's not a great idea to mix the two languages.
User is online!Profile CardPM
+Quote Post

Xing
RE: Adding A File Extension To User Input
6 Sep, 2007 - 05:27 AM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(Amadeus @ 6 Sep, 2007 - 01:16 AM) *

Very true, although it's not a great idea to mix the two languages.

Yes, it's not a good idea. But if you do, you have to take care of lot of things like:

##The C++ compiler must be used to compile main(). This allows the C++ compiler to deal with static initialization and other magical things that main() has to do.

##The C++ compiler must be used to direct the linking process. This allows the C++ compilers to deal with templates and also to link in the C++ libraries.

##The C++ and C compilers probably need to come from the same vendor and have compatible versions. This allows them to use compatible calling conventions in the binary code they generate, as well as compatible naming strategies.

##The interface between the two languages should be kept reasonably small. For example, it is unwise to enable all C++ routines in the entire application to be callable from C, although the reverse is possible: it is not that hard to make C routines callable by C++.

This post has been edited by Xing: 6 Sep, 2007 - 05:28 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month