#### Archived

This topic is now archived and is closed to further replies.

This topic is 5068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to strip the code of all comments. However, as you can see, it doesn't work right. Anyone know the problem?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
string str = "", main = "";
ifstream in("file.txt");
int pos;
while(!in.eof()) {
in.getline((char *) str.c_str(), 255);
pos = str.find("//");
if(pos != string.npos) {
main += str.erase(pos, str.length());
} else {
main += str;
}
main += '\n';
}
cout << main << endl;
in.close();
cin.get();
return 0;
}  
I'm running it with this as file.txt:
  one; // comment 1
two;

r;
four;  
[edited by - Nerothos on April 5, 2004 11:23:48 PM]

##### Share on other sites
What actually happens with the given input ? I suspect that on lines where you don''t strip the comment you''re going to get an extra new line (so all the lines will end up spaced out). I''m also not convinced about the way you''re using in.getline - you shouldn''t access str.c_str() for writing; I usually would do something like this, if it needed to be in a string:
char    line[255];in.getline(line, 255);str.assign( line );
Not elegant, but this has always worked fine for me and is definatelly legal (incidentally, the const declarator that you had to override with a cast to get it to work is ment to produce a compiler error when you try writing to something you shouldn''t - pay attention to the compiler errors!!).

##### Share on other sites
Change:
while(!in.eof()) {		in.getline((char *) str.c_str(), 255);

To:
while(getline(in, str)) {

EDIT: Tried to compile it and found some other things.
Change if(pos != string.npos) { to if(pos != string::npos) { and don't use main as a variable name.

[edited by - Beer Hunter on April 6, 2004 4:50:20 AM]

##### Share on other sites
quote:
Original post by Nerothos
in.getline((char *) str.c_str(), 255);
Just. Plain. Wrong.

c_str() returns a const char *, meaning it is not there for you to write to but to read from. As the AP noted, pass the entire string.

Also note that getline doesn''t read in newlines, so you might want to insert one in main (you might also want a different variable name, for readability) with each line.

##### Share on other sites
Man, I really spent no thought time on hacking that together! Thanks, I''ll try all that right now.

##### Share on other sites
Great! Thanks for all the help!

##### Share on other sites
btw C++ is not the tool for this. Sure it''s possible, but you''ll find it much easier with perl.

##### Share on other sites
quote:
Original post by ChaosEngine
btw C++ is not the tool for this. Sure it''s possible, but you''ll find it much easier with perl.

btw perl is not the tool for this. Sure it''s possible, but you''ll find it much easier with C/YACC/Lex. Just offering a bit of perspective. The OP may be writing a compiler, in which case perl would be a horrible choice. Anyhow, the problem is trivial enough that no language has a real advantage.

##### Share on other sites
quote:
Original post by haro
quote:
Original post by ChaosEngine
btw C++ is not the tool for this. Sure it''s possible, but you''ll find it much easier with perl.

btw perl is not the tool for this. Sure it''s possible, but you''ll find it much easier with C/YACC/Lex. Just offering a bit of perspective. The OP may be writing a compiler, in which case perl would be a horrible choice. Anyhow, the problem is trivial enough that no language has a real advantage.

good point and well made. I take my hat off to you sir...however I too was trying to provide a bit of perspective as many people on this forum believe there are no other languages apart from C++!