Thread: Whats wrong?

  1. #1
    yatta!
    Guest

    Whats wrong?

    Whats wrong with my program? Pleeease help!!

    Thanks!!
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    int main()
    {
    	fstream file;
    	file.open("data.txt", ios::out);
    	cout << "File object initialised, ready to create and insert text.\n";
    	cout << "Inserting text...";
    	file << "Bling\nplock";
    	cout << "Done\n";
    	file.close();
    	cout << "File object closed\n"; 
    	
    	cout << "Char array created\n";
    	char thing[40];
    	
    	file.open("data.txt", ios::in);
    	cout << "File object initialised\n";
    	cout << "Ready to insert text...";
    	
    	while(!file.eof());
    	{
    		file >> thing;
    	}
    	cout << "done\n\n";
    	cout << "Text:\n";
    	cout << thing;
    	file.close();
    	cout << "\nFile object closed\n";
    
    	return 0;
    }

  2. #2
    Shadow12345
    Guest
    This kind of works, except that when it reads the Bling Plock back in it messes it all up and I'm not sure why

  3. #3
    Shadow12345
    Guest
    This kind of works, except that when it reads the Bling Plock back in it messes it all up and I'm not sure why

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    fstream file;
    
    file.open("data.txt", ios::out);
    
    cout << "File object initialised, ready to create and insert text.\n";
    
    cout << "Inserting text...";
    
    file << "Bling\nplock";
    
    cout << "Done\n";
    
    cout << "File object closed\n";
    
    file.close();
    
    cout << "string created\n";
    
    char thing[40];
    
    file.open("data.txt", ios::in);
    cout << "File object initialised\n";
    cout << "Ready to insert text...";
    
    for(int NotDone = 0; !file.eof();  NotDone++) {
    	file >> thing[NotDone];
    }
    
    
    
    cout << "done\n\n";
    cout << "Text:\n";
    cout << thing;
    
    file.close();
    
    cout << "\nFile object closed\n";
    
    return 0;
    
    }

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Try using getline instead of extraction.
    Code:
    for(int NotDone = 0; !file.eof();  NotDone++) 
    {
    	file.getline(  thing[NotDone], 40, '\n' );
    }
    I haven't tested this but it should help.
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    yatta!
    Guest
    That doesn't work...

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I notieced that his program outputs lots of garbage at the end of the string that was read in. How do you fix that?

  7. #7
    yatta!
    Guest
    I'm confused...what would I change in my code to fix it and why?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM