Questions tagged [streamreader]

StreamReader is a C# class designed for character input in a particular encoding, it can be used for reading lines of information from a standard text file. By default, a StreamReader is not thread safe.

Implements a TextReader that reads characters from a byte stream in a particular encoding.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. StreamReader can be used for reading lines of information from a standard text file.

StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a Read method.

By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.

Official Microsoft documentation

1931 questions
3
votes
6 answers

Issues using StreamReader.EndOfStream?

So I'm doing a project where I am reading in a config file. The config file is just a list of string like "D 1 1", "C 2 2", etc. Now I haven't ever done a read/write in C# so I looked it up online expecting to find some sort of rendition of C/C++…
MattMacdonald
  • 93
  • 1
  • 1
  • 9
3
votes
4 answers

C# CSV dynamic split

I have multiple 1.5 GB CSV Files which contain billing information on multiple accounts for clients from a service provider. I am trying to split the large CSV file into smaller chunks for processing and formatting the data inside it. I do not want…
Saidur Rahman
  • 420
  • 2
  • 6
  • 19
3
votes
2 answers

C# StreamReader throws OutOfMemoryException on last line when splitting large delimited text file

I am writing a function in C# to split a large delimited file into smaller delimited files. I am writing this function because a 2.7 GB file was taking hours to ETL and was causing a bottleneck to the ETL batch. I assign a max number of lines per…
zBomb
  • 339
  • 1
  • 17
3
votes
3 answers

Why StreamReader.EndOfStream property change the BaseStream.Position value

I wrote this small program which reads every 5th character from Random.txt In random.txt I have one line of text: ABCDEFGHIJKLMNOPRST. I got the expected result: Position of A is 0 Position of F is 5 Position of K is 10 Position of P is 15 Here…
vldmrrdjcc
  • 2,082
  • 5
  • 22
  • 41
3
votes
1 answer

C# Increasing Efficiency of a Program?

I am working on a C# program that reads in very large files and is checking them for different attributes and fields. I had been testing with files with under 1 million lines and it was preforming as expected. I have recently tested it on a file…
buzzzzjay
  • 1,140
  • 6
  • 27
  • 54
3
votes
1 answer

how to convert Image to string the most efficient way?

I want to convert an image file to a string. The following works: MemoryStream ms = new MemoryStream(); Image1.Save(ms, ImageFormat.Jpeg); byte[] picture = ms.ToArray(); string formmattedPic = Convert.ToBase64String(picture); However, when saving…
user532104
  • 1,373
  • 6
  • 17
  • 27
3
votes
4 answers

C# .NET StreamWriter: How to skip lines when writing file using StreamWriter?

I read in a text file using StreamReader. I want to write out this same text file EXCEPT its first 4 lines and its last 6 lines. How do I do this? Thanks.
user776676
  • 4,265
  • 13
  • 58
  • 77
3
votes
2 answers

Can't read data correctly with BinaryReader from WebRequest

Hy! I'm developing a C# SOAP service to communicate with one of our governmental services. They also use SOAP for communication, but now they introduced two new endpoints that handle requests very differenty. The first thing that I noticed is that…
Balázs Varga
  • 1,797
  • 2
  • 16
  • 32
3
votes
1 answer

Writing to a file while reading it with streams?

I wrong a function to read a configuration file, but if the command line arguement "-ip x.x.x.x" is specified, I want to overwrite the IP setting in the configuration file. I am using the following code which reads fine, but appends my new line to…
3
votes
2 answers

Detect how each line of a file ends in C#

Is it possible to loop for each line in a file and check how it ends (LF / CRLF): using(StreamReader sr = new StreamReader("TestFile.txt")) { string line; while ((line = sr.ReadLine()) != null) { if (line.contain("\r\n") …
user2848242
  • 177
  • 1
  • 11
3
votes
5 answers

Reading the next line of a file only once

I have an application that reads information from a text file and then categorizes them and puts them onto a Database. For one category, I need to check the line that comes right after the current line and look for a certain keyword? How do i get…
techmanc
  • 293
  • 1
  • 7
  • 19
3
votes
2 answers

C# Can StreamReader check current line number?

I tried making a script that would read a TXT file line by line, and change labels depending on what is inside. Is there a way to check which line is being read?
JEREDEK
  • 87
  • 1
  • 9
3
votes
1 answer

StreamReader returning null

My goal is to start using Stream in order to read a HTTP contents (HttpResponseMessage.Content). As for the moment I'm using ReadAsStringAsync to read the content, and I would like to improve the performance and memory usage with…
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
3
votes
5 answers

How to find out how many characters a file has without reading the whole of it?

If the file is a text file, and StreamReader can figure out the Encoding it uses, how can I find out how much characters it has without reading the whole file? I'm reading 1GB CSV files and it takes at least 4 seconds to read it with a StreamReader.…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
3
votes
5 answers

Lengthy lines of code vs readability

This is perfectly fine C# code and works fine provided correct URL. But the everything is just done at one line by reducing the readability of the code. Here is the code : return new…
Shiva
  • 1,379
  • 1
  • 15
  • 32