BaseFile->ReadChar



  • On 18/05/2013 at 00:33, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R14 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    Hi Folks,

    I'm having a bit of a strange issue printing out the first few characters of a text file I'm trying to read. Instead of printing out each character (as what I thought the for loop below would do..) it's printing out the character along with the file's path! Code below:

      
    AutoAlloc<BaseFile> BFile;   
    Filename FName;   
    FName = Filename("D:\TextFile.txt");   
    if(!BFile->Open(FName, FILEOPENREAD))   
    {   
        GePrint("File not found!");   
    }   
    CHAR *Char;   
    String str;   
    for(int I = 1; I <= 2;) // example trying to get first two characters   
    {   
        BFile->ReadChar(Char);   
        str += CharToString(Char); // little function to turn char to a string..   
        GePrint(str); // this results in the print example below...   
        I++;   
    }   
      
    Prints: "R:\TextFile.txt" where the drive letter is replaced by the Char letter (in this case an "R").   
    

    I haven't got anything else in the code that could be affecting this I wouldn't have thought. There's a LONG to get the length/number of characters in the file and print the result. But other than that, that's about it! The character prints are correct, that is the ReadChar is getting the right character in the file, but what's with the file extension etc being printed as well!? Is this a bug or something I'm doing incorrectly?

    WP.



  • On 18/05/2013 at 01:44, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    <ADDRESS>
    User Information:
    Cinema 4D Version:   R14 
    Platform:   Windows  ;   
    Language(s) :    
    C++  ;

    ---------
    </ADDRESS> Hi Folks,

    I'm having a bit of a strange issue printing out the first few characters of a text file I'm trying to read. Instead of printing out each character (as what I thought the for loop below would do..) it's printing out the character along with the file's path! Code below:

     
    AutoAlloc<BaseFile> BFile;   
    Filename FName;   
    FName = Filename("D:\TextFile.txt");   
    if(!BFile->Open(FName, FILEOPENREAD))   
    {   
        GePrint("File not found!");   
    }   
    CHAR *Char;   
    String str;   
    for(int I = 1; I <= 2;) // example trying to get first two characters   
    {   
        BFile->ReadChar(Char);   
        str += CharToString(Char); // little function to turn char to a string..   
        GePrint(str); // this results in the print example below...   
        I++;   
    }   
     
    Prints: "R:\TextFile.txt" where the drive letter is replaced by the Char letter (in this case an "R").   
    

    I haven't got anything else in the code that could be affecting this I wouldn't have thought. There's a LONG to get the length/number of characters in the file and print the result. But other than that, that's about it! The character prints are correct, that is the ReadChar is getting the right character in the file, but what's with the file extension etc being printed as well!? Is this a bug or something I'm doing incorrectly?

    WP.

    "Char" is an pointer whose value isn't set and ReadChar() is storing data in this undefined (arbitrary) location of the pointer. Be surprised that only a letter in your string was replaced and not some arbitrary memory location...

    Best regards,

    Wilfried



  • On 18/05/2013 at 02:24, xxxxxxxx wrote:

    CHAR char;
    String str;  
    for (int i=1; i <= 2; i++) {
        if (!BFile->ReadChar(&char)) break;
        str += CharToString(char);
        GePrint(str);    
    }
    

    Try it like this.



  • On 18/05/2013 at 03:28, xxxxxxxx wrote:

    Thanks both of you. I ended up with the following which seems to work:

      
    CHAR Char[2];   
    Char[1] = 0;   
    CHAR *Cpoint = &Char;[0];   
    for(int I = 1; I <= 2;)   
    {   
        if(!BFile->ReadChar(Cpoint)){break;}   
        str += CharToString(Cpoint);   
        GePrint(str);   
        I++;   
    }   
    

    Is someone able to explain to me (maybe in laymens terms...!) why making a Char array of size two (and making the second one NULL) works, but having a single Char gives other unknown text in the print? Is this to do with the memory block of the Char having some memory not "NULLed" out?

    WP.



  • On 18/05/2013 at 04:59, xxxxxxxx wrote:

    A string in C and C++ is a set of characters with a null-terminator (/0 or 0 valued byte).  Thus, even to take a single character and make a string out of it, you need two characters: the character itself and a null-terminator.  The null-terminator concept is a way to avoid storing or pre-knowing the size of the string.  C4D String actually stores the number of characters as the first LONG value and then the characters.  But the C/C++ standard uses the null-terminator as a way to know the extent of the character sequence even if the memory block is larger in which it is stored.  That is, you can have a 1024 block of memory hold variable-sized strings at any time and always know the termination (and then the size) of the string by seeking the null-terminator.  A bit antiquated but still in practice.



  • On 18/05/2013 at 05:17, xxxxxxxx wrote:

    Thanks Robert, makes sense.

    WP.


Log in to reply