c#


ToArray() function limitation


I am using the .ToArray() method to convert my string to char array whose size i have kept char[] buffer = new char[1000000]; but when I am using the following code:
using (StreamReader streamReader = new StreamReader(path1))
{
buffer = streamReader.ReadToEnd().ToCharArray();
}
// buffer = result.ToArray();
threadfunc(data_path1);
The size of the buffer getting fixed up to 8190, even it is not reading the whole file after using .ToCharArray() or .ToArray().
What is the reason for this does .ToCharArray() or .ToArray() have size limitations? As if I do not use this function I'm able to read whole file in string format, but when trying to convert it into char array by using this function I am getting size limitations.
ToCharArray() returns new instance of of array. So your buffer will refer to the new instance which is the size of data returned by ReadToEnd.
If you want keep buffer same size just add new array to the existed one
char[] buffer = new char[1000000];
using (StreamReader streamReader = new StreamReader(path1))
{
var tempArray = streamReader.ReadToEnd().ToCharArray();
tempArray.CopyTo(buffer, 0);
}
If you want just use the result array - you don't need to "predict" the size of array - just use returned one
public char[] GetArrayFromFile(string pathToFile)
{
using (StreamReader streamReader = new StreamReader(path1))
{
var data = streamReader.ReadToEnd();
}
return data.ToCharArray();
}
var arrayFromFile = GetArrayFromFile(#"..\path.file");
My guess is the problem is that read to end should finish before you call the ToCharArray(). This might help you. You don't need to define buffer since ToCharArray() creates a new instance of char[] itself.
string content;
using (StreamReader streamReader = new StreamReader(path1))
{
content = streamReader.ReadToEnd();
}
var buffer = content.ToCharArray();
You are probably using incorrect encoding. By default StreamReader(String) uses UTF8 encoding:
The complete file path is specified by the path parameter. This
constructor initializes the encoding to UTF8Encoding and the buffer
size to 1024 bytes.
Don't pre-allocate the buffer size, unless you have a specific need.
If your file is in ASCII format, you need to update your StreamReader constructor:
char[] buffer = null;
using (StreamReader streamReader = new StreamReader(path1, Encoding.ASCII))
{
buffer = streamReader.ReadToEnd().ToCharArray();
}
// buffer = result.ToArray();
threadfunc(data_path1);
Does your file contain binary data? If it contains EOF character and the stream is opened in text mode (which StreamReader does), that character will signal end of file, even if it is not actually the end of the file.
I can reproduce this by reading random .exe files in text mode.

Related Links

How to subscribe to events properly in Windows Runtime apps?
Controlling a rich text box control that is on form 1 from form 2
LINQ Query to get data
Viewing 2D array in WPF Form C#
C# TCP Server and reading strings sent to the server to activate a function
Unity3D C#-Pass List value to another class
C# - Is there any standard way of converting a string to json
Showing package installation progress with WiX custom bootstrapper
DateTime and Stopwatch value comparison
How to update a datasource dynamically C#?
C# Sql Server setting datasource = localhost
Convert Table to IEnumerable<SelectListItem>
TapePage Image property not being painted
Enable-Migrations command changed something
What in the Repeater
Can someone show me right way to send SMTP email in C# code for ASP.NET page

Categories

HOME
drupal
gcc
indexing
algolia
kendo-ui-angular2
macros
system-calls
cors
fortran
pelican
casting
blob
opencv4android
postsharp
okhttp3
hiveql
msbi
terminal
jshell
richtextbox
wavefront
revit-api
rtc
segmentation-fault
google-my-business-api
velocity
camunda
cakephp-2.0
eclipse-orion
hql
facebook-marketing-api
versioning
geography
orange
xforms
gnu-screen
procedural-generation
uipath
aws-certificate-manager
traveling-salesman
cloudformation
azure-table-storage
restangular
openoffice-impress
ibm-bpm
language-detection
wercker
storefront
availability
offset
certbot
area
jquery-inputmask
ssdp
aspnetboilerplate
ocamlbuild
anonymous-function
infovis
text-formatting
stream-socket-client
stanford-nlp-server
pyopenssl
skype-bots
beanshell
fluent-nhibernate-mapping
seyren
was
cardview
timefield
racket-student-languages
mac-app-store
scikit-image
particle-system
audiorecord
web-optimization
std
fadein
log4cxx
openhab
euro
stereotype
targetprocess
esb-toolkit-2.1
gcc-warning
perfect-scrollbar
mri
cl.exe
chunks
esri-arc-engine
crtdbg.h
unityvs
register-allocation
lwuit
zoneminder
cocos2d-x-2.x
oxygenxml
smartxls
satisfiability
lettuce
windows-phone-store
data-layers
static-class
kohana-orm
typography
dynamic-expresso
http-compression
pymel
database-restore
wpml
openafs
event-propagation
flatten
erlide
pinchzoom
capitalization
automapper-2
zipstream
excellibrary
onunload
lgpl
mediastreamsource
numerical-computing
calendarextender
atmega16
office-2003

Resources

Encrypt Message