Help Need

i have a file max.asm. i want to remove all comments and blank lines from file.
i wrote this code,i removed the blank lines and comments that are at starting position but how can i remove comments that are in mid of the line.

1
2
3
4
5
6
7
8
9
10
11
12
13
string line;
	ifstream in("Max.asm");

	while(getline(in,line))
	{
		if(!line.empty())
		{
			if(!line.find_first_of("//") == 0)
			{
				cout<<line<<endl;
			}
		}
	}



// This file is part of the materials accompanying the book
// "The Elements of Computing Systems" by Nisan and Schocken,
// MIT Press. Book site: www.idc.ac.il/tecs
// File name: projects/06/max/Max.asm

// Computes M[2] = max(M[0], M[1]) where M stands for RAM
@0
D=M // D=first number
@1
D=D-M // D=first number - second number
@OUTPUT_FIRST
D;JGT // if D>0 (first is greater) goto output_first
@1
D=M // D=second number
@OUTPUT_D
0;JMP // goto output_d
(OUTPUT_FIRST)
@0
D=M // D=first number
(OUTPUT_D)
@2
M=D // M[2]=D (greatest number)
(INFINITE_LOOP)
@INFINITE_LOOP
0;JMP // infinite loop
When you use the function "getline(...)" you can set the character delimiter: http://www.cplusplus.com/reference/iostream/istream/getline/ In this case you would do what the compiler does and ignore everything after "//" on the line.
thanks for reply

but it did't worked.it ignores all before "//" and remaining part prints in next line.give me another solution
Sorry you need to use and embedded loop so that your program keeps reading the file. Once you see the "//" characters use the "ignore(...)" function to discard everything up until the new line: http://www.cplusplus.com/reference/iostream/istream/ignore/

Pseudo-Code:
1
2
3
4
5
6
7
8
9
while(in.is_open())
{
     while(in.getline(line, '//'))
     {
          //Do Something
     }
     
     in.ignore(256, '\n');
}


Or something like that.
Last edited on
no no it also did not worked
Last edited on
pls give me code for this if possible or give me its exact solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main(){
    string Result;
    string line;
    ifstream infile ("Max.asm");

    while (!infile.eof()){
        getline(infile,line);
        int i = line.find("//");
        if (i==0) continue;
        Result += line.substr(0,i) + '\n';
    }
    cout << Result;
}
Thank you very much.....problem solved
Topic archived. No new replies allowed.