How to know if Key in Vector does not exist / If File has write permissions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How to know if Key in Vector does not exist / If File has write permissions

  1. #1
    Join Date
    Mar 2010
    Posts
    42

    How to know if Key in Vector does not exist / If File has write permissions

    Hi all,

    I have 2 questions to ask, and I decided to ask them in a single thread (to save space for the other forum goers).

    1. How to find if a key in a vector doesnt exist?

    Ex. There is a hash_map Dictionary defined Dictionary[<string>, <string>] (meaning the key is represented as a “string”, and the value is also a string

    If I have something like this:

    hash_map <string, string> Dictionary;

    string doesNotExist = “does not exist”
    string DictValue = Dictionary[doesNotExist];

    It should identify that the Key for that Dictionary does not exist.

    It does not error however, but it returns a blank string "". I want to identify if the key does not exist since it may be possible that the key exists but also contains a blank string "".

    2. How to find if a file is writable to or not?

    The problem I have is the file can be opened if it has read permissions but it cannot be written to since it has no write permissions. I am imagining something like this:

    If(myFile.isopen())
    {
    If(myFile.isWriteable)
    {
    //write to the file
    }
    Else
    {
    Printf(“Error: File is not writable”);
    }
    }

    I tried searching but all I find is if the file exists, etc.

    Thank you all for the future help!
    Last edited by LeanA; April 27th, 2010 at 08:05 AM. Reason: Clarification in question 1

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: How to know if Key in Vector does not exist / If File has write permissions

    Could you explain your first question in more detail ? For a dictionary, you
    normally would use a map, not a vector. For a map, you would use the find()
    member function.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: How to know if Key in Vector does not exist / If File has write permissions

    Number 1 doesn't make sense to me. Vector's don't have keys and string DictValue = Dictionary[doesNotExist]; doesn't make sense syntactically.

    2 you open the file for writing and check for failure. You could also call _access.

  4. #4
    Join Date
    Mar 2010
    Posts
    42

    Re: How to know if Key in Vector does not exist / If File has write permissions

    Hi Philip, and GCDEF, thanks for replying:

    Oops, sorry, I got confused and made an error: it should be hash map and not vector. I declare it like this:

    hash_map <string, string> Dictionary;

    Hopefully this will clarify the first question. I will also edit my original post.

    Thank you!

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: How to know if Key in Vector does not exist / If File has write permissions

    I have never used hash_map (not standard yet), but it must have a find function:

    Code:
    if (Dictionary.find("some_key") == Dictionary.end())
    {
        // key does not exist
    }

  6. #6
    Join Date
    Mar 2010
    Posts
    42

    Re: How to know if Key in Vector does not exist / If File has write permissions

    Hi Philip,

    Thanks for the tip! Going to try it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center