Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Read a file but skip over comments "#"

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

0
missymadi
Asked:
missymadi
  • 2
1 Solution
 
jkrCommented:
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

0
 
missymadiAuthor 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
0
 
jkrCommented:
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now