Get string from file question

Ok so i have a string in a file which looks like this:

print "hello";

how do i get my program to only get whats in between the ""?

i want it to read the print as a function to output this to screen.

here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

    string stuff;

    ifstream file("hello.txt");
    getline(file,stuff);


    if(stuff == "Print" || stuff == "print"){
        cout << "what do you want to print?";
    }

    return 0;
}


this code works at getting the print and when it gets it it outputs a line of text, but i want it to output the line of text in between the "".
Analyze that:
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
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
FILE *f;
int main()
{
    f=fopen("hello.txt", "r+");
    char str[1000], vector[1000]; int i, j;
    string p;
    fgets(str, 1000, f);
    for(i=0; str[i]!='"'; i++)
    {
        vector[i]=str[i];
    }
    vector[i]=NULL;
    p=vector;
    if(p=="print ")
    for(j=i+1; str[j]!='"'; j++)
    {
        cout<<str[j];
    }

    fclose(f);
    return (0);


}


If you want to read from a file a string and to use it (to compare it with a constant string), the form of a line must be constant. And the lines in that file must be (for example): print "hello"
it mustn't has another form, like: print"hello" or print "hello".
The thing is that you can't read just a part of a line from a file, you have to read all the line and to store it in a char-vector so that you can work with it. In the program below, I read all the line then I store the first part of the line (the "print" -- the function name) in a vector. Then I transform that line from a char-vector in a string, so that I can compare it with a constant string: if(p=="print"). The name of that function (print) must be wrote in that file in a single way: print or PRINT. Or if you want that this name to be pRInt or PRInt, you can use the toupper macro in a loop and to compare then with "PRINT". Toupper is a macro that converts all the characters in upper caracters: pRint converts in PRINT. G-luck!
Topic archived. No new replies allowed.