user defined file I/O

This is a discussion on user defined file I/O within the C++ Programming forums, part of the General Programming Boards category; ok in my code it has at a point where the user enter there name,the code then adds ".txt" to ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    user defined file I/O

    ok in my code it has at a point where the user enter there name,the code then adds ".txt" to the end of it and 'trys' to open the file

    now before anyone sais anything,i am a complete and utter newb

    ill give you a snipit of the code

    Code:
    cout<<"\nusername:";
        getline(cin,user,'\n');
        cin.ignore();
        user = user + txt;
        ifstream userfile ( user );
        if ( !userfile.is_open() ){
             cout<<"user dosent exist";
             cin.get();
             cin.ignore();
             }
    i HAVE declared the two strings,one has as its starting operator ".txt",im also using fstream and iostream and am using namespace std

    any help for a new guy?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    ifstream userfile ( user.c_str() );
    The constructor for file streams that automatically opens a file given as the constructor's argument expects a C-style NULL terminated character string as an argument and not a C++ string container object. Thankfully, C++ strings have the handy c_str() member function which returns a C-style NULL terminated character string for you to use as I have above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    any help for a new guy?
    1) You should always post code that shows where and how the variables are declared. It isn't productive to speculate what you might actually have for code.

    2) You don't need to declare a string variable named txt and assign it ".txt", e.g.

    string txt = ".txt";

    All you need to do is this:

    user = user + ".txt";

    3)
    i HAVE declared the two strings,one has as its starting operator ".txt"
    The terminology goes like this: "I have declared two strings and one has a starting value of ".txt".

    4)Your problem is this line:

    ifstream userfile ( user );

    You have a string type in between the parentheses: the variable user is a string type. You have to use a different type between the parentheses for your program to work. To get the needed type from a string type, you have to use the string function c_str(). hk_mp5kpdw posted how to do that.

    Basically, in C++ whenever you put a variable between the parentheses of a function, it has to be the correct type. The function is expecting a specific type, and if you don't feed it the exact type it is expecting, you will get an error.
    Last edited by 7stud; 06-05-2005 at 03:41 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    thank you for that

    and im sory i got a few things wrong with posting my code and terminology

    i hope that things can get a bit better from now on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. User entering I/O File names at command line
    By Wiggin in forum C++ Programming
    Replies: 6
    Last Post: 04-27-2002, 12:43 AM
  5. User Defined save file (help)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 04-16-2002, 11:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21