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.
Related
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.
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.
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.
convert string to isolatedfilestream? i am trying to convert string in to isolatedfilestream and re-write in XML file on wp7. can any one suggest me. thank in advance
If you wish to store and retrieve only XML data, please check the following tutorial. It will help you in understanding how to store XML data.
http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-XML-files-using-XmlWriter
additionally do check the following tutorial when you want to save large amount of data, specifically when u want to store an object of a class. This will teach you how to store and retrieve data using XMLserializer, this will also maintain the entire sequence of data stored.
http://rushabh.greenpoison.org.in/blog/2012/06/07/local-windows-phone-7-database-using-xmlserializer/
You can use IsolatedFileStream like any other stream, to a large extent. So any way that you would write to a stream on the desktop (e.g. XmlWriter.Create(stream)) should be fine. For files small enough to load into memory, I'd personally use LINQ to XML most of the time - again, you can load that from a stream, and then save it to an XmlWriter wrapping an IsolatedFileStream.
I would recommend against loading the XML as a string first and then parsing it; if you load it directly from the stream, you allow the parser to handle the declared text encoding itself, rather than you having to do it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I convert an integer into its verbal representation?
I am looking for some solution to return a number as text.
By that I mean that 100 should be returned as 'one hundred'
It is more or less just a tidious task to make such a function myself, but I rather not re-invent the wheel, and this can't be the first time someones has requested this.
Unfortunatly my search so far has not turned up anything, so here I try stackowerflow.
Basically the numbers comes from a database, so if there is some smart methods you could use here it would be pretty nice.
As mentioned, a small function that returns eg. 100 as 'one hundred' is not a complicated task, but what if you need to take language considarations into the solution?
Has anybody come accross something that actually can do this, and perhaps in multiple languages?
Yes you can use this:
Java numbers to text
Basically the idea is to form the numbers by simply defining all the digits and the tenths, and after that you can also the define the hundreds, the thousands and so on. Because numbers in English are always formed the same way, this is very easy for the English language.
Something like this (may be not the best one, I just make a draft search)?
http://www.daniweb.com/software-development/csharp/threads/53072