Thread: read binary file

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41

    read binary file

    Hello,
    Why does not this work
    Code:
    ifstream dtmfile (filename, ios::in|ios::binary|ios::ate);
    when filename is declared as a string and read with getline (file,filename); when this works
    Code:
    ifstream dtmfile ("dem.bin", ios::in|ios::binary|ios::ate);
    string filename is holding "dem.bin", still it does not work.. what do I miss?
    cheers

  2. #2
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    maybe I should have included my compilation error..
    error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert
    parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since the std::string came in relatively late in the standard library.

    So, the iostream open interface takes a C-style string, not a std::string. Use std::string::c_str().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Thanks for your reply. How can I use a std::string::c_str()? I declared it as string filename.
    Would you recommend reading a mixed text and number file in another way? Say fgets or something.
    Cheers

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    I was talking about the binary file of course, and thinking about another thing. forget the last part with mixed number and text

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, filename.c_str() will give you a C style string (const variant, so you can't change it - but that is fine for opening a file).

    Since you haven't posted what your file-reading code actually does, I can't comment on how you go about reading (or writing).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 04:54 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM