prodizy wrote:
Which of the following is the best way to read and write files in an
XPCOM component(using C++)?

FileOutputStream or nsILocalFile::OpenNSPRFileDesc are the best way (openANSIFileDesc has, I believe, issues if the MS CRT is linked statically, but is otherwise not worse than OpenNSPRFileDesc)

1. NSPR? - In PR_open(), instead of a char*,  can I specify
nsILocalFile as the file to be opened ?

No, NSPR is on a lower level than XPCOM, so you can't pass XPCOM interfaces to it. You can, of course, pass the filename to it (but then you can't use filenames outside of the native character set)

2. nsIFileInputStream & nsIFileOutputStream? - I want to avoid this as
these interfaces are not frozen.

You could get an input stream via the IO service and nsIChannel::Open, but writing to files that way would be harder. (But those interfaces would be the best way)

Or Is there any other easier way? Am I missing something?

You didn't list nsILocalFile::OpenNSPRFileDesc explicitly above, but that would be the way to go IMO, especially if you want to use only frozen APIs.

And most importantly, does mode paramenter we need to pass when opening
a file with nsILocalFile::OpenAnsiFileDesc() is supported by Windows?
What should be the argument in case of Windows? I looked at prio.h, it
just says that windows doesn't supports mode flags.

Of the ANSI function? That parameter is a string, not a combination of flags, and it is supported. If you want to write to the file, pass "w" or "wb" here. It's just like the mode parameter of fopen. This function maps pretty directly to _wfopen (of fopen, on older versions of Gecko or on Win9x).

I tried writing to a file using OpenAnsiFileDesc() & fwrite(). But the
file I open is becoming read only. And nothing is written to the file.

This ought to work, if you pass the right mode string.

Are there any C++ examples available for doing file I/O in  XPCOM?

These have examples, although they are for reading a file only:
http://developer.mozilla.org/en/docs/nsILocalFile:openNSPRFileDesc
http://developer.mozilla.org/en/docs/nsILocalFile:openANSIFileDesc



--
All the world's a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts, [...]     --W. Shakespeare
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to