Removing block of code from class if condition is met [duplicate] - c#

This question already has answers here:
How to delete a line from a text file in C#?
(11 answers)
Closed 10 years ago.
What I'm doing is reading a file line by line, and then comparing each line that's read with a pre-specified string. If it's a match, I want to remove the code from the class I'm inspecting, starting at the line that matched the pre-specified string until a designated location I've identified is reached. What I'm struggling with is how to implement a removal. I've written methods to add, subtract, adjust, etc., but never to just completely remove a chunk of code, so I don't know how to procede. I bet there's some simple way to accomplish this, but it's escaping me right now.

It sounds like you are asking "How do I remove something from the middle of a file?" - it just happens to be source code.
The easiest way is to create a temp file (or in memory stream) to which you write all the content you want to keep and then when you are done processing the old file you over-write it with the contents you choose to keep.

Related

Fastest way to initialize List<string> with data from file [duplicate]

This question already has answers here:
How best to read a File into List<string>
(10 answers)
Closed 5 years ago.
I'm trying to initialize a List<string> with some data from a file. The file is a list of words separated by carriage returns so currently, I am doing
var wordList = new List<string>(textFromFile.Split( new[] {"\r\n", "\r", "\n"}, StringSplitOptions.None ) )
but for the size of text files I'm dealing with (172,888 lines in one of the files!) this is very slow. Is there a better way to do this? The text file doesn't have to be formatted the way it is currently, I could parse it and write it out in a different format if there is a better method of storing the data. In C++ I would be thinking of binary data and a memcopy but I don't think there is a similar solution in C#?
If it's relevant, the code is in a Unity app so limited to early .NET capabilities of their Mono version
You might want to use File.ReadAllLines to read the file and it does exactly what you're looking for also it should be well optimized.
var wordList = File.ReadAllLines("yourFileSrc");
To improve performance even more you may want to split your files to N of files and process them in parallel using TPL (Task parallel library) or use .AsParallel method (as kindly suggested by Evk)
More info about PLINQ you can find here
*** Update
For parsing a large string you might want to split the string first (without parsing it) to a number of lesser strings and then process them in parallel.

read huge file line by line in c# [duplicate]

This question already has answers here:
Read line by line a large text file and search for a string
(2 answers)
Closed 7 years ago.
I have huge files in c# (more than 300 MB). I need effiecent way to read the file line by line because once I try to read it it takes more than 30 minutes while the target time is around 3 minutes. I tried File.ReadAllBytes which reads the files successfully and very fast and load it to the string. But after that it takes very long time to process the string line by line. Is there better way or faster way to do so.
Thank in advance.
You can use File.ReadLines, it will enumerate through lines of file:
var lines = File.ReadLines(path);
foreach(var line in lines)
{
// do your logic here
}
It will not load file at the first line. It will load it while looping through lines, so it's better way to read bigger files, than loading it at once.
MSDN says in description of File.ReadLines
Remarks The ReadLines and ReadAllLines methods differ as follows: When
you use ReadLines, you can start enumerating the collection of strings
before the whole collection is returned; when you use ReadAllLines,
you must wait for the whole array of strings be returned before you
can access the array. Therefore, when you are working with very large
files, ReadLines can be more efficient.

How To Read And Write On A File In Same Time Using C# [duplicate]

This question already has answers here:
Read and Write to File at the same time
(5 answers)
Closed 7 years ago.
i want to know how to read file and write on it in the same time for example:
file content:
Johny
tony
jack
Ahmad
Johny
string line;
line = file.ReadLine();
if (line == "johny")
{
line= "Sam"
}
You have to bear in mind that "Sam" and "Johnny" are not the same length. What will the file do with those empty bytes? Worse, what if you replaced "Sam" with "Johnny"? You will overwrite letters in the next record.
Fixed-width records can address this, but for a small file system, I would just read all into a list, then rewrite the whole list to file again. Or a different approach would be to set it all up in a database and let the database handle the reads and writes and you handle the business logic.
But just writing new data to a file on the fly as you are reading it is probably going to be more trouble than it's worth.

Replacing string in a list doesn't work [duplicate]

This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
I cannot, for the life of me, understand why the Replace(); method will not work when I try to replace the text of an element inside a list of type String. This question is, to an extent, related to a previous thread of mine [found here: Merging two files and handling duplicate entries (would be a good idea to view it, as well, for the full code)], but at the end of the day, it all boils down to the fact that the command doesn't work while, my code, is actually correct (!).
I'll give you an example of what doesn't work in my situation. In its most basic form, for the sake of example, this is what I'm trying to accomplish but it doesn't work (for real!):
[In my original code, I've triple-checked (with breakpoints and everything) that my list indeed contains the string I want to replace (I'm replacing the element itself!), but it just won't do it! Seriously now, it is a 4 element list at the present time (although more elements CAN be added, refer to other thread).]
One last thing, sorry about the punctuation (too many exclamation marks, I know), but I'm actually raging over this. Code below (remember, most basic form, but an example I tried):
// List[index].Replace(oldValue, newValue);
newFile[3].Replace(newFile[3], "Replace it with this!");
Could you help me with this?
newFile[3] = newFile[3].Replace(newFile[3], "Replace it with this!");
In c#, strings are immutable. As such, the Replace method returns a new string.
Edit:
The main reason for strings to be immutable is to make them thread-safe.
If you wanna find out more, have a read: Why .NET String is immutable?
newFile[3] = newFile[3].Replace(newFile[3], "Replace it with this!");

Looping through data from textfile [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reading/writing INI file in C#
I have a textfile to store certain data for my parser. The data stored looks something like this:
[Yandex]
Hostname=http://yandex.ru
Query=yandsearch?text=[QUERY]&lr=213
LinksMask=<a class="b-serp-item__title-link" tabindex="2"[...]href="[LINK]" onmousedown=
TotalPages=100
NextPage=<a id="next_page" class="b-pager__next" href="[LINK]" onmousedown
NextPage2=<a id="next_page" class="b-pager__next" href="[LINK]" onmousedown
CaptchaURL=showcaptcha?retpath
CaptchaImage=captchaimg?
CaptchaField=rep
[Google Classic]
Hostname=http://[GOOGLEHOST]
Query=search?as_q=[QUERY]&num=100&hl=en&output=ie&filter=0
LinksMask=<li class="g"><h3 class="r"><a[...]href="[LINK]"
TotalPages=10
NextPage=<td class="b" style="text-align:left"><a href="[LINK]"
NextPage2=</font></a></td><td><a href="[LINK]"
CaptchaURL=
CaptchaImage=
CaptchaField=
I want to parse out the data under each [XXXX]
and save the Hostname=, Query=, etc into strings..
What would the easiest way be to accomplish this?
Not looking to store the data in another way than textfile, i want it to be very easy to change for the user :)
This is known as ini file format, and you can use the Win32 API GetPrivateProfileString to read it, using P/Invoke to call the API. Please see here for more details. The article linked to by L.B. gives better/easier solutions though.

Categories

Resources