Thread: Cin to textfile

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    7

    Cin to textfile

    I want to get artnr, artname and artpris to a textfile called varulager.txt . But I don't really remember how, can getline() send all the variables as I have written to the textfile?
    Code:
     void NyVara(int artnr, string artname, float artpris){
    	varulager.open("varulager.txt", ios::app);
    			if(!varulager){
    			cout<<"Kunde inte utföra önskad åtgärd";
    
    		}
    	cout<<"Artikelnummer";
    	cin>>artnr;
    	cout<<"Varubeskrivning";
    	cin>>artname;
    	cout<<"Pris (ex 99.50)";
    	cin>>artpris;
    	getline(cin, artnr, artname, artpris) >> "varulager.txt";
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope. For writing, you would use varulager << or basic_ostream::put.
    Remember that you have to output to the file you opened; not cin or cout, since they are standard stream directed to the screen by default!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    varulager << artnr, artname, artpris; ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    varulager << artnr << artname << artpris;
    Should work better.
    Btw, I don't see where varulager is declared.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    That's because Im writting a quite big program and this is just a function. I have it declared in the top of my program.
    EDIT; Got the error message, error C2676: binary '<<' : 'class std::basic_ifstream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
    Why?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In other words, it's global? Suggest you stay away from those. Pass it as an argument instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    Since Im using the textfile in other functions I thought that was the best with a global declaration..

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Edit: Actually, it sounds like varulager is an ifstream, you want an ofstream which does output.

    And passing the fstream to the fuction by reference is preferred to using global data. In one small program it might not be a big deal, but using global data that way is generally bad practice so you might as well gain better habits now.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually you would try to avoid global variables unless they're really necessary. It's good programming practice as Daved says!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. textfile questions
    By ReLiEnThAwK in forum C++ Programming
    Replies: 11
    Last Post: 08-07-2005, 06:48 PM