Using filebuf::sh_read

This is a discussion on Using filebuf::sh_read within the C++ Programming forums, part of the General Programming Boards category; Ok, I think I've posted this question before but it never got replied to. My problem is that I want ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Using filebuf::sh_read

    Ok, I think I've posted this question before but it never got replied to. My problem is that I want to open a file with read and write sharing, using a std::ifstream, but I can't. I've seen in about 99999 places on the web, tutorials, books, MSDN, everywhere, this:

    std::ifstream file("filename.txt", ios::in, filebuf::sh_read);

    BUT, sh_read isn't a member of std::filebuf and I don't have just filebuf::sh_read because I'm using the new header, i.e. <fstream> instead of <fstream.h>. Including <fstream.h> as well allows me to use sh_read but then there are "no constructors that take 3 arguments" Is shared access even supported anymore, or was it supported to begin with?? And do I have to convert all my code to using Windows API files?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Using filebuf::sh_read

    Originally posted by Hunter2
    My problem is that I want to open a file with read and write sharing, using a std::ifstream, but I can't.
    Is this what you want?
    Code:
      char buf[100];
      fstream f("myfile.txt", ios::in | ios::out);
      
      f << "testing";
      f.seekg(0);
      f >> buf;
      cout <<buf <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It's very interesting, but unfortunately, no What I mean is, open it in this way:
    Code:
    std::ifstream file("theFile.txt"/*other parameters*/);
    //do some stuff, but don't close file
    std::ifstream file2("theFile.txt"/*other parameters*/);
    Or something along those lines, so that the second file opens properly as well. With WinAPI you would open it with FILE_SHARE_READ, right? I'm looking for an equivalent with ifstream, or fstream, whichever it works for. Or do I have it all wrong?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, to start with ifstream is for read only files, afaik. ostream is for writing, fstream can be used for either.

    As for opening the same file twice, I haven't tried, but I guess if you careful with the buffer flushing you should be OK, but I wouldn't guarantee it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Odd, I thought the second open failed when I tried. Maybe I got something wrong, I guess I'll have to do more testing.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    **Deleted post's previous contents**

    Heh... oops.. I didn't allow file sharing with the WinAPI calls. I'm all better now
    Last edited by Hunter2; 08-31-2003 at 10:16 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21