reading in from a file to a certain point

Apr 26, 2011 at 12:17am
hi all, question here about how to most efficiently do some file reading.

I have a txt file, and must read through the data until i hit
a certain word (we'll call it "word" in this case)

so file would look something like this

datadatadata
datadatadata
datadatadata
word
datadatadata
datadatadata

i was doing

while (infile != "word")
{
do task
}

but that didn't work.

Thanks for any advice
Apr 26, 2011 at 6:01am
although ugly, it gets the job done, considering "word" is what your looking for =P


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>
using namespace std;

void readnote()
{
    char* filename[256];
    cout << "Your note: \n" << endl;
    *filename=((char*)"sunday.txt"); // or whatever file you want to read from
    ifstream filein(*filename);
    char b[256];
    for(;;)
    {
       filein >> b;
       if (b[0]=='w')
       {
           if (b[1]=='o')
           {
               if (b[2]=='r')
               {
                   if (b[3]=='d')
                   {
                       return;
                   }
               }
           }
       }

       if (filein.fail()) break;
       cout << b << " ";
       if (filein.eof()) break;
    }
    return;
}
int main()
{
    readnote();
    return 0;
}
Apr 26, 2011 at 6:03am
you may edit it to your liking/usage

EDIT:: i got this function from one of my programs, and edited it to answer your question. so if it doesnt work right, just let me know
Last edited on Apr 26, 2011 at 6:05am
Topic archived. No new replies allowed.