Thread: User input into a text file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    19

    Post User input into a text file

    Hello, I am somewhat new to C++ but one thing I can't seem to be able to figure out is how to have users inputed text into a text file using filestreams. I tried signing the users input to a string variable using cin and then putting into the text file. For example:

    Code:
     
    #include <iostream>
    #include <fstream>
    using namepace std;
    
    int main()
    {
    string x;
    cout << "Enter your name: ";
    cin >> x;
    ofstream a_file ("test.txt")
    a_file << x;
    return 0;
    }
    Can someone explain this to me please?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) What compiler are you using?

    2) What directory is your program in?

    3) What directory is test.txt in?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    1) Visual C++ 2005 Express Edition
    2) The projects directory in C:\My Documents\Visual Studio 2005\Projects\Stuff\Stuff
    3) The "test.txt" is in the same directy as the source which is ^^^^.

    The manifest file with the debug file and two others are located in the Debug folder in the directory mention in the second answer.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try using the full path name of the text file or moving the text file up one directory. You can also try searching around your computer for test.txt to find out in what directory test.txt was created.
    Last edited by 7stud; 01-10-2006 at 08:05 PM.

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    » ofstream a_file ("test.txt")
    Eh, where is the semi-colon?

    Assuming that's not the problem, try testing if it opened the file:
    Code:
    if( !a_file.is_open() )
    {
      //It didn't open!
    }
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    That above was just an example here is the actual code I'm toying with:

    Code:
    #include <iostream>
    #include <fstream> 
    using namespace std;
    
    int main()
    {
    	string nam; 
    	cout << "Enter a name: ";
    	cin >> nam; 
    	ofstream a_file("test.txt");
    	if (!a_file.is_open() )
    	{
    		cout << "The file failed to open";
    		return 0;
    	}
    	else
    	{
    		a_file << "This is in the file";
    		a_file << nam; 
    		a_file.close();
    	}
    	return 0;
    }
    Here is the error I'm getting error:
    C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion).

    When I change the code to this:

    Code:
    #include <iostream>
    #include <fstream> 
    using namespace std;
    
    int main()
    {
    	char nam[10]; 
    	cout << "Enter a name: ";
    	cin >> nam[10]; 
    	ofstream a_file("test.txt");
    	if (!a_file.is_open() )
    	{
    		cout << "The file failed to open";
    		return 0;
    	}
    	else
    	{
    		a_file << "This is in the file";
    		a_file << nam[10]; 
    		a_file.close();
    	}
    	return 0;
    }
    The only chnage being that I changed the variable type this time however, it builds fine but I get this debug error:

    Run-Time Check Failure #2 - Stack around variable 'nam' was corrupted.

    I am going to throw down some break points to see what's up but if someone can help me out that would be awsome.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    i'll try to answer...
    you declare
    Code:
    char nam[10];
    your accessing:
    Code:
    a_file << nam[10];
    the last value for your array is in nam[9].

    EDIT: stand corrected. Dave's answer is the correct. i didn't see that...

    sorry i'm really not good at explaining.. hope this helps
    Last edited by what3v3r; 01-10-2006 at 09:01 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The solution is to add #include <string> to your includes in the first piece of code.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Wow, I can't believe I forgot about the "string" directory.

    The code with the sting variable works now, thanks Daved and everyone else who had input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 08:39 PM