I am looking how to read from a file search for the word then from that word put it in a textbox to a end word
like from date end paragraph
ty
like from date end paragraph
ty
|
C#
C# reading a text file |
|
You could do something similar to this:
Code using System;
Sep 10, 2019 09:42 AM
(This post was last modified: Sep 10, 2019 09:42 AM by vaiperboy.)
(Sep 10, 2019 12:06 AM)Bunnyslippers69 Wrote: You could do something similar to this:well thats not really a good way since it loads all of the lines into the memory, which sometimes can be useless (imagine if the word was found in the first line?) ,slow and being able to crash also if I understood correctly you want to find where line a contains a certain word and return that line? if so, @OP, you could load one line at a memory and checking that. This can be done by Code foreach (var line in File.ReadLines("path")) {or if you feeling linqy Code var lines = File.ReadLines("path")but thats just real basic. All depends on how big your file is, if you need faster stuff to handle bigger stuff look at https://github.com/pdonald/aho-corasick FYI, the linq version searches for all occurrences “I wish I could say everything in one word. I hate all the things that can happen between the beginning of a sentence and the end.” ~ Leonard Cohen
Sep 10, 2019 12:46 PM
(This post was last modified: Sep 10, 2019 12:47 PM by MavenGradle.)
(Sep 10, 2019 12:06 AM)Bunnyslippers69 Wrote: You could do something similar to this: No error handling and like vaiperboy pointed out this is very inefficient when dealing with very large files. Same goes for the LINQ method vaiperboy posted. It's just a prettier way to write all that code. (Sep 10, 2019 09:42 AM)vaiperboy Wrote: If it's important that these files remain accessible while you're doing your thing, you might not want to use the File.ReadLines method because it will place a full on write block handle on the file. You might be more inclined to use a StreamReader like so: Code var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ![]() (Sep 20, 2019 02:43 PM)n0s1eep Wrote: string.Contains is perfectly fine for this use case. Use of regexp is overkill, a professional programmer knows when to use the right tool for the right job. The above user has no clue what he's talking about so just ignore his idea to use regex. The guy can't even build a regex pattern to match duplicate words. Use IndexOf and specify the appropriate string comparison enum value.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Oct 3, 2019 03:02 PM
(This post was last modified: Oct 3, 2019 03:06 PM by max_int.)
Code using System;(Oct 3, 2019 03:02 PM)max_int Wrote: I stopped reading when i saw found=true “I wish I could say everything in one word. I hate all the things that can happen between the beginning of a sentence and the end.” ~ Leonard Cohen (Oct 4, 2019 06:53 AM)vaiperboy Wrote: I stopped reading when i saw found=trueCan you explain please ? I didn't pretend it was the most efficient way to do it. (Oct 4, 2019 07:02 AM)max_int Wrote: Can you explain please ?I mean for one, its a void. How you returning a boolean to it? secondly, for this piece of code Code if (line.Contains(someText, comparer)) {Code [code]“I wish I could say everything in one word. I hate all the things that can happen between the beginning of a sentence and the end.” ~ Leonard Cohen (Oct 4, 2019 07:16 AM)vaiperboy Wrote: I mean for one, its a void. How you returning a boolean to it?I agree with the idea of returning immediately when the string is found. Anyway, in some cases, it may not be the best option 1. you need to do some computations inside this function before returning. 2. In case you use an unmanaged resource inside the loop, i may be very bad to exit without cleaning. The code i posted is from a bigger class where i face the both cases, that's why i'm not exiting in the loop. Cheers
Oct 4, 2019 08:58 AM
(This post was last modified: Oct 4, 2019 09:10 AM by MavenGradle.)
(Oct 4, 2019 07:45 AM)max_int Wrote: I agree with the idea of returning immediately when the string is found. You have no reason not to return immediately inside the function that you shared with us, which by the way will not compile due to an incorrect return value and because the string.Contains method has NO OVERLOAD for a StringComparer. You're using a using statement which means the StreamReader will be disposed of correctly no matter how you look at it. If an exception is thrown at any point (including evaluating the return value) Dispose will still be called too. Even if this is part of a larger code base, what you're doing is still incredibly bad practice because it adds more steps in the chain which are simply unnecessary. I agree that any resources should be disposed of properly because the .NET GC is not perfect, has been known to let certain things linger in memory etc. In such cases, you'd be better of using a using statement inside a using statement to make sure that the dispose method will be called correctly or dispose of it yourself in the right manner by calling the IDisposable.Dispose() method. ![]() |
|
|
| Possibly Related Threads... | |||
| Thread / Author | Replies | Views | Last Post |
| [C#] by Formula 1Dec 6, 2019 01:11 AM | 23 | 708 |
Dec 6, 2019 01:11 AM Last Post: itcorp |
| [C] by pebblakOct 15, 2019 05:24 AM | 6 | 307 |
Oct 15, 2019 05:24 AM Last Post: Joseph Ducreux |
| [C] by nuvexy.Oct 6, 2019 03:13 PM | 13 | 516 |
Oct 6, 2019 03:13 PM Last Post: themayonnaiseguy |