I am readin a .txt file Line for Line and what I am interested to do is to identify that if the Line do contains any characters as Letters, text, symbols.
Exceptions is things like: '\n' ' ' etc... so "" wont be possible here ?
So I am trying to identify if the line contain any "Real Charaters"/typed characters from the keyboard.

I have started some code and wonder if this is possible to do in the if-statement:
if( Line == ? ) // (If the line do contains "Real Characters" typed from the keyboard)

std::string Line;

ifstream ReadFile("C:\\File1.txt);

while ( getline(ReadFile, Line, '\n')
{
	
	if( Line == ? )
	{
		//Do Stuff
	}
}

Recommended Answers

All 5 Replies

I am readin a .txt file Line for Line and what I am interested to do is to identify that if the Line do contains any characters as Letters, text, symbols.
Exceptions is things like: '\n' ' ' etc... so "" wont be possible here ?
So I am trying to identify if the line contain any "Real Charaters"/typed characters from the keyboard.

I have started some code and wonder if this is possible to do in the if-statement:
if( Line == ? ) // (If the line do contains "Real Characters" typed from the keyboard)

std::string Line;

ifstream ReadFile("C:\\File1.txt);

while ( getline(ReadFile, Line, '\n')
{
	
	if( Line == ? )
	{
		//Do Stuff
	}
}

I'd say first define exactly what the criteria is for whatever it is that you are doing. It may involve going through Line one character at a time and checking against a list of "approved" ascii values:
http://www.asciitable.com/
The functions from the cctype library could come in handy too.
http://www.cplusplus.com/reference/clibrary/cctype/
Depending on what you define as "typable", I guess that might potentially be considered Ascii values 32 through 126.

I doubt you'll want to use Line == in your code. Probably have some boolean function that receives a string and determines whether that string is "legal" by whatever criteria you set up. That function would traverse the string, checking for "legal" characters.

> So I am trying to identify if the line contain any "Real Charaters"/typed characters from the keyboard.
Your idea of "Real Characters" and C++'s idea are probably different. If one of the cctype functions searches for what you want, you can use that along with the STL:

#include <algorithm>

...

if (std::find_if(Line.begin(), Line.end(), std::isalnum) != Line.end())
  std::cout << "Found \"Real Characters\"\n";

Ed suggests isalnum(), because that sounds closest to what you mean by "Real Characters". If none of the cctype functions work for you, you can still define a white-list of characters and search for each of them directly:

const std::string whiteList = "0123456789ABCDEFabcdef";

if (Line.find_first_of(whiteList) != std::string::npos)
  std::cout << "Found \"Real Characters\"\n";

>> const std::string whiteList = "0123456789ABCDEFabcdef";

I have tried this approach out and it seems to work fine for this purpose.
As I understand it searches for "separate characters in this string like 0 or 1 or A or a etc...
as long as the end of the line isn´t reached (::npos)

It searches for any of the characters in the whiteList string. If none of those characters are found, find_first_of returns string::npos. If one of the characters is found, find_first_of returns the index of the character in the line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.