Read random string from a file without reading all file - c#

how to read a random line from a file without reading all the file?
I tried FileStream class and Seek, Read functions. But didn't get luck with 100% working method.

This isn't possible as a line is defined by a carriage return or line break. There is no way to know how many lines there are in the file without reading all of it unless you know something about the structure of the file; for example, that every line is 80 characters. In that case you're picking a random 80 characters from a file of length N, in which case your FileStream seek is the way to go.

Related

Using StreamReader.ReadLine() to read a specific line number w/o reading entire file

I'm using StreamReader.ReadLine() in C# to read through a text file to find the specific content like "Step-xx" and then read and use the contents that point to the next occurrence of "Step-xx+1". I know the occurrence of the "Step-xx" line is 100 lines apart in my textfile. How can I jump to line 2500 and read the contents following "Step-25", rather than reading 2500 lines and comparing it to "Step-25", which I'm doing now. I need to speed up this search.
Thanks.
Files are not lines based (or even character based), so you can't skip to a specific line in a file.
If you really need to skip ahead in the file, you would need to make a guess where the 2500th line might start based on average line lengths, seek to that position and start reading. You would need to use a FileStream directly, not a StreamReader, and read the file as bytes. You would be looking for the 0x0d 0x0a byte combination that is used as newline in a Windows text file. When you have the bytes between two newlines, you can decode them into a string and look for the Step-xx markers.
Thanks for all the replies. This will do the trick.
string line = File.ReadLines(FileName).Skip(14).Take(1).First();
I need to figure out how changing from StreamReader to ReadLines would impact other things.
Thanks again

How to read and write byte array line by line in text file

I want to record binary message packets in text file, one message per one line. After that I will read line by line to parse into meaningful message.
I looked into binaryWriter class and found write method which writes byte array but could not find writeLine method.
Please suggest good approach to record byte array in text file.
When you write binary to a file; you aren't writing this:
1011100111011
0110101010101
1000110100101
Because thats not actually binary. That is textual (human-readable) representation of binary. A real binary file represented by text is the ASCII/Unicode encoding of the binary. Its very hard to read; if you want proof; just open up a PNG file in Notepad++.
Thus; having line endings for a binary file makes no sense at all. Hence, no WriteLine method on BinaryWriter.
If you want to write out the binary above; you need to format it as a string, like so:
textWriter.WriteLine(Convert.ToString(value, 2));
Now, you probably can just use BinaryWriter (that is how you write byte[] after all) but just don't expect it to be human readable! You would then use BinaryReader to deserialize your written file.
If you really want to save binary data to a text file, but also have line breaks, you might want to use Convert.ToBase64String. This will ensure you don't have any line feed characters inside of your binary data, that would inadvertently break the line.

C#: Search and replace txt line

I am looking for a way to search a comma separated txt file for a keyword, and then replace another keyword on that exact line. For example if i have the following line in a big txt file:
Help, 0
I want to find this line in the txt (by telling program to look for the first word 'help') and replace the 0 with 1 to indicate that i have read it once so it looks like:
Help, 1
Thanks
It is generally a very bad idea to try and overwrite data in the same file: if your code throws an exception, you'll be left with a partially processed file; if your search target and replacement value have different lengths, you have to re-write the rest of the file. Note that these don't apply in your specific situation - but it's best not to let it become habit.
My recommendation:
Open both the input file and a temporary file (Path.GetTempFileName)
process and write each line ( StreamReader.ReadLine)
When finished with no errors, rename the original file to something like origFile.old
rename the temporary file to the original file name.
If something goes wrong, delete the temporary file and exit. This way the original file is left intact in the event of an error.
If you want to do the replacement "in place" (meaning you don't want to use another, temporary, file) then you would do so with a FileStream.
You have a couple of options, you can Read through the file stream until you find the text that you're looking for, then issue a Write. Keep in mind that FileStream works at the byte level, so you'll need to take character encoding into consideration. Encoding.GetString will do the conversion.
Alternatively, you can search for the text, and note its position. Then you can open a FileStream and just Seek to that position. Then you can issue the Write.
This may be the most efficient way, but it's definitely more challenging then the naive option. With the naive implementation, you:
Read the entire file into memory (File.ReadAllText)
Perform the replace (Regex.Replace)
Write it back to disk (File.WriteAllText)
There's no second file, but you are bound by the amount of memory the system has. If you know you're always dealing with small files, then this could be an option. Otherwise, you need to read up on character encoding and file streams.
Here's another SO question on the topic (including sample code): Editing a text file in place through C#

Read a very long line of text C#

I want to read a very long line of text using C#. The length of the line is about 100000 chars. I am using StreamReader.ReadLine() method. I also want to write this long string to a file. One of them or both are not working properly. I could only write 99328 characters to a file.
How can I read and write a very long line of text using C#?
I suspect that you did not close your output buffer and it's not completing the file write.
Consider using
File.ReadAllText
http://msdn.microsoft.com/en-us/library/ms143368.aspx
and File.WriteAllText
http://msdn.microsoft.com/en-us/library/ms143375.aspx
You are probably failing to call .Flush() on your output stream.

C#: String.IndexOf to FileStream.Seek

having a FileStream that I read with a StreamReader (it is a very large file), how can I set the Seek position of the FileStream to the first occurrence of a certain substring so that I can start reading this large file from a given point?
Thanks
What's in the file? Just lines of Unicode text? Then you've got a problem.
You will never know the position of the start of a line until you've read all the previous lines at least once. Unless the file is encoded in UTF-32, each character may take a variable number of bytes to represent it. Each line will have a variable length.
The best you can do is to scan through the file once and then make note of the positions of the starts of lines, in an index.
FileStream cannot do the search for you. You'll have to manually search for it. Probably you'll want to use an efficient string searching algorithm such as Knuth Morris Pratt.
Maybe this can help (Building a Regular Expression Stream Search with the .NET Framework):
https://www.developer.com/design/building-a-regular-expression-stream-search-with-the-net-framework/
If you mean first time you read the file then well you will have to read to know the position (of the particular string). Next time if content of the file is not changing you can remember this position (in some variable for use in same run of program), set stream position and start reading it.
Take a look at this example on MSDN
filestream=new FileStream(s.Substring(s.IndexOf("string"),s.Length),FileMode.Open,FileAccess.Read);

Categories

Resources