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!!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main( int argc, char* argv[] ) {
//Prompt to open file, loop until proper file is input...
ifstream infile;
string filename;
do {
cout << "Enter file name:";
cin >> filename;
infile.open(filename.c_str());
if (!infile) {
cerr << "Cannot open file: " << filename << "\n" << endl;
}
} while (!infile);
string line;
int counter = 0;
while( getline(infile,line) ){
// Counter for line number(s)...
++counter;
// Check for string literal(s)...
std::string::size_type pos = line.find("\"");
if(pos != std::string::npos){
std::string sentence = line.substr(pos);
// Print out filename, line number(s), and "string literal"
cout << filename << " line # " << counter << ", " << sentence << "\n";
}
}
infile.close();
system("pause");
return 0;