Thread: Printing files to the screen console

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Printing files to the screen console

    Hi there,

    I made a program which contains car info... the car make and the mpg. Each car make/mpg is put into a structure and written into a file called cars.dat.

    From there, I open the file cars.dat and go through to see if the mpg is > 30 or < 30. If it's >30 it gets put into the file Over30.dat, otherwise it gets put into Under30.dat. The program works well so far.

    Here is my problem:

    I'm having trouble figuring out how to print the contents from all 3 files to the screen. Essentially I want to print cars.dat, Over30.dat, and Under30.dat to the screen console so the program actually looks like it's doing something instead of doing everything "behind the scenes"

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    // put the data into a structure
    struct Car {
    	string make;
    	int mpg;
    };
    
    int main()
    {
    	Car c1, c2, c3, c4, c5, c6, c7;
    	
    	// define each car
    	c1.make = "Prius";
    	c1.mpg = 45;
    	c2.make = "Civic";
    	c2.mpg = 35;
    	c3.make = "Suburban";
    	c3.mpg = 12;
    	c4.make = "BMW";
    	c4.mpg = 18;
    	c5.make = "Mercedes";
    	c5.mpg = 20;
    	c6.make = "Siena";
    	c6.mpg = 22;
    	c7.make = "Focus";
    	c7.mpg = 32;
    
    	// put all the car info into cars.dat
    	ofstream fout;
    	fout.open("cars.dat", ios::binary);
    	fout << c1.make << " " << c1.mpg << "\n";
    	fout << c2.make << " " << c2.mpg << "\n";
    	fout << c3.make << " " << c3.mpg << "\n";
    	fout << c4.make << " " << c4.mpg << "\n";
    	fout << c5.make << " " << c5.mpg << "\n";
    	fout << c6.make << " " << c6.mpg << "\n";
    	fout << c7.make << " " << c7.mpg << "\n";
    
    	// open cars.dat and go through the structure classes and put the cars in their associated new files (either over30.dat
    	// or under30.dat)
    
    	fout.open("cars.dat");
    
    	if(c1.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c1.make << " " << c1.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c1.make << " " << c1.mpg << "\n";
    		}
    
    	if(c2.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c2.make << " " << c2.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c2.make << " " << c2.mpg << "\n";
    		}
    
    	if(c3.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c3.make << " " << c3.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c3.make << " " << c3.mpg << "\n";
    		}
    
    	if(c4.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c4.make << " " << c4.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c4.make << " " << c4.mpg << "\n";
    		}
    
    	if(c5.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c5.make << " " << c5.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c5.make << " " << c5.mpg << "\n";
    		}
    
    	if(c6.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c6.make << " " << c6.mpg << "\n";
    	}
    		else {
    			ofstream fout;
    			fout.open("under30.dat", ios::app);
    			fout << c6.make << " " << c6.mpg << "\n";
    		}
    
    	if(c7.mpg > 30) {
    		ofstream fout;
    		fout.open("over30.dat", ios::app);
    		fout << c7.make << " " << c7.mpg << "\n";
    	}
    		else {
    		ofstream fout;
    		fout.open("under30.dat", ios::app);
    		fout << c7.make << " " << c7.mpg << "\n";
    		}
    
    		// here is where i'm having trouble:
    		char buf[80];
    		ifstream printCars;
    		printCars.open ("cars.dat", ios::in); 
    		if (printCars.is_open() && printCars.eof())
    		for(int i=0;i<80;i++)
    			cout << buf[i];
    
    		printCars.close();
    		return 0;
    }
    I'm having trouble towards the bottom.

    If someone could help me out with this, that'd be killer.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What exactly is the problem, other than the fact that you have 17 different variables named "fout" and who knows which ones are related? You will have to close your cars.dat file before you can try to read from it.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    How exactly do I display the contents of all 3 files? Ok, say I close my cars.dat file...

    I then put something like this:

    Code:
    		char buf[80];
    		ifstream printCars;
    		printCars.open ("cars.dat", ios::in); 
    		if (printCars.is_open() && printCars.eof())
    		for(int i=0;i<80;i++)
    			cout << buf[i];
    
    		printCars.close();
    And it still won't output anything onto the screen.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to read from printCars into buf before you can print it.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Kinda confused. My apologies... can you point me into the right direction with some literature on this? I've been searching, but can't quite find anything to get me past this hump.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If "<<" goes out, then I would guess ">>" goes in.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Ok, cool. Got it. Thanks.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console screen size
    By harry_p in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2010, 04:46 PM
  2. Moving the Console Cursor & Printing In Color
    By Adak in forum C Programming
    Replies: 0
    Last Post: 03-09-2009, 11:31 AM
  3. console screen (Windows)
    By hakan in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2008, 06:35 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM