This question already has answers here:
Read last line of text file
(6 answers)
Closed 8 years ago.
Scenario is the following:
A (weather) service dumps sensor data into a log file/text file.
The new readings are appended to the bottom of a given (existing) file
New data is added at regular intervals (interval may or may not be known)
I need to parse the new information/line and send it off to another service.
I don't want to read the whole file every time, unless I have to.
EDIT: Sorry for the bad wording. "unless I have to" should be understood as if there is no other way around. I have seen the post/answer referenced and it seems a little extensive.
Framework is 4.5.x.
Thank you.
To get the the last line of a text file you can use this
File.ReadLines(myFileName).Last();
This is the simplest method, but is inefficient. You can write your own parser as show here
Related
This question already has answers here:
Should log file streams be opened/closed on each write or kept open during a desktop application's lifetime?
(13 answers)
Closed 6 years ago.
i need to write data into a CSV file each 600 msec using a c# application . The question: is better open and close file each time or keep it open until the end of write data actions? Note: i will change file name each day and each 60000 record
Thanck a lot for your opinions
CSV files are really easy to write to. If you don't know how to write to a file, the dotnetperls is your friend. You can simply call BinaryWriter.Write() to write anything. Write a value then a comma. That's it! If this file is going to be edited by the user at the time of running the application, then don't keep it open. Otherwise, keeping it open makes sure nothing unexpected happens.
This question already has answers here:
Comparing list of strings with an available dictionary/thesaurus
(2 answers)
Closed 7 years ago.
I'm using C# to write a program that generates lines of text over and over. The user enters a set of numbers, 1-26, in whatever order, and the program matches each number to a letter.
The point is to have it go through every order of the alphabet until it eventually generates an actual word. For example, someone could enter 7-2-15-26-3, and it would eventually read that set of numbers as "hello".
I got the program to work and to print every outcome to a txt file, but because there are so many different possible outcomes, it is almost impossible to find an actual word in the file without going through every single line.
One of my tests only had 11 letters to choose from, it took a few minutes to finish and the txt file was so big, it would not open.
So my question is, does anyone know of a library or spell check that I could use to check if each string is an actual word? If I could check it each time, I could have it only print the outcomes that are words. I would have it check against preset words, but I won't always know what the outcome will be so I need to check against everything.
I have searched online but haven't found much. Again, I'm using C#. Thank you for any help.
Edit: Sorry about asking a question that had already been answered, I didn't see the other question before. I'll try the NHunspell and see how that works.
Try Nhunspell, it's free (.Net version of popular "Hunspell")
E.g.
Check Spelling,
bool correct = hunspell.Spell("Recommendation");
Get suggestions,
List<string> suggestions = hunspell.Suggest("Recommendatio");
More c# code samples
I suggest that you incorporate an english dictionary into your application so that you have something to check against.
Every time a new word is generated, it checks through the dictionary and takes all the matching results through regex and returns null if no word matches.
Hope this helps.
This question already has answers here:
How to insert characters to a file using C#
(10 answers)
Closed 7 years ago.
I'm wondering if there is a way to write data from the program to a specific line on an external text document.
For example, so far within my program I have a string array which gets the users input and saves it in the corresponding element and into the text file. It can read the first element fine, however how do I get it so when I enter the input for element 2 it overwrites what is on line 2 so when it reads the text file it displays the newest input into a label?
Well, the simplest approach would be:
string[] lines = File.ReadAllLines(path);
lines[index] = newText;
File.WriteAllLines(path, lines);
However, that assumes that there's already an appropriate number of lines in the file - is that always the case?
Note that this is also potentially inefficient in memory if the file is very large - but making it more efficient would also make the code more complicated.
This question already has answers here:
How to join 2 or more .WAV files together programmatically?
(5 answers)
Closed 9 years ago.
I am working on Regional Language Text to Voice converter in C#. I have wav files for individual characters.
I want to merge them to get a resultant word's single WAV file.
Im using NAudio library. It supports the concatenate method which takes outputFile and IEnumerable string sourceFiles as arguments.
But when I execute the method I get NullArgumentException in the line which creates WavFileReader object.
But I have a passed a string of array containing filepaths and an existing empty output wavfile as arguments. I am stuck here.
Could you please suggest me how to use this method,as in how to call this method? and what arguments to use...
There is no Concatenate method in NAudio. I assume you are referring to my answer here. The output WAV file should not already exist - it will be created for you. But the most common cause for an ArgumentNull exception is a null parameter being passed into the function. Are you completely sure your source files array does not contain a null. (adding the stack trace of the exception to your question might help us diagnose further).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read a text file reversely with iterator in C#
I need to read txt file more than 7 million lines from bottom to the top by just following line of code: I was wondering is the right way or shall i use iterator for this task? Some answers using iterator already in stackoverflow.
foreach (string line in File.ReadAllLines("read.txt").Reverse())
{
Console.WriteLine(line);
}
As files are not line based, there is no convenient way to read lines from the end of the file. What you are doing works fine, as long as the entire file fits in memory.
If you run into memory problems, you can read the file in chunks, e.g. File.ReadLines(path).Skip(6000000), then File.ReadLines(path).Skip(5000000).Take(1000000). This will read the file up to that point every time, but it will use less memory.
Another alternative would be to read chunks of bytes from the file, locate the line breaks in the bytes, and decode the bytes between them into strings.