C
C#7mo ago
Haxo

How to Read byte by byte until EOF?

Hello! I couldn't find an answer on the internet on this question so I guess I will have to ask it. I am trying to make my own version of WinRar and to avoid using too much RAM I want to read and write the file byte by byte. How do I do it? Here's the code:
using System;
using System.IO;

namespace Paxer;

class Core
{
public static void writeFile(FileStream _target, string root, string sub_path)
{
// What is origin?
string full_path = Path.Combine(root, sub_path);

FileInfo file_info = new FileInfo(full_path);
FileStream _from = new FileStream(full_path, FileMode.Open);

// Lenghts
long path_len = sub_path.Length;
byte[] path_blen = BitConverter.GetBytes(path_len);

long len = file_info.Length;
byte[] blen = BitConverter.GetBytes(len);

// Write
byte b;

// What to do here ???
}
}
using System;
using System.IO;

namespace Paxer;

class Core
{
public static void writeFile(FileStream _target, string root, string sub_path)
{
// What is origin?
string full_path = Path.Combine(root, sub_path);

FileInfo file_info = new FileInfo(full_path);
FileStream _from = new FileStream(full_path, FileMode.Open);

// Lenghts
long path_len = sub_path.Length;
byte[] path_blen = BitConverter.GetBytes(path_len);

long len = file_info.Length;
byte[] blen = BitConverter.GetBytes(len);

// Write
byte b;

// What to do here ???
}
}
11 Replies
Haxo
HaxoOP7mo ago
I want to point out I don't seek functions like "ReadAllBytes" or "WriteAllBytes"
Lisa
Lisa7mo ago
FileStream.Read Method (System.IO)
Reads a block of bytes from the stream and writes the data in a given buffer.
Haxo
HaxoOP7mo ago
ok thanks I will read it nvm ignore the ping im stupid
Lisa
Lisa7mo ago
FileMode is about how the operating system should open the file. Open it, create a new one and open it, add to an existing file. FileAccess is about the operations you want to do on that file
Haxo
HaxoOP7mo ago
I just haven't noticed FileMode Enum does not contain "Write" item or something like that so I understood how it works thanks anyway I used too much python....
Lisa
Lisa7mo ago
:nodd:
Haxo
HaxoOP7mo ago
If I want to write to file it will clear its contents and move pointer to the beggining right? When I decide to write something the pointer will be always at the end? or it depends
Lisa
Lisa7mo ago
It depends on the filemode
Haxo
HaxoOP7mo ago
understood
Jimmacle
Jimmacle7mo ago
also, reading single bytes at a time isn't very efficient you want a reasonably sized buffer
Want results from more Discord servers?
Add your server