If I Open a file using something like:

FileStream^ fs = File::OpenRead( path );

How can I determine the size of the file so that I can read
all of the data bytes in the file? What sort of Read would
you use to read one byte at a time. Would you open a buffer
(say 1024 bytes) and index into the array for each character?

array<Byte>^b = gcnew array<Byte>(1024);

Or would you use a read buffer size of 1 byte ch[1] as a
buffer.array<Byte>^b = gcnew array<Byte>(1);

fs->Read ( b, 0, 1 );

How would you handle the end of thie file so that
you know how many bytes are in the file?

I need to scan a file to turn off 1 or more bits in each of the bytes
in the file and write out these data to a new "copy" of the file with those bits turned off using a bitwise AND of each byte in the input file.

I will also need to seek using random access to certain spots in the
input file. I have seen an example:

fs->Seek (15, SeekOrigin::Current);

This moves the "read cursor" 15 chars further and

fs->Seek (15, SeekOrigin::Begin);

I see "Begin" so I assume that you could use "End"? I have
also shown an example that uses "Current". Are there any
more usages. Could you tell the size of the file by simple
subtracting (End - Begin). How would you write this code?

I understand that you can use SeekOrigin with BaseStream and Seek to set the file pointer of the underlying stream to the beginning. I need more examples of this. in another example I see:
Something like this:

fs.BaseStream.Seek(0, SeekOrigin.End)

What is with this example?

I am new to c++ and the FileStream way of accessing file data. I am
quite confused about this and need some help. Thank you in advance for any help or examples that might be useful.

All the best!


The general way to get the file size is to seek to the end of the file then get the current file position. I don't know how you would do that with CLR/C++ (managed code), but I'm certain if you read the available methods in FileStream you will find it.

How does one get the current file position as an integer?

The Seek() method returns a long long which is the length of the file

>How can I determine the size of the file so that I can read
all of the data bytes in the file?

fs->Length property.

array<Byte>^ bb=  gcnew array<Byte>(fs->Length);


array<Byte>^b= System::IO::File::ReadAllBytes(file);

>How does one get the current file position as an integer?

fs->Position property.

Lets say I have an integer vaiable L that is the size (or length) of the FileStream (fs) - I want do something like this:

for (int i = 0; i <= L; i++) 
            fs->Read(ch, i, 1);

usech(char ch)
 // do something with the character

I would appreciate an example of where a file is opened and processed character by character until the end of the file is reached.

Thanks again.

P.s. Please show a routine that returns L.

Thanks again for any help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.