Thread: No Matching Function Error When Opening File

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't use sizeof(string) to determine the space needed on the file to store a string. You should use a fixed size space for that - there would be several different possible choices here, but you do need to use some method to define how large your string is.

    Also, it is not absolutely necessary to use a fixed size record to be able to random access the file - however, if the records are NOT all the same size, then you would need some other way (such as an array of integer indicating "which record is where in the file").

    --
    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.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The reason I am using binary rather than text is because, as I understand it, that is required for random access.

    Your strings must have a fixed size for random access to work. Do you really need random access? If you're not sure or you don't really need it, then don't use binary i/o, use text based i/o with the stream operators << and >>.

    If you absolutely need random access, then you must use fixed size strings. For that, I'd stop using the string class and instead use a character array of a fixed size. They are not as easy to use as the string class, but are necessary for fixed record length i/o that you pretty much have to have for random access in the file.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    19
    OK. Could I just convert between string and char array before/after writing/reading to the file? If I can, how can I convert a char array to a string?

    Thanks!

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just pass it to a string constructor.
    std::string s = mychararray;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Could I just convert between string and char array before/after writing/reading to the file? If I can, how can I convert a char array to a string?

    You could do this only if you forced the strings to be a certain size (or smaller). However, it may make more sense to store character arrays in your struct if you have one and do the conversion when storing the data rather than when writing it and reading it.

    To convert a character array to string (after reading) just assign it.

    To convert a string to a character array (before writing) use strcpy or strncpy:
    Code:
    const int MAX_NAME_LEN = 100;
    
    // ensure name is small enough to fit. There are other ways to do this, but this one works.
    name.resize(MAX_NAME_LEN-1);
    
    char namebuf[MAX_NAME_LEN];
    strcpy(namebuf, name.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM