how many streams with a file?

I wanted to know if i could use two streams with a file

fstream fS;
ifstream read;

fs.open("text.txt", ios::in | ios::out | ios::app);
read.open("text.txt", ios:: in);

my aim is to read a record from text.txt in to fs and compare it with other records of text.txt by read

1
2
3
4
5
6
7
8
bool checkID(char userID, inStream read);
  read.open("text.txt", ios::in);
   while (read.good()){
     read.getline(id, size);
     if(strcmp (id, userID)==0)
        return true;  
}
    return false;
Unless "text.txt" is somehow too large, you should read the whole file into memory and do your comparison from there. Hitting secondary storage is expensive and should be avoided.
what do you mean by hitting?
By "Hitting" I mean tapping or utilizing. I am referring to either reading or writing to the storage device.
You can seek to any point in the file, why do you need two streams?
@ LB: Why would you work with the file on the disk? Why not work with it in memory like a sane person would? Even if the file is too large I would suggest a file map over using the "std::istream.seek()" function.
Last edited on
Because of this:
csstudent123 wrote:
my aim is to read a record from text.txt in to fs and compare it with other records of text.txt by read
He can read the record he wants, then seek to the beginning and read the whole file normally. Basically the same as what you're saying but without the requirement to store everything in memory (it is now optional).
@ LB: "Six of one..." I guess.

@ OP: There you go, the answer to your question is technically a 'yes' but either option presented here would be less awkward to work with.
Thanks for the replies, but its our tutor desires
Topic archived. No new replies allowed.