Finding "strings" in .cpp files

For a project i need to find strings in double quotes and output them to screen, My problem is it finds the first double quote in '"', causing it all to go to hell from there and giving me this output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"' )
			break;
	}

	//store characters untill the end is found
	string buffer;

	buffer += ch;

	while(in.get( ch ) )
	{
		if( ch == '"
"stringfinder.cpp"
"output.txt"


I can't think of how to get rid of any '"' in the .cpp file before searching for strings. Any ideas plz?
Well, those particular double-quotes are within single-quotes, so you could check for that. You'll need to keep the last char read.
And don't forget that '\"' doesn't end a string, either. Nor does '\''.
You've actually got a really hairy task. Is this homework?
Heh, this was fun. I went and wrote myself a little program to dump all the string literals (with their starting line numbers) found in a C or C++ source file.

There are three things you need to watch out for:

1) commentary.

/* this is "a comment" */
"but /* this */ is not "


2) implicitly quoted characters

'"'


3) explicitly quoted characters

\"


This category also includes the infamous

"this string begins on one line, \
but ends on the next"

I handled this by converting the quoted-newline literal into a newline escape:

"this string begins on one line, \nbut ends on the next"


There is also the case of string literal concatenation

"this string begins on one line, "
"but ends on the next"

My program treats them as distinct string literals even though the compiler will combine them into a single string constant, since they are distinct literals in the source code. I think this is the correct choice, but you may disagree. How you handle it is up to you.


[edit] Oh, you must also handle the case where the source file contains errors -- and complain if you hit the end of the file before terminating a string constant.

Hope this helps.
Last edited on
Ya this is homework. But anyway i figured it out, but have another problem, lets say there is a string "Say \"hello\" to everyone" how do i find the backslash, cuz '\\' doesn't seems to be working for me.
Hint:
1
2
3
4
  string::size_type index;
  ...

  index = s.find_first_of( "\"\'\\/", index );
im not having trouble ignoring '"'. i cant find the backslash \" what is the character used for it is it '\\', cuz that aint workin for me
Nevermind i fixed that too, thanks for all the suggestions, and i have another question, how can u show the line number that the string was on?
Last edited on
Counting lines.
Sorry, I forgot you were reading the file one character at a time. I was reading it a line at a time.

Every time you read a newline, increment you line counter.
Topic archived. No new replies allowed.