Reading a std::string from a std::map and write it to a file.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Reading a std::string from a std::map and write it to a file.

  1. #1
    Join Date
    Apr 2008
    Posts
    156

    Reading a std::string from a std::map and write it to a file.

    Hello,

    I had a std::map which hold some header info in std::string type.
    I would like to store this info in to a .txt file and read it back.

    I would like share my logic here.
    Code:
    std::map<unsigned long,std::string> head_string;
    for(i=0;i<head_count;i++)
    {
                std::string heads = head_string[i];
    				char headsdr[44]; 
    				std::copy (heads .begin(),heads .end(),&headsdr[0]); 
    				for (int j = 0; j < 44; j++)				
    					fprintf (fptr,"%c",headsdr[j]); 
    				fprintf(fptr,"\n");
    }
    Will this approach is correct ?
    Will %c handle extended ASCII characters as this has a max of 0-255?
    how can i write the std::string in to a text file.?

    Thanks
    Dave.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Reading a std::string from a std::map and write it to a file.

    There are potential problems with your code:
    Code:
    for(i=0;i<head_count;i++)
    {
       std::string heads = head_string[i];
    You should not write loops that assume what the map keys are. Use the actual keys that are existing in the map to perform your loop. What if there is no "i" entry in the map? That line "head_string[i]" creates an empty entry using "i" as a key.

    The proper way to loop is this:
    Code:
    std::map<unsigned long, std::string>::iterator mapIt = head_string.begin();
    while (mapIt != head_string.end() )
    {
        //...
        // mapIt->first is the unsigned long key
        // mapIt->second is the string 
        ++mapIt;  // go to next entry in the map
    }
    The loop structured this way ensures that you are only using keys in the map, and not inadvertently creating new keys while you're looping.

    Second:
    Code:
    char headsdr[44]; 
    std::copy (heads .begin(),heads .end(),&headsdr[0]);
    What if heads has more than 44 characters? You now have a memory overwrite error. What you should be doing is ensuring that the second argument to std::copy is the minimum of 44 or heads.size():
    Code:
    size_t numChars = std::min( 44, heads.size() );
    std::copy (heads.begin(), heads.begin() + numChars, &headsdr[0]);
    Something similar to the code above.
    Will %c handle extended ASCII characters as this has a max of 0-255?
    Any characters above 127 will print depending on the current locale setting. So does it work? I don't know -- try it. If it doesn't work, then that is another thread of discussion.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Reading a std::string from a std::map and write it to a file.

    In addition, the std::map may not be the ideal container here at all, if the map keys are sequential zero-based integers. And that's just what it looks like to me here. Are the keys sequential line numbers assigned while reading in the strings from the file? In such a case an std::vector or even a plain array may be the better choice of container.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center