show output from file

This is a discussion on show output from file within the C++ Programming forums, part of the General Programming Boards category; How do I show an output from a file automatically? Some outputs can be 1000's of lines long that can ...

  1. #1
    Unregistered
    Guest

    Angry show output from file

    How do I show an output from a file automatically?
    Some outputs can be 1000's of lines long that can exceed the screen's limit only showing part of the output. I can output the results to a file but is there a solution for the program to open the output file automatically? I was suggested using 'system()' from 'stdlib.h' but I don't know how? any suggestions or help?

    here's a sample code: (the commented lines are the implements)

    #include<iostream.h>
    #include<fstream.h>
    // #include<stdlib.h>

    void main()
    {
    ofstream OutFile ("C:/output.txt")
    for ( int i = 0; i < 100; i++ ) { OutFile << i << endl; }
    // system() //what parameter(s) do I use for this function or is there a better method?
    }

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    You can pass whatever system command you want with the system() function.

    As a parameter you pass it the command:

    system("cd my_directory"); // will actually execute that command in the system
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Thought of something to add:

    Just think of the system() function as you being in a command prompt window or a system terminal.

    In Unix for example you would type "ls -al" to get a long listing of the current directory showing all the files. In order for you to do the same thing from your program you can write the following:

    #include <stdlib.h>

    int main()
    {
    system("ls -al);
    return 0;
    }

    After you run it, it should do the same thing......

    Cheers
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    The solution is to read the file line by line, printing out each line, and pausing after 50-100 lines, whatever pleases you

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    
    	int lines=0;
    	ifstream input;
    	char line[255];
    	input.open("filename");
    	if(input.fail() )
    	{
    		cout << "error with file";
    		return 0;
    	}
    	do
    	{
    		input.getline(line,254,'\n');
    		lines++;
    		cout << line << endl;
    		if( lines % 50 == 0 ) // change this to display more or less lines before pausing
    		{
    			cout << "Press ENTER to continue " << endl;
    			cin.ignore(100,'\n'); 
    			//wait for the user to press enter
    			//the 100 is incase they enter junk chars before they press enter
    		}
    	}
    	while( ! input.eof() );
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  2. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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