We help IT Professionals succeed at work.

Read a file but skip over comments "#"

missymadi
missymadi asked
on
Medium Priority
626 Views
Last Modified: 2012-05-06
Experts,

      I am reading a file into a C++ program but I want to skip over comments listed in the file. Right now the program is trying to process the comments - which is incorrect.
I tried using double quotes but that also throws an error.

Thanks, MissyMadi
void ReadInputFile()
{
	ifstream ifs("project1.txt");
	string line;
	string processName;
	int tickCount;
 
	while(getline(ifs,line))
	{
		if(line[0]!='#')  //right here is where it's not supressing the comments...
		
		processName = line;
		
		if(getline(ifs,line))
		{
			tickCount = atoi(line.c_str());
		
		}
 
		cout << "[" << processName << " " << tickCount << "]" << endl;
		
		// Create a new PCB object and assign to it the values just read in
		PCB *p = new PCB();
		p->name = processName;
		p->tickCount = tickCount;
 
		// Push the new PCB object on the Long Term Wait Queue
		LTWQ.push(*p);
	}
}

Open in new window

Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You could just use 'continue' to skip these lines, e.g.
void ReadInputFile()
{
        ifstream ifs("project1.txt");
        string line;
        string processName;
        int tickCount;
 
        while(getline(ifs,line))
        {
                if(line[0]=='#')  continue; // skip comment lines
                
                processName = line;
                
                if(getline(ifs,line))
                {
                        tickCount = atoi(line.c_str());
                
                }
 
                cout << "[" << processName << " " << tickCount << "]" << endl;
                
                // Create a new PCB object and assign to it the values just read in
                PCB *p = new PCB();
                p->name = processName;
                p->tickCount = tickCount;
 
                // Push the new PCB object on the Long Term Wait Queue
                LTWQ.push(*p);
        }
}

Open in new window

Author

Commented:
Thanks! Worked great!

At the end of my file there is an * for EOF. Again the text file is trying to process the *. How do I skip over this symbol?

Thanks, MissyMadi
CERTIFIED EXPERT
Top Expert 2012
Commented:
Just in a similar way, e.g.
void ReadInputFile()
{
        ifstream ifs("project1.txt");
        string line;
        string processName;
        int tickCount;
 
        while(getline(ifs,line))
        {
                if(line[0]=='#')  continue; // skip comment lines
 
                if(line[0]=='*')  break; // stop at end of file
                
                processName = line;
                
                if(getline(ifs,line))
                {
                        tickCount = atoi(line.c_str());
                
                }
 
                cout << "[" << processName << " " << tickCount << "]" << endl;
                
                // Create a new PCB object and assign to it the values just read in
                PCB *p = new PCB();
                p->name = processName;
                p->tickCount = tickCount;
 
                // Push the new PCB object on the Long Term Wait Queue
                LTWQ.push(*p);
        }
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.