Read .txt into String

Pages: 12
Im new to all of this.
My problem is that I'm trying to find a specific text in a .txt file. Currently im opening the Text File as ifstream, then reading it with getline into a String.
Then im searching for the Text with find(), but there I got some problems.

The .txt File contains only 1 huge line, so I must read the hole file in one String. But then I got no result (0) when in try

cout << string.length();

Also, if i try to cut down the length of the String with

String.substr(int,int);

I got this error:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Abort trap

If I try to get the starting point of the Text I'm searching with

int start = String.find("text");

I got 0 or a verry huge number like 84672757843.

What am I doing wrong? Im on Mac OS X. Here is a part of my Code:

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
 string name;
            f.open("/tmp/get_video_info", ios::in);
            cout << "Opening File...\n";
            getline(f, name);
            while(!(name.find("title"))){
                cout << "Starting...\n";
                getline(f, name);
                if(name.find("title")){
                    cout << name.find("title") << "\n";
                    cout << name.length() << "\n";
                }
                else{
                    cout << "Still not found!\n";
                }
            }
            if(name.find("title")){
                //In this part
                unsigned int beginn;
                unsigned int ende;
                
                beginn = name.find("title");
                ende = name.length();
                
                cout << beginn << " " << ende << "\n";
                //Here I have to find the title
            }
            else{
                cout << "Something verry strange happened...\n";
                return 1;
            }
            
        }
    }
    


Hope you can help me. I need to extract the title out of the File.

Here is the File Content:

account_playback_token=&url_encoded_fmt_stream_map=url%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v17.lscache5.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dcp%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526itag%253D44%2526ip%253D79.0.0.0%2526signature%253D043543D5B6F6A75E4E493017D5F82A7E32863F5A.49D52EDB57B9031D085F9DB370C9F5C2159D1DC8%2526sver%253D3%2526ratebypass%253Dyes%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dlarge%26fallback_host%3Dtc.v17.cache5.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26itag%3D44%2Curl%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v7.lscache4.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dalgorithm%25252Cburst%25252Ccp%25252Cfactor%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526algorithm%253Dthrottle-factor%2526itag%253D35%2526ip%253D79.0.0.0%2526burst%253D40%2526sver%253D3%2526signature%253DCFB3443D3865199F5FD1E4079BB4C63AD218E0C1.0D8DAD948CFE6DBD448FFF00EF98EDBD5C9D2E01%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526factor%253D1.25%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dlarge%26fallback_host%3Dtc.v7.cache4.c.youtube.com%26type%3Dvideo%252Fx-flv%26itag%3D35%2Curl%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v1.lscache8.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dcp%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526itag%253D43%2526ip%253D79.0.0.0%2526signature%253D9E8A13086455B9544DC832EE306052F3AF009D20.286DBA1F8887712309F18D5C6A495017DD8319AE%2526sver%253D3%2526ratebypass%253Dyes%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dmedium%26fallback_host%3Dtc.v1.cache8.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26itag%3D43%2Curl%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v7.lscache3.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dalgorithm%25252Cburst%25252Ccp%25252Cfactor%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526algorithm%253Dthrottle-factor%2526itag%253D34%2526ip%253D79.0.0.0%2526burst%253D40%2526sver%253D3%2526signature%253DACDCAF6A4A06B959FE8DB425A44EF7002CDBFDAA.74A1315D2AD6AAD7B55C61B1088EDB941579488A%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526factor%253D1.25%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dmedium%26fallback_host%3Dtc.v7.cache3.c.youtube.com%26type%3Dvideo%252Fx-flv%26itag%3D34%2Curl%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v12.lscache5.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dcp%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526itag%253D18%2526ip%253D79.0.0.0%2526signature%253DA83EB142F8AA4142B6D092D155C0DAAB0BEA2ABE.192E038C23131099596C9FE01229C4CABF7DBB89%2526sver%253D3%2526ratebypass%253Dyes%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dmedium%26fallback_host%3Dtc.v12.cache5.c.youtube.com%26type%3Dvideo%252Fmp4%253B%2Bcodecs%253D%2522avc1.42001E%252C%2Bmp4a.40.2%2522%26itag%3D18%2Curl%3Dhttp%253A%252F%252Fo-o.preferred.ham02s03.v24.lscache1.c.youtube.com%252Fvideoplayback%253Fupn%253D0ARLrrmhOKM%2526sparams%253Dalgorithm%25252Cburst%25252Ccp%25252Cfactor%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Csource%25252Cupn%25252Cexpire%2526fexp%253D900148%2526algorithm%253Dthrottle-factor%2526itag%253D5%2526ip%253D79.0.0.0%2526burst%253D40%2526sver%253D3%2526signature%253D4BDEE32649637A73A46CC3A31F27749722841AE5.30FF0F7429570EC5B6E2FE8672CC7A8958996102%2526source%253Dyoutube%2526expire%253D1335214510%2526key%253Dyt1%2526ipbits%253D8%2526factor%253D1.25%2526cp%253DU0hSSlJMUl9GSkNOMl9KRlZGOloyczNocWNOQmlR%2526id%253D84b917106780148c%26quality%3Dsmall%26fallback_host%3Dtc.v24.cache1.c.youtube.com%26type%3Dvideo%252Fx-flv%26itag%3D5&allow_embed=1&vq=auto&fexp=900148&allow_ratings=1&keywords=stop+motion%2Cstop+animation%2Cstop+motion+animation%2Cstop+motion+drawing%2Canimation&track_embed=0&view_count=51368&video_verticals=%5B316%2C+1104%2C+3%5D&fmt_list=44%2F854x480%2F99%2F0%2F0%2C35%2F854x480%2F9%2F0%2F115%2C43%2F640x360%2F99%2F0%2F0%2C34%2F640x360%2F9%2F0%2F115%2C18%2F640x360%2F9%2F0%2F115%2C5%2F320x240%2F7%2F0%2F0&author=MrFoxyjocelyn&muted=0&length_seconds=102&plid=AAS-WbWEB-2HKZmb&tmi=1&ftoken=&status=ok&tabsb=1&watermark=%2Chttp%3A%2F%2Fs.ytimg.com%2Fyt%2Fimg%2Fwatermark%2Fyoutube_watermark-vflHX6b6E.png%2Chttp%3A%2F%2Fs.ytimg.com%2Fyt%2Fimg%2Fwatermark%2Fyoutube_hd_watermark-vflAzLcD6.png&timestamp=1335192413&has_cc=False&hl=de_DE&no_get_video_log=1&endscreen_module=http%3A%2F%2Fs.ytimg.com%2Fyt%2Fswfbin%2Fendscreen-vflu8-ZE9.swf&avg_rating=3.49275362319&video_id=hLkXEGeAFIw&sw=0.1&token=vjVQa1PpcFO9lR_HHiMPYL5OelOQ1XgZU0Z_FYKoa9E%3D&thumbnail_url=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FhLkXEGeAFIw%2Fdefault.jpg&title=Stop+Motion+Animation+Drawing
Last edited on
Nobody knows something?
Are you sure it really contains just that one line, no empty lines or something like that?
No, it is only one line without any emptys.
Well, this shouldn't be the cause of your problem, but

while(!(name.find("title"))
probably doesn't do what you want it to.

(same with

if(name.find("title"))

actually, though the bug that this would cause is very unlikely to happen).
Last edited on
Yes, i think I could remove that part.

But I can't find a solution for my problem.. is there no way to just read in 5 characters, check if they are those which I search and then continue?
Last edited on
Can you please comment the rest out and try

1
2
3
4

while(getline(f,name)) {
   cout << name << endl;
}


and post the result?
There is no output, just this..

Searching for Informations...
Opening File...
logout


Programm Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
      string name;
            f.open("/tmp/get_video_info", ios::in);
            cout << "Opening File...\n";
            while(getline(f,name)) {
                cout << name << endl;
            }
            if(name.find("title")){
                //If its found..
            }
            else{
                cout << "Something verry strange happened...\n";
                return 1;
            }
            
        }
    }
Last edited on
In that case... after f.open

 
cout << f.good()<< endl;
Thats the Output:

Searching for Informations...
0
Opening File...
logout
Well, there we have our offender. The file apparently doesn't get opened. Either the file doesn't exist (typo in your code?) or you don't have read permissions.
Oh okay thanks:)

But what could be the reason? The file is in the Temporary Folder.. And the file does exist..

I will try some things, thanks :)

--Edit

Oky now I git this Output:

Searching for Informations...
1
Opening File...
logout


This is the Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     string name;
            system("chmod 777 /tmp/get_video_info");
            f2.open("/tmp/get_video_info", ios::in);
            cout << f2.good() << endl;
            cout << "Opening File...\n";
            while(getline(f2,name)) {
                cout << name << endl;
            }
            if(name.find("title")){
                //If its found..
            }
            else{
                cout << "Something verry strange happened...\n";
                return 1;
            }
            
        }
    }


I had to change f to f2 and create a new ifstream.. But still I cant read the File or?
Last edited on
I have code to do this. I made a class that handles strings and characters and makes this very easy. But... that would be cheating wahahahahaha
Could you just give me an example or a hint how to do this? That would really help me.. I have no idea why my code does not work :(
Oopps. Wasn't thinking. Let me correct my post.
if(name.find("title")){
//If its found..
}


The value will always be something other than 0, as long as "title" is not the very first thing in the file.
I have implemented the exact code you gave.
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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main ( )
{
    string data;

    fstream f1;
    f1.open ( "text", ios::out );
    f1 << "hello title";
    f1.close();

    fstream f2;
    f2.open ( "text", ios::in );
    cout << f2.good ( ) << endl;
    cout << "Opening File...\n";

    while ( getline ( f2, data ) )
    {
        cout << data << endl;
    }

    if ( data.find ( "title" ) )
    {
        //If its found..
    }
    else
    {
        cout << "Something verry strange happened...\n";
        return 1;
    }

    return 0;
}


output
1
Opening File...
hello title
As you can see. Same s*** as yours. Except.. My code displays my files data.
Hmm.. but it still doesnt seem to work..

My result:

Searching for Informations...
1
Opening File...
Starts at --> 4294967295
Ends at --> 0
logout


My Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string name;
            system("chmod 777 /tmp/get_video_info");
            f2.open("/tmp/get_video_info", ios::in);
            cout << f2.good() << endl;
            cout << "Opening File...\n";
            while(getline(f2,name)) {
                cout << name << endl;
            }
            if(name.find("title")){
                unsigned int beginn = name.find("title");
                unsigned int ende = name.length();
                cout << "Starts at --> " << beginn << endl;
                cout << "Ends at --> " << ende << endl;
            }
            else{
                cout << "Something verry strange happened...\n";
                return 1;
            }
            
        }
    }


I want to make something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string name;
            system("chmod 777 /tmp/get_video_info");
            f2.open("/tmp/get_video_info", ios::in);
            cout << f2.good() << endl;
            cout << "Opening File...\n";
            while(getline(f2,name)) {
                cout << name << endl;
            }
            if(name.find("title")){
                unsigned int beginn = name.find("title");
                unsigned int ende = name.length();
                cout << "Starts at --> " << beginn << endl;
                cout << "Ends at --> " << ende << endl;
                string RealTitle = name.substr(beginn,ende);
                cout << "The Title is: " << RealTitle << endl;
            }
            else{
                cout << "Something verry strange happened...\n";
                return 1;
            }
            
        }
    }


But it does not work..

Here you can download the Text file im working with: http://www.youtube.com/get_video_info?video_id=hLkXEGeAFIw&feature=related
Last edited on
Ok.

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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main ( )
{
    string data;

    fstream theFile;
    theFile.open ( "text", ios::in );
    cout << theFile.good ( ) << endl;
    cout << "Opening File...\n";

    while ( getline ( theFile, data ) )
    {
        cout << data << endl;
    }

    if ( data.find ( "title" ) )
    {
        //If its found..
    }
    else
    {
        cout << "Something verry strange happened...\n";
        return 1;
    }

    return 0;
}


Outputs the entire file.
Does your code output the entire file?
Last edited on
Pages: 12