NonNullTerminated Strings?

This is a discussion on NonNullTerminated Strings? within the C++ Programming forums, part of the General Programming Boards category; Is there anyway to get a NonNullTerminated string?...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    16

    NonNullTerminated Strings?

    Is there anyway to get a NonNullTerminated string?

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Strings are always non-null terminated. Unless you are referring to arrays of chars.

    search for std::string
    [code]

    your code here....

    [/code]

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    16
    I'm refering to an array of chars because those strings aren't that good with files..

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> Is there anyway to get a NonNullTerminated string?

    That is about as descriptive as saying: "Can I get an int that equals 12?".

    Geez.



    ITSA
    Socket Library!

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by sPlOrYgOn
    I'm refering to an array of chars because those strings aren't that good with files..
    Well, I won't try to convince you of something different, but I don't use those relicts from the past at all


    Anyway, I'm still not sure if I understand your problem, but you could try to truncate the last char, or create them without a \0 at the end.
    The only problem is, that unless you hand it to a function that takes a lenght as parameter, the function will read all data in the string and then begin to read data behind the string (because of the missing \0). It will continue to do so until - by chance - it hits a \0 in memory.

    edit: btw, this is a very bad thing to do
    [code]

    your code here....

    [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm refering to an array of chars because those strings aren't that good with files..
    For an array of char to be considered a string you must terminate it with nul, or have some other way of determining the length. How is the std::string class not good with files? Are you using FILE*'s or something?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    16
    im doing binary files

    [edit]
    and everytime i save the structure with the string in it the string will be all messed up thats y i use an array of chars
    [/edit]

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I've got this one licked!

    Use an array of ints instead and fill each element with the ascii code instead of the letter.

    To print it cast as char.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Give an example of how you are reading/writing to the file. Chances are you are making a *very* common mistake.



    ITSA
    Socket Library!

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by sPlOrYgOn
    im doing binary files

    [edit]
    and everytime i save the structure with the string in it the string will be all messed up thats y i use an array of chars
    [/edit]
    You can't save a string like that. It's an object. You need to save the strings data.

    string.data()

    Give this link a chance
    [code]

    your code here....

    [/code]

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    16
    i'm saving the files like this
    Code:
    fout.write((const char*)MyStructure, sizeof(Structure));
    and the array of ints sounds good but i dunno the number for the letters

    [edit]
    i save strings just fine in binary files
    [/edit]

  12. #12
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    my solution was kind of a joke I don't suggest you use it, but nonetheless, it is a 2minute job to display the ascii charset:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
         int x;
    
         for(x=0 ; x<255 ; x++)
              cout << "No. " << x << " \t" << static_cast<char>(x) << endl;
    
         return 0;
    }
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    16

  14. #14
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    only problem is that's not ascii.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >fout.write((const char*)MyStructure, sizeof(Structure));

    Is MyStructure a pointer? If yes, then the above should work. Otherwise try:
    fout.write((const char*)&MyStructure, sizeof(Structure));

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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