VB icon

A Simple Text Viewer 1.0

Email
Submitted on: 9/28/2016 10:52:03 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1596
 
     Here is a simple Win32 console text viewer, you can set a number of rows to show and will pause till a key is hit then display the rest, You can also clear the screen on start up and also set the display text case. Hope you like the first version comments welcome.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Simple Text Viewer 1.0
// Description:Here is a simple Win32 console text viewer, you can set a number of rows to show and will pause till a key is hit then display the rest, You can also clear the screen on start up and also set the display text case. Hope you like the first version comments welcome.
// By: Ben
//**************************************

// Console TextViewer 1.0
// By Ben a.k.a DreamVB
// 23:46 28/09/2016
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <algorithm>
using namespace std;
using std::cout;
using std::endl;
string UCase(string src)
{
	string Temp = src;
	std::transform(Temp.begin(), Temp.end(), Temp.begin(), ::toupper);
	return Temp;
}
string LCase(string src)
{
	string Temp = src;
	std::transform(Temp.begin(), Temp.end(), Temp.begin(), ::tolower);
	return Temp;
}
int main(int argc, char *argv[]){
	
	string src_file = "";
	string StrLine = "";
	string StrTemp = "";
	string parm = "";
	fstream fs;
	int pos = string::npos;
	int i = 2;
	int Rows = 10;
	int CurRow = 1;
	//Flag vars
	bool DispUpper = false;
	bool DispLower = false;
	bool ClearSrc = false;
	if (argc < 2){
		cout << "Usage: " << argv[0] << " <filename>" << endl;
		exit(1);
	}
	//Get any other parms
	while (i < argc){
		if (argv[i][0] == '/'){
			//Extract parm
			parm = LCase(argv[i]);
			pos = parm.find_first_of(':');
			if (pos != string::npos){
				//Extract value.
				StrTemp = parm.substr(pos + 1);
				//Fix the line
				parm.erase(pos);
			}
			//Set ammount of rows to show before pause.
			if (parm == "/rows"){
				Rows = atoi(StrTemp.c_str());
			}
			//Check if displaying in uppercase
			if (parm == "/u"){
				DispUpper = true;
			}
			//Check if displaying lowercase
			if (parm == "/l"){
				DispLower = true;
			}
			//Check if clearing screen on first load
			if (parm == "/c"){
				ClearSrc = true;
			}
		}
		i++;
	}
	//Get filename
	fs.open(argv[1], ios::in);
	
	//Check if file was opened.
	if (!fs.good()){
		cout << "Cannot read file: " << endl;
		cout << argv[1] << endl;
		exit(1);
	}
	
	//Check if clear screen is enabled
	if (ClearSrc){
		system("cls");
	}
	//Read in the file.
	while (!fs.eof()){
		//Read in line
		std::getline(fs, StrLine);
		//Check if current line is greator than total of display lines.
		if (CurRow > Rows){
			//Wait for a key press
			getch();
			CurRow = 1;
		}
		//Convert to uppercase.
		if (DispUpper){
			StrLine = UCase(StrLine);
		}
		//Convert to lowercase.
		if (DispLower){
			StrLine = LCase(StrLine);
		}
		//Output the line
		cout << StrLine.c_str() << endl;
		//INC counters
		CurRow++;
	}
	//Close file.
	fs.close();
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.