I would like to know how i check the first character of each line before I either ignore it and move onto the next line or add it to an array.

What i am looking for is tags like <. If the line begins with that i move onto the next line until I find a line beginnging with a letter or number.

I got this code from a C++ book which has lots of very small examples but don't think it's right or something is missing but i don't know what.

This is my file which asks for a file which needs to be processed.

char FileName[20];
	string line;
	string sub1 = "<";
	string sub2 = " ";

	cout << "Please enter a filename\n";
	cin >> FileName;
	
	ifstream myfile(FileName);

	while(! myfile )
	{
		cout << "Unable to open file: " << FileName << endl;

		cout << "Please enter Gene Expression Matrix\n";
		cin >> FileName;
	}

	while (! FileName.eof()) //Loop through lines
	{
		getline(FileName, line);
	
		unsigned int pos1 = str.find(sub1, 0);
		unsigned int pos2 = str.find(sub2, 0);
		if(pos1 = string::npos)
			if(pos2 = string::npos)
				count << sub << "not found" << endl;
	}
	FileName.close();
	return 0;

Recommended Answers

All 2 Replies

You can just do the following

if(line[0] == '<' || line[0] ==' '){
   //some code
}

Chris

I got this code from a C++ book...

I think no books with such absurd codes ;)
For example:

char FileName[20];
...
ifstream myfile(FileName);
... // Is eof() a member of CHAR ARRAY FileName???
while (! FileName.eof()) //Loop through lines

Never, ever scan files with while (! file.eof()) loops! No eof flag settings at the start until the program reads anything. If you try to read an empty file file.eof() returns false but the next read do nothing!

string line; // avoid comments like "This is string line" (see below;)
...
while (getline(myfile,line))  { //Loop through lines
     // Now you have the next line here
}

Well, what happens if a user types filename with length >= 20? Right, your program crashed.
Never, ever get user input in a char array without buffer size check codes! Better use std::string and getline function:

string FileName;
...
if (!getline(cin,FileName)) { // eof on cin (Ctrl+Z. for example)
    // terminate program
}
ifstream myfile(FileName.c_str()); // get a pointer to string text
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.