Reading Data from File

Can anyone help me to retrieve data from the file. How do I display the appointment of Peter with fstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void searchRecords()
{
     ifstream someStream( "appointment.txt" );

    // Set up a place to store our data read from the file
    string line;
    cout<<"Please enter user";
    cin>>line;

    // Read and throw away the first line simply by doing
    // nothing with it and reading again
    getline( someStream, line );

    // Now begin your useful code
    while( !someStream.eof() ) {
        // This will just over write the first line read
        getline( someStream, line );
        cout << line << endl;
    }


Dexter, 10thOct12 - 9am , Training room

Peter, 15thoct 12 - 10am, discussion room 2

Jane, 10thOct12 - 11am, meeting room 1

Liam, 9thOct12 - 8:30am, meeting room 2

Parry, 8thOct12 - 9am, meeting room

Liam, 7thOct12 -12pm, Conference room

Richard, 10thOct12 - 9am , meeting room 1

Peter, 15thoct12 - 10am, meeting room 2

Jane, 3rdOct12 - 5pm, Lab 1
what errors are you getting or what do you not understand? you need to be very specific in your questions.
Currently, there are no errors.

// Set up a place to store our data read from the file
string line;
cout<<"Please enter user";
cin>>line;


After keying in user - Liam. I want just the result

Liam, 9thOct12 - 8:30am, meeting room 2

Liam, 7thOct12 -12pm, Conference room

but now it is showing everything

Dexter, 10thOct12 - 9am , Training room

Peter, 15thoct 12 - 10am, discussion room 2

Jane, 10thOct12 - 11am, meeting room 1

Liam, 9thOct12 - 8:30am, meeting room 2

Parry, 8thOct12 - 9am, meeting room

Liam, 7thOct12 -12pm, Conference room

Richard, 10thOct12 - 9am , meeting room 1

Peter, 15thoct12 - 10am, meeting room 2

Jane, 3rdOct12 - 5pm, Lab 1


- How do i write the code so that they only take the line with Liam
Anyone can help me ????

I need help with my assignment .. i can pay you if you can help me :(
I believe a big problem is you're overwriting the users name request request.
cin >> line; with getline( someStream, line );
You would need two strings, one for request, the other for the read files input. After each read line, have a loop to read from the beginning to the first comma, and assign a variable its content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string name, lookname, line;
// name = user inputted
// lookname = Assigned to length of line up to first comma
// line = lines of text in file being read
while( !someStream.eof() ) {
        // This will just over write the first line read
        getline( someStream, line );
       
for(int x=0;x<line.length();x++)
{
if(line[x] !=',')
 lookname[x]=line[x];
else
x=name.length(); // Ends search
}
if (lookname==name)
 cout << line << endl; // print out entire line that has name, date and appointment

    }


I didn't check this code for errors, since I don't have your full program, but you should be able to correct any problems it has.

Hope this helps..
cin>>line; is incorrect because your getting "line" below
getline( someStream, line );

change cin>>line; to cin >> UserName; for the name to search.

after your
getline( someStream, line );
You have no method to tell if UserName is part of "line".

1
2
// This is what your missing after getline
          if ((someStream = line.find(UserName, 0)) != string::npos)
Last edited on
thanks for your help .. there wasn't error , but after keying in the name. the program stops

sent you a PM whitenite
Last edited on
Topic archived. No new replies allowed.