Problem in Unicode reading from file

Login to reply to this topic.
Wed, 2008-03-05 08:13
Joined: 2007-11-20
Forum posts: 7

Hello friends....

I want the unicode data in the file to be read in my HBuf16* . I m able to get that data in
TBuf<16>. Now i need this data in HBuf16* so required by my display function.

Please help me is any way to convert TBuf<16> to HBufC16* of any direct way to read
unicode data in HBufC16*.

It's helpful if someone of u can share the sample code to do the same.

-Thanks

-Amit Nema


Wed, 2008-03-05 23:56
Joined: 2007-09-23
Forum posts: 159
Re: Problem in Unicode reading from file

Read it into a HBufC8 and then convert that into a wide descriptor.

You will find loads of previous postings about how to convert from narrow to wide descriptors.

Thu, 2008-03-06 06:33
Joined: 2007-11-20
Forum posts: 7
Re: Problem in Unicode reading from file

hi...

Thanks for the response ...

As I want to read the unicode data & when i am reading it in HBufC8 then the value in it get garbage.

As we need to read unicode data so it is required to store in 16 bit buffer only

That is why we are storing it in TBuf16<128> & then converting it to HBufC16*.

But I am not getting sucess in doing the same.

- Thanks

- Amit Nema

Thu, 2008-03-06 08:23
Joined: 2005-11-20
Forum posts: 1242
Re: Problem in Unicode reading from file

Did you take up my hint and learn about Unicode encodings? Because, if you are not sure which encoding you want to use for your file, or are not sure which encoding your already-existing file uses, you will indeed get nothing than garbage.

By the way, TBuf<16> is a TBuf that offers room for up to 16 Unicode characters. That's probably not what you mean to say in your post. You probably mean TBuf16 (the 16 bit variant) or simply TBuf as opposed to TBuf8 (the 8 bit variant, used not very often, mostly for binary data), right?

So, which encoding does your file use?


René Brunner

Thu, 2008-03-06 10:15
Joined: 2007-11-20
Forum posts: 7
Re: Problem in Unicode reading from file

hi ...

My file contains the Unicode text i.e in hindi.

That content i need to read in HBufC16* p_hintext variable.

Now can u guide me the correct way.

Thanks

amit nema

Thu, 2008-03-06 10:35
Joined: 2005-11-20
Forum posts: 1242
Re: Problem in Unicode reading from file

"Hindi" is not the encoding, that's the script that you are using, which is completely unimportant for the task at hand. It could be Chinese, it wouldn't change a thing.

I think it's back to basics. I would recommend reading the Wikipedia article about Unicode:
http://en.wikipedia.org/wiki/Unicode
and there especially the chapter about, you guessed it, encodings, with special attention to UTF-8 and UTF-16, the two most probable candidates for your file.

As long as you don't know what kind of file you have, you won't be able to read it correctly. Which program on which platform produced your Hindi file, anyway?


René Brunner

Fri, 2008-03-07 01:27
Joined: 2007-09-23
Forum posts: 159
Re: Problem in Unicode reading from file

So how did you read the data into the TBuf16<>?
If you need to convert that into a HBufC16* then there is something wrong with the code, a function to i.e. take a string and display it then it should not be taking a HBufC16* as a paramter it should be taking a const TDesC&.

Reading from the file into a TBuf8, or any other 8 descirptor will not turn it into garbage, you file contains a series of bytes, if you read into a TBuf8<> then that will contain exactly the series of bytes as was in the file. Reading into any descriptor does not change the data in any way.

Fri, 2008-03-07 06:10
Joined: 2007-11-20
Forum posts: 7
Re: Problem in Unicode reading from file

Hi...

Below is the way i m reading the data in TBuf16 :-
RFs fs;
fs.Connect();
RFile file;
_LIT(KFileName,"c:\\s.txt");
file.Open(fs, KFileName, EFileRead|EFileShareAny);
TFileText fileText;
fileText.Set(file);
fileText.Seek(ESeekStart);

TBuf16<16> readText;
while(fileText.Read(readText) != KErrEof)
{
// here is code to manipulate
}
file.Close();
fs.Close();
Using the hard code value for the data in HBufC16* p_hintext i m storing as follows :-
//Text for display "Webdunia me aapkaa swagat hai"

p_hintext->Des().Append(_L("\x0935\x0947\x092c\x0926\x0941\x0928\x093f\x092f\x093e \x092e\x0947\x0902 \x0906\x092a\x0915\x093e \x0938\x094d\x0935\x093e\x0917\x0924 \x0939\x0948\x0964"));

Now I want to store the same text in a file & want to read from there. So now please help me out how to store & in which format did i store the data. Also the way so that i get read that data in my p_hintext buffer.

-Thanks

-Amit Nema

Fri, 2008-03-07 07:53
Joined: 2005-11-20
Forum posts: 1242
Re: Problem in Unicode reading from file

Look, really not much is won if somebody like me gives you now the code for solving *exactly* your problem at hand of writing 16 Unicode characters into a file and reading them again from there, because this a very special case that you will very soon surpass, and then you will be right back here with the next question for a case that is a little more difficult, and so on, and maybe people will not be very happy with your productivity.

There is simply no way around learning, and learning yourself. (Did you learn about the encodings? I guess not.)

But of course that's not my problem, and not my business either. I will quit this thread with a little last tip: For simply writing a string into a file and reading it again I would probably use the TFileText class. If you check that, you will see that it is very simple.


René Brunner

Fri, 2008-03-07 08:10
Joined: 2007-11-20
Forum posts: 7
Re: Problem in Unicode reading from file

I think u have not read my last comment in this thread. In the last thread i have clearly mention that i have already used the TFileText class for reading the file. Also I m able to read the data in HBufC16 by using a TPtr16 point to HBufC16. It is feacting the data. But it is not the data i m exactly looking for. If U r not having no solution then no issue. But don't tell that i m doing nothing.

-thanks for the responses

- amit

Fri, 2008-03-07 08:44
Joined: 2005-11-20
Forum posts: 1242
Re: Problem in Unicode reading from file

Ok, sorry, you are right, I should have checked your post more closely - you do use TFileText already. My fault.

But then I understand even less where the real problem is.

Do you already now try to use TPtr16 and HBufC16 because of some future expansion of your program? For the case at hand at least there is no need to use any HBuf's and any pointers. You could put your string into a normal TBuf big enough, write it with TFileText::Write, and then later read it again, as you do already. And there is hardly a way how anything could go wrong, I think.

By the way, usually people do not write "16" as in "TPtr16" or "TBuf16", but simply "TPtr" and "TBuf" and so on, because most of the time it is 16-bits only anyway.


René Brunner

Fri, 2008-03-07 10:28
Joined: 2007-11-16
Forum posts: 32
Re: Problem in Unicode reading from file


TBuf<100> ipword;     //:: hardcoded
              ipword.Copy(datafromfile);
              HBufC* temp = HBufC::NewLC (ipword.Length());
              CleanupStack::PushL(temp);
               *temp = ipword;
               DisplayFunction(*temp);
               CleanupStack::PopAndDestroy (temp);

Amit u can try above code , i hope it helps.

Pramod


"Nobody will believe in you unless you believe in yourself."

  • Login to reply to this topic.