filestream

How to split a large file into chunks in c#?

北战南征 提交于 2019-11-27 07:29:45
I'm making a simple file transfer sender and receiver app through the wire. What I have so far is that the sender converts the file into a byte array and sends chunks of that array to the receiver. This works with file of up to 256mb , but anything above, the line: byte[] buffer = StreamFile(fileName); //This is where I convert the file Throws a System out of memory exception. I'm looking for a way to read the file in chunks then write that chunk instead of loading the whole file into a byte . How can I do this with a FileStream ? EDIT: Sorry, heres my crappy code so far: private void btnSend

C# - How do I read and write a binary file?

只谈情不闲聊 提交于 2019-11-27 06:45:24
问题 How do I read a raw byte array from any file, and write that byte array back into a new file? 回答1: (edit: note that the question changed; it didn't mention byte[] initially; see revision 1) Well, File.Copy leaps to mind; but otherwise this sounds like a Stream scenario: using (Stream source = File.OpenRead(inPath)) using (Stream dest = File.Create(outPath)) { byte[] buffer = new byte[2048]; // pick size int bytesRead; while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { dest.Write

Load and read a csv file with php

半城伤御伤魂 提交于 2019-11-27 06:28:52
问题 I want to download some content in csv format. It is stored online in a csv file. I can not just read the content. I first have to download the file, open and then read it. Is there a way to open it directly? Or do I have to first load/upload it on my server and then open it like a classic file? 回答1: Yes, it can open directly using fopen and fgetcsv However, this feature sometime is restricted for security concern, and you can read the details via the documentation http://php.net/manual/en

C# FileStream : Optimal buffer size for writing large files?

笑着哭i 提交于 2019-11-27 06:13:44
Suppose I'm writing a couple of files to disk, between 2MB and 5GB. What are sensible buffer values for the FileStream ? Is it sensible to work with buffersizes of several megabytes, or should I stick to kilobyte-buffers ? Rubens Farias Default buffer size is 4 KiB. Also, take a look here: Sequential File Programming Patterns and Performance with .NET A quick little benchmark based on the document referenced shows no increase in performance on my system greater than 128KB buffer size. Your mileage may vary, feel free to use the below. Stopwatch sw = new Stopwatch(); Random rand = new Random();

C equivalent to fstream's peek

我的未来我决定 提交于 2019-11-27 04:58:52
I know in C++, you're able to peek at the next character by using: in.peek(); . How would I go about this when trying to "peek" at the next character of a file in C? fgetc + ungetc . Maybe something like this: int fpeek(FILE *stream) { int c; c = fgetc(stream); ungetc(c, stream); return c; } You could use a getc followed by an ungetc Charles Ma you'll need to implement it yourself. use fread to read the next character and fseek to go back to where you were before the read EDIT : int fsneaky(FILE *stream, int8_t *pBuff, int sz) { sz = fread(pBuff, 1, sz, stream) fseek(pFile, -sz, SEEK_CUR);

Why does BinaryWriter prepend gibberish to the start of a stream? How do you avoid it?

跟風遠走 提交于 2019-11-27 04:50:37
I'm debugging some issues with writing pieces of an object to a file and I've gotten down to the base case of just opening the file and writing "TEST" in it. I'm doing this by something like: static FileStream fs; static BinaryWriter w; fs = new FileStream(filename, FileMode.Create); w = new BinaryWriter(fs); w.Write("test"); w.Close(); fs.Close(); Unfortunately, this ends up prepending a box to the front of the file and it looks like so: TEST, with a fun box on the front. Why is this, and how can I avoid it? Edit: It does not seem to be displaying the box here, but it's the unicode character

FileUpload to FileStream

不问归期 提交于 2019-11-27 04:25:29
I am in process of sending the file along with HttpWebRequest. My file will be from FileUpload UI. Here I need to convert the File Upload to filestream to send the stream along with HttpWebRequest. How do I convert the FileUpload to a filestream? Since FileUpload.PostedFile.InputStream gives me Stream, I used the following code to convert it to byte array public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[input.Length]; //byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) {

Encode a FileStream to base64 with c#

一曲冷凌霜 提交于 2019-11-27 03:03:46
问题 I know how to encode / decode a simple string to / from base64 . But how would I do that if the data is already been written to a FileStream object. Let's say I have only access to the FileStream object not to the previously stored original data in it. How would I encode a FileStream to base64 before I flush the FileStream to a file. Ofc I could just open my file and encode / decode it after I have written the FileStream to the file, but I would like to do this all in one single step without

How to convert stream results to string

Deadly 提交于 2019-11-27 01:49:38
问题 I want to convert the stream result output to string since I want to use it in Junit I think that I need to use the string writer but Im not sure how exactly to use it. StreamResult result = new StreamResult(new File("C:\\file.xml")); transformer.transform(source, result); Thanks Fedor 回答1: Have a look at and learn to use the javadocs of the StreamResult class (http://java.sun.com/javase/6/docs/api/). One of the constructors of StreamResult takes a Writer object as a parameter. You will see

Read from a growing file in C#?

独自空忆成欢 提交于 2019-11-27 01:22:00
问题 In C#/.NET (on Windows) is there a way to read a "growing" file using a file stream? The length of the file will be very small when the filestream is opened, but the file will be being written to by another thread. If/when the filestream "catches up" to the other thread (i.e. when Read() returns 0 bytes read), I want to pause to allow the file to buffer a bit, then continue reading. I don't really want to use a FilesystemWatcher and keep creating new file streams (as was suggested for log