943,148 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1104
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 7th, 2010
0

getline string literal from file C++

Expand Post »
Totally new to C++, so please forgive me...

I'm trying to have a user input a filename and then check the file for all string literals.

The output should return the string literal (not the whole line, which is what I am getting).

Also, I have to ensure it ignores comments that include quotations.

I've tried doing a: line.find("\""); to get the string literals but pulling my hair out over here!

HELP! Your expert guidance please!!

C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int main( int argc, char* argv[] ) {
  9.  
  10. //Prompt to open file, loop until proper file is input...
  11. ifstream infile;
  12. string filename;
  13.  
  14. do {
  15. cout << "Enter file name:";
  16. cin >> filename;
  17. infile.open(filename.c_str());
  18. if (!infile) {
  19. cerr << "Cannot open file: " << filename << "\n" << endl;
  20. }
  21.  
  22. } while (!infile);
  23.  
  24.  
  25. string line;
  26. int counter = 0;
  27.  
  28.  
  29.  
  30. while( getline(infile,line) ){
  31.  
  32. // Counter for line number(s)...
  33. ++counter;
  34.  
  35.  
  36.  
  37.  
  38. // Check for string literal(s)...
  39. std::string::size_type pos = line.find("\"");
  40. if(pos != std::string::npos){
  41. std::string sentence = line.substr(pos);
  42.  
  43. // Print out filename, line number(s), and "string literal"
  44. cout << filename << " line # " << counter << ", " << sentence << "\n";
  45. }
  46. }
  47.  
  48.  
  49. infile.close();
  50. system("pause");
  51. return 0;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamfabian is offline Offline
2 posts
since Feb 2010
Feb 7th, 2010
0
Re: getline string literal from file C++
Can you give a typical line from the file?
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 8th, 2010
0
Re: getline string literal from file C++
Click to Expand / Collapse  Quote originally posted by jonsca ...
Can you give a typical line from the file?
Yes, thanks for your help...


here is the sample input file (hello.cpp)


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main() {
  4. std::cout << "Hello world!\n" << std::endl;
  5.  
  6. // "Have window stay open on viewing"
  7. char cChar;
  8. std::cin.get(cChar);
  9. /* "THIS IS A TEST COMMENT" */
  10. return 0;
  11. }
Last edited by iamfabian; Feb 8th, 2010 at 12:13 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamfabian is offline Offline
2 posts
since Feb 2010
Feb 8th, 2010
0
Re: getline string literal from file C++
You will need to either tokenize the lines and get rid of anything after // or between /* */
(You may be able to base what you do off of something like this: http://www.daniweb.com/code/snippet217441.html)

-or-
You can write a finite state machine similar to the ones that a compiler would use to detect and strip comments from a file (or even run your source file you are testing through the C++ preprocessor -- command varies from compiler to compiler -- to strip the comments before you run this program. I am not sure if that will preserve line numbers).

-or-
You can use a regex library (e.g. Boost Regex )and do something like this. Perhaps a bit of overkill but it's the same issue.

I wish I could have been of more help to you with an algorithm. I tried writing a bit of a finite state machine with just if statements but it got a bit cumbersome and I'm sure there are more efficient/faster ways of doing it.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 8th, 2010
0
Re: getline string literal from file C++
Oh, c'mon, jonsca
Click to Expand / Collapse  Quote originally posted by jonsca ...
You will need to either tokenize the lines and get rid of anything after // or between /* */

-or-
You can write a finite state machine similar to the ones that a compiler would use...

-or-
You can use a regex library...
He said
Click to Expand / Collapse  Quote originally posted by iamfabian ...
Totally new to C++, so please forgive me...
He can't even find a double quote in the line he's read, how is he going to add a regex library, or even know what a state machine is? I myself never heard of a state machine until my girlfriend was getting her masters.

Know your audience...

Sheesh!
Moderator
Reputation Points: 3275
Solved Threads: 888
Posting Sage
WaltP is offline Offline
7,704 posts
since May 2006
Feb 8th, 2010
0
Re: getline string literal from file C++
Quote ...
Oh, c'mon, jonsca
Quote ...
Know your audience..
Noted. I didn't come up with anything earth shatteringly simple, so sometimes you need an elephant gun as a fly swatter.
Personally, I think the preprocessor idea is the least difficult of all but I didn't try it to see if the line numbers are preserved. That way you don't have to worry about the comments because they are gone.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 8th, 2010
0
Re: getline string literal from file C++
All you have to do is find the ", and if found, find the next ". Copy the stuff in between. It's simple.

Tokenize... sheesh!
Moderator
Reputation Points: 3275
Solved Threads: 888
Posting Sage
WaltP is offline Offline
7,704 posts
since May 2006
Feb 8th, 2010
0
Re: getline string literal from file C++
This looks at lot like a template code that someone is asking you to modify without you having understood the parts.

I am not quite sure why you would want to output a double quote in real life as it is not a useful piece of information

but you want to show something
C++ Syntax (Toggle Plain Text)
  1. std::string str = "tetron";
  2. cout << str;
This will output the word tetron
but you really want to add in an extra line and tell it to print at the
same time so you should use a special cout variable called endl;

There are a couple of points with the original code I am surprised
by there is a c-style main declaration reading in argv[] when you
don't need parameters and if you use endl; you can get rid of system.(pause);

C++ Syntax (Toggle Plain Text)
  1. #include <string> //words and sentences
  2. #include <iosmtream> //cout & cin
  3.  
  4. void main()
  5. {
  6. //set the value of line as you initialise it
  7. std::string line("I answered, \"no.\" Now the nest sentence.");
  8. char to_find = '"';
  9. //we want a special int to avoid awarning but just like int pos;
  10. std::string::size_type number_of_letter_in_line(0), pos(0);
  11.  
  12. //you can cout a " by using << with to find
  13. std::cout << to_find << std::endl;
  14.  
  15. //find() starts looking at a letter_number you give it
  16. line.find(to_find, pos); //starts looking at I
  17. std::cout << "found " << to_find << " at " << pos << std::endl;
  18. //this will give you 0 if found which is wrong
  19. pos = line.find(to_find, pos);
  20. std::cout << "found " << to_find << " at " << pos << std::endl;
  21. //now we have a new pos that is the location in the string
  22. char quote = line[pos];
  23. //to find the next quote you would need to advance pos
  24.  
  25. }

you should be able to use the rest of your code with this
the std:: are in place of using namespace std;
Reputation Points: 32
Solved Threads: 37
Junior Poster
tetron is offline Offline
199 posts
since Feb 2010
Feb 8th, 2010
0
Re: getline string literal from file C++
Click to Expand / Collapse  Quote originally posted by WaltP ...
All you have to do is find the ", and if found, find the next ". Copy the stuff in between. It's simple.

Tokenize... sheesh!
It's the fact that there may be string literals within the comments that he wants to ignore. /* "So if I"m parsing for quotes I'm gonna get this one" */ So you could just look for // and /* and lop off the rest of the line, but you'd miss something like "correctone" in function(/* "nevermind" */"correctone"), hence having to have some idea where your comments begin and end (enter the tokenizer).

If you were guaranteed there to be no string literals within the comments (my preprocessor idea) then it would be as simple as you propose!
Last edited by jonsca; Feb 8th, 2010 at 2:03 pm.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 8th, 2010
0
Re: getline string literal from file C++
1) check the file for all string literals

Can string literals include embedded quotations? If so, then the project becomes harder.

2) Also, I have to ensure it ignores comments that include quotations.

Again, you can use a brute force method or use find() on an STL string to find either the \\ or the \* and then see if there are quotation marks between there and the end of the line or the */, depending on which type of comment is found.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: keyboard hook for C++
Next Thread in C++ Forum Timeline: Dynamically creating multiple arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC