how many streams with a file?

Sep 14, 2014 at 4:29pm
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;
Sep 14, 2014 at 4:31pm
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.
Sep 16, 2014 at 3:16am
what do you mean by hitting?
Sep 16, 2014 at 12:46pm
By "Hitting" I mean tapping or utilizing. I am referring to either reading or writing to the storage device.
Sep 16, 2014 at 12:52pm
You can seek to any point in the file, why do you need two streams?
Sep 16, 2014 at 1:28pm
@ 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 Sep 16, 2014 at 1:28pm
Sep 16, 2014 at 1:49pm
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).
Sep 16, 2014 at 2:01pm
@ 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.
Sep 18, 2014 at 3:23am
Thanks for the replies, but its our tutor desires
Topic archived. No new replies allowed.