Text Parser

Hi everyone, I am building a computer program in my free time that will hopefully become a full on program language translator. However right now all I am trying to do is parse a java file (any file would work though), and ignoring lines of code that would not work in C++, such as public static void..and so on. My problem is that I can only ignore one line at a time. The code is below, any help would be greatly appreciated.

#include <iostream>
#include <fstream>
#include<iomanip>
#include <string>

using namespace std;

int main(int argc,char *argv[])
{
string line, filename;
ifstream f;

filename = argv[1];


f.open(filename.c_str());
if (f.is_open())
{
while (f.good())
{
getline(f, line);
if(line == "public class Foo")
continue;
else if(line == "*")
continue;
else if(line == "*/")
continue;
cout << line << endl;
}
f.close();
}
else cout << "Unable to open file";
return 0;
}

And this is the file I was parsing (Foo.java):

public class Foo
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{

int a, b, c;
float y;
String str;
int [] z = new int [2];

a = 2;
b = 3;
c = a + b;

y = 100;

str = "Hello Converter!";

z[0] = 10;
z[1] = 20;
z[2] = 30;

System.out.println();
}

}

So what happens is only "public class Foo" gets ignored and nothing else. Its probably something simple I'm not seeing, so any advice?


try a for loop to use getline() for each line in the file

for(int i=0; i<lineNum; i++)
{
getline();
}
Ok that's what I was thinking, but I kept getting stuck on how to get the whole length of the file. I know the function getFileSize() will give the byte size of the file, but I want to do it by counting the lines and returning that number into an integer. I can do it in Java, but havent found anything like that for c++


Each line end with "enter character" . Using this we can find Line number / Parser the java file.
Last edited on
Topic archived. No new replies allowed.