once more... read txt file

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 04:00, xxxxxxxx wrote:

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

---------
hi there, i found several posts here about reading a txt file, but somehow i got stucked.
this is how my code looks atm:

> `

  
\>  String GetfileContent(Filename fn)  
\>  {  
\>       AutoAlloc<BaseFile> bfile;  
\>       bfile->Open(fn, GE_READ, FILE_IGNOREOPEN, GE_MOTOROLA, MACTYPE_CINEMA, MACCREATOR_CINEMA);  
\>    
\>       String singleChar;  
\>       String line = "";  
\>       VLONG fileLength = bfile->GetLength();  
\>       CHAR c;  
\>       CHAR* pc = &c;  
\>    
\>       for (VLONG i = 0L; i != fileLength; ++i)  
\>       {  
\>             bfile->ReadChar(pc);  
\>             singleChar = pc;  
\>             // Line read from the file  
\>             line += singleChar;  
\>       }  
\>    
\>       bfile->Close();  
\>       return line;  
\>  }      
\>  

`

i thought that when i now call:
GePrint(GetfileContent("filename.txt"));

it should print the content of filename.txt into the console window.. but ot doesnt.

what is wrong here?

thank you for helping
cheers,
ello

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 04:34, xxxxxxxx wrote:

ah. my fault , i forgot the path to the file 🙂

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 06:14, xxxxxxxx wrote:

now i only need to know why i get those thrange cryptic bunch of chars between every char from the txt file??
edit:
i use SubStr to always just get the first char. that works here. is the code below save for different plattforms?

> `

  
\>  String GetfileContent(Filename fn)  
\>  {  
\>       AutoAlloc<BaseFile> bfile;  
\>       LONG i = 0;  
\>       String singleChar;  
\>       String line = " ";  
\>       CHAR c;  
\>       CHAR* pc = &c;  
\>       if(bfile->Open(fn, GE_READ, FILE_IGNOREOPEN, GE_MOTOROLA, MACTYPE_CINEMA, MACCREATOR_CINEMA))  
\>       {  
\>            VLONG fileLength = bfile->GetLength();  
\>            while(i < fileLength)  
\>            {  
\>                 bfile->ReadChar(pc);  
\>              singleChar = pc;  
\>                 line = (line + singleChar.SubStr(0, 1));  
\>                 i++;  
\>            }  
\>            bfile->Close();  
\>       }  
\>       else  
\>       {  
\>            line = "Error";  
\>       }  
\>       return line;  
\>  }  
\>  

`

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 06:45, xxxxxxxx wrote:

You can't do this type of assignment between the C4D SDK String class and CHAR*: singleChar = pc;

A C/C++ 'string' (char array) needs to be Null terminated. Then you need to instantiate the String class with a constructor.

What would work better here is to declare c as CHAR c[2], set c[1] = 0 (null terminate). Then do this:

bfile->ReadChar(pc);
line += String(pc);

SubStr() is a long way to go to concatenate a single character into a string.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 07:47, xxxxxxxx wrote:

I should also note that in your situation, a for loop would be much more optimal than a while loop:

for (i = 0L; i != fileLength; ++i)

The for loop is more encapsulated, creating tighter, faster machine code than a while loop for simple incrementation.

ETA: c[1] = 0; only has to be done once - outside of the loop.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 09:00, xxxxxxxx wrote:

thanks for your advice, when i do so, i get those cryptic characters in the console. thats why i used SubStr(0,1)..

how do i get rid of them and get just the content of the txt file?

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 10:38, xxxxxxxx wrote:

Two possibilities offhand:

1. Might be control characters (tabs, carriage returns, etc.)? It may also be byte-order but that would be suspect for a non-binary text file.

2. The file might not be plain ASCII? If the file is in UTF-16 or some other 'text' format using Unicode that would explain the characters. You would need to check for control sequences or read two chars at a time.

Otherwise, a A+0 is "A" and a valid string (C/C++/C4DSDK). Have you tried simply printing GePrint(*pc) to see what values you are getting? (You could do some ASCII fix up here to get the character printed rather than the ASCII value.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 11:29, xxxxxxxx wrote:

Hm, the file is saved from windows notepad with ansi coding.. and the characters are just typed straight away, no tabs or anything ... just like "test123"

here is the console output:
console1.gif

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 13:11, xxxxxxxx wrote:

I would simply use the standard FILE struct and the functions fopen and fread from stdio.h. That also works on both, PC and MAC, without problems.

Greetings,
Jack

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 13:59, xxxxxxxx wrote:

That's what I use. 🙂 Though these standard C file accessors are not Unicode/wide-character aware - if needed.

Let me run a simple test plugin to see what I get. Will make determination easier.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 14:19, xxxxxxxx wrote:

Try this instead:

> String GetFileContent(const Filename& fn) \> { \>      AutoAlloc<BaseFile> bfile; \>      if (!bfile) \>      { \>           MessageDialog("Test: BaseFile"); \>           return ""; \>      } \>      if (!bfile->Open(fn, GE_READ, FILE_IGNOREOPEN, GE_INTEL)) \>      { \>           MessageDialog("Test: bfile->Open"); \>           return ""; \>      } \>       \>      String line = ""; \>      LONG fileLength = bfile->GetLength(); \>      CHAR c[2]; \>      c[1] = 0; \>      CHAR\* pc = &c;[0]; \>      for (LONG i = 0L; i != fileLength; ++i) \>      { \>           bfile->ReadChar(pc); \>           // Line read from the file \>           line += String(pc); \>      } \>      GePrint(line); \>      bfile->Close(); \>      return line; \> }

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 14:23, xxxxxxxx wrote:

An endian issue I think. If you run on Windows and use GE_MOTOROLA, the bits will be considered in the opposite order.

You can change those LONGs back to VLONGs. I did a quick build in VS6.0 for R9.1 (from a simple project).

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/05/2009 at 14:32, xxxxxxxx wrote:

thank you both.
robert, that indeed works!