Outputing cin data to a file?

This is a discussion on Outputing cin data to a file? within the C++ Programming forums, part of the General Programming Boards category; If i have a program that collects a series of data from a user through cin commands, is it possible ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    Outputing cin data to a file?

    If i have a program that collects a series of data from a user through cin commands, is it possible to take the product of those inputs and save them to say...a text file to be called upon later if needed.

    For example. My program allows a user to create a character sheet. I would like that character sheet to be called up later when needed.

    Thanx in advance!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    IOstream Library - C++ Reference
    Create an output fstream, and use it like you would for cout
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    hehehe, well im extremely new to c++ , hell extremely new to programming at all. I had a feeling that was going to be a bit beyond what i can understand quite yet.

    Thank you very much for the link, i will read over it carefully. However, i tend to learn by better by seeing something, replicating it, then breaking/fixing it.

    Any chance anyone can give me a very simple bit of code that might utilize file input/output stream. It would help me greatly.

    Like i said, I will read over the information on the link very carefully as well. I'll do my best to sort it out myself.

    Thanx!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    cout << "This is a line" << endl;

    vs.

    fout << "This is a line" << endl;


    It's that simple, if you just open a file.

    If you can't read some documentation and at least TRY this simple exercise, then there is no future for you as a programmer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Like i said, i am reading the documentation but that my learning comes from seeing it in action and being able to work with it.

    I'm not quite sure what to do with the fout command you posted. From what you said I'd need to create an output fstream first then use fout to output to the stream?
    so something like this?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    #include <string>
    using namespace std;
    
    int main()
    {
     fostream fileName
     fout << "Whatever i want sent to the file";
    }
    Last edited by kamitsuna; 02-23-2010 at 10:18 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kamitsuna View Post
    I'm not quite sure what to do with the fout command you posted. From what you said I'd need to create an output fstream first then use fout to output to the stream?
    I think fout was intended as an example instance of an ofstream object. So
    Code:
    #include <fstream>
    using namespace std;
    
    int main () {
    	ofstream file("tmp.txt");
    
    	file << "hello world";
    
    	return 0;
    }
    "file" could have been called "fout".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Thanks a bunch MK27!
    So after playing with this a little bit i got it sorted out. Managed to export the data i was looking for to a file. My next question is how can i make the file name dynamic.

    Code:
    ofstream fout(cName);
    cName is a cin that is run earlier in the program, I'd like the program to dynamically name the files based on the cName input.

    If i do it
    Code:
    ofstream fout("temp.txt");
    It outputs the file exactly the way i wanted.
    I tried
    Code:
    ofstream fout(cName,".txt");
    and of course, i didnt work.
    Last edited by kamitsuna; 02-24-2010 at 09:19 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by kamitsuna
    cName is a cin that is run earlier in the program, I'd like the program to dynamically name the files based on the cName input.
    Perhaps you meant to say that cName is a std::string object whose value was read from std::cin earlier in the program? If so, it is easy to do what you want to do:
    Code:
    ofstream fout((cName + ".txt").c_str());
    That is, you append ".txt" to cName to get a new string object, then you call the c_str() member function to get a C-style string that is provided to construct fout.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Oh i understand, i was close. Not sure i understand what c_str() is though.
    c_str() is what formats the string of fout?

    That worked just like i wanted. Thank you laserlight!
    Last edited by kamitsuna; 02-24-2010 at 09:32 AM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I just started with C++ too, but I am pretty sure it is the same as C here insofar as you cannot concatenate a string literal onto a variable "on the fly" like this.

    So you need to add ".txt" to the end of cName:
    Code:
    cName.append(".txt");  // C++ string
    strcat(cName,".txt"); // C string
    in the later case you need to make sure cName has room for the extra elements.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27 View Post
    I am pretty sure it is the same as C here insofar as you cannot concatenate a string literal onto a variable "on the fly" like this.
    Looks like you can. Nice to learn new things!

    Quote Originally Posted by laserlight View Post
    If so, it is easy to do what you want to do:
    Code:
    ofstream fout((cName + ".txt").c_str());
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    so
    Code:
    (cName + ".text")
    basically reconstructs the string then
    Code:
    .c_str()
    formats it so then when the final code runs it looks like
    Code:
     ofstream fout (cName.txt);
    That sound right?

    I'm going to post a new thread about calling for the information i just output to the file.

    EDIT: Whats wrong with my forum spacing?!? My left profile box seems to get bigger and bigger!
    Last edited by kamitsuna; 02-24-2010 at 09:40 AM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kamitsuna View Post
    Oh i understand, i was close. Not sure i understand what c_str() is though.
    c_str() is what formats the string of fout?
    Hmm, looks like ofstream takes a const char*, not a string.
    Code:
    char fname[]="myfile.txt";   // C-string  ("fname" would be a char*)
    string fname="myfile.txt";   // C++ string  ("fname" is not a char*)
    But .c_str() is a string.method that converts the C++ string to a C string. So another example:
    Code:
    string fname="myfile.txt";
    ofstream(fname.c_str());
    Without that, ofstream will throw an error.
    Last edited by MK27; 02-24-2010 at 09:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    I think i understand...sorta

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM

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