Print String Literals From Source?

Hey guys I'm new here. Anyway. I have been trying to figure out how to do this for a while now. Without flat out just giving me the answer can you explain to me how this is accomplished?

Here is the code I have to start with:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char* argv[] ) {
    ifstream infile( argv[1] );
    char ch = 0;
    while( infile.get( ch ) ) {
        cout.put( ch );
    }
    return 0;
}


Thanks for the help in advance guys!
std::cout <<"This is a string literal.";
I know what a string literal is. What the program is supposed to do is scan a source code file and print out all of the strings to the console. It also has to print the line number.

Thanks
Well, that's a tough one. It almost involves parsing C. Look at this:
Things that are string literals:
1
2
3
4
5
"string"
"str""ing"
"str\
ing"
"/*string*/"

Things that aren't string literals:
1
2
3
4
5
//"string"
/*"string"*/
//\
"string" <-- note: syntax highlighting is broken
/*"*/string/*"*/
Last edited on
wow, and to think that this is only the second assignment I've had in my c++ class. Anyway, how can I get started? I basically want to output anything that is in between quote but I do not know how to test for that. Maybe do an if (ch=='"') and then inside the if statement have while (ch != '"')

I need all the help I can get on this one.

Thanks
Also, how can I output what line of code I am on?

Thanks
You can read a file line by line like this:
1
2
3
std::ifstream file("foo.c");
std::string s;
std::getline(file,s);

Then it all comes down to just matching pairs of quotes of quotes and line counting.
I cannot seem to get this code to work. Here is what I have:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
    ifstream file( "stringfinder.cpp" );
	string s;
	getline (file, s);
}
ok nvm I was missing an include. How can I make this loop until the end of file though? I was thinking of using a while loop somehow?

Thanks
1
2
3
4
5
6
7
ifstream fin( "file.txt" );
string line;
while( getline( fin, line ) )
{
    // deal with line
}
fin.close();
Last edited on
okay I am really close to being done now I think! At the moment I have the entire source code being printed with the line numbers and everything. Now I only need to know one more thing.

After using getline, how can I scan each character so that I can check for a quotation?

Thanks
okay I am really close to being done now I think! At the moment I have the entire source code being printed with the line numbers and everything. Now I only need to know one more thing.

After using getline, how can I scan each character so that I can check for a quotation?

Thanks
okay I am really close to being done now I think! At the moment I have the entire source code being printed with the line numbers and everything. Now I only need to know one more thing.

After using getline, how can I scan each character so that I can check for a quotation?

Thanks
okay I am really close to being done now I think! At the moment I have the entire source code being printed with the line numbers and everything. Now I only need to know one more thing.

After using getline, how can I scan each character so that I can check for a quotation?

Thanks
Thanks for that link. I think I am really close but still cannot seem to figure it out.
Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() 
{
	int numLines = 0;
	char filename[100] ="";

	cout << "Please enter a file name: ";
	cin >> filename;

    ifstream infile( filename );
	string s;
	

	while (!infile.eof())
	{
		numLines++;
		getline(infile, s);
		char ch = 0;
		while(infile.get( ch )) 
		{
			numLines++;
			getline(infile, s);
			 if (ch='"')
			 {
				 cout << filename << ":" << numLines << ": " << ch;
				while (ch!='"')
				{
					cout<<"in the while";
				 cout.put (ch);
				}
				cout << "\n";
			 }
		}

	}
}
Topic archived. No new replies allowed.