Answered by:
Makin a logbook and need some help.

Question
-
Hello Ladies and gents.
I ask you guys once more for some answers. Im studying C# and is new to it and need some help with my project.
Its a logbook. There you should be able to:
1. Post a log message with a title and description // This i figured out.
2. Get all log messages printed to screen // This i figured out.
3. Search the log.txt files for a specific word // Need help on this
4. Erase a log message and its file.txt // Need help on this
5. End program // This is figured out
Here is the sauce:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace PROJEKTloggbok { class MainClass { public static void Main(string[] args) { StreamWriter streamWriter; string Titel; string besKriv; string sokord; // Backup string for search function int index; // Backup string for search function DateTime tiden = DateTime.Now; while (true) { // Skapa loop så att man återvänder till meny Console.WriteLine("___________________________________"); Console.WriteLine("\n\tBRF HOUSEMAFFIA´S LOGGBOK"); Console.WriteLine("[1] Skriv ett inlägg"); // OM val 1, GÖR MEDAN användaren skriver "slut". (Post a message in log) Console.WriteLine("[2] Skriv ut alla inlägg"); // ANNARS OM val 2, innehållet skrivs ut. ( print out all logmessages to screen) Console.WriteLine("[3] Sök inlägg"); // ANNARS OM val 3, innehållet/listan töms. (Search log files for a specific word) Console.WriteLine("[4] Radera ett Inlägg"); // Delete a log entry or its file Console.WriteLine("[5] Avsluta"); // Avsluta (Terminate program) Console.Write("\nVälj: "); // Choose in menu int val; int.TryParse(Console.ReadLine(), out val); // Läser av valet och fångar up felaktig inmatning. if (val == 1) { String dateStr = tiden.ToString("yyyy-MM-dd"); // Datum skrivs till loggen Console.WriteLine("När du har skrivit färdigt ditt inlägg så \nskriv bara -> end <- för att återgå till menyn."); // Tells user to end the logentry with the word "end" Console.Write("\nTitel (brand,bråk,skadegörelse osv) : "); // fyll i titel ( fill title etc) Titel = Console.ReadLine().ToUpperInvariant(); Console.WriteLine("Beskrivning (Beskriv vad som hände) : ");// Beskriv vad som hände ( Type a small report) do { besKriv = Console.ReadLine(); if (besKriv.ToLower() == "end") // om logginlägg = end avbryts loopen break; streamWriter = File.CreateText(dateStr + "-Loggbok-" + Titel + ".txt"); // uttökar textfilens namn (extend the file names streamWriter.WriteLine(tiden + "\n" + Titel + "\n" + besKriv); // skriver i dokumentet (write to file) streamWriter.Close(); } while (true); } else if (val == 2) { Console.Clear(); foreach (string filename in Directory.GetFiles(".", "*.txt")) // shows all content in logfiles to screen { StreamReader sr = new StreamReader(filename); Console.WriteLine(sr.ReadToEnd()); sr.Close(); } { Console.WriteLine("Tryck på valfri tangent för att återgå till menyn :"); // Press a key to return to main menu Console.ReadKey(); } } else if (val == 3) { Console.WriteLine(" Skriv in ett sökord :"); // Search the .txt files for a specific word sokord = Console.ReadLine(); StreamReader break; } else if (val == 4) { // Delete a log entry Ie the whole .txt file of it. break; } else if (val == 5) // Shut down the program { break; } else { Console.WriteLine("\nFelaktig inmatning!"); continue; } } } } }
Thanks for looking at this guys :)
Mike
Answers
-
Since this is an assignment, we're not allowed to just do it for you.
This is because assignments are an important part of the learning process and if you just get a cut and paste solution then all you learn is cut and paste. Which is not good.
Also, a student runs the risk of being given a solution which involves advanced techniques they haven't studied yet. Further devaluing the learning experience because when you read the solution you don't understand it. If you don't really understand the code then you could get a bad solution suggested. Peer review may well help you out there but I've seen threads where there are a dozen solutions to a problem suggested. Sometimes a number of them are not good.
.
I think you have no response on deletion because it isn't clear what is required.
You have a filename from the outer foreach.
You can therefore use that to delete a file, based on whatever your criteria are.
Something like
File.Delete(filename);
Back your files up before you go deleting any.
Also.
Stick a break point in and see exactly what you end up with for your first filename.
- Proposed as answer by Kristin Xie Monday, February 15, 2016 10:00 AM
- Marked as answer by Kristin Xie Wednesday, February 17, 2016 9:54 AM
All replies
-
-
In order to find a specific substring in a file for the case 3, read the text similar to case 2, the use Contains:
string some_text = "some text to seek";
foreach (string filename in Directory.GetFiles(".", "*.txt"))
{
using( var sr = new StreamReader( filename))
{
string full_text = sr.ReadToEnd();
if( full_text.Contains(some_text))
{
Console.WriteLine( $“The text was found in file {filename}”);
}
}
}It is also possible to use Regular Expressions, to scan the files line-by-line and to perform a case-insensitive searching.
-
-
Since this is an assignment, we're not allowed to just do it for you.
This is because assignments are an important part of the learning process and if you just get a cut and paste solution then all you learn is cut and paste. Which is not good.
Also, a student runs the risk of being given a solution which involves advanced techniques they haven't studied yet. Further devaluing the learning experience because when you read the solution you don't understand it. If you don't really understand the code then you could get a bad solution suggested. Peer review may well help you out there but I've seen threads where there are a dozen solutions to a problem suggested. Sometimes a number of them are not good.
.
I think you have no response on deletion because it isn't clear what is required.
You have a filename from the outer foreach.
You can therefore use that to delete a file, based on whatever your criteria are.
Something like
File.Delete(filename);
Back your files up before you go deleting any.
Also.
Stick a break point in and see exactly what you end up with for your first filename.
- Proposed as answer by Kristin Xie Monday, February 15, 2016 10:00 AM
- Marked as answer by Kristin Xie Wednesday, February 17, 2016 9:54 AM
-
Thanks for reply.
Yes its an assignment and i took it a bit more advanced than I´M needed, The teacher is happy if it do this as simple console app that uses just memory and not read/write to a file. But myself wanted to take it to next level and we have not gotten to the part where you use steamreader yet. So you can say i´m working ahead.
I made it more advanced just to push it a bit. But i totally agree about "cut´n paste" education.
Ill search for examples here on MSDN and more for solutions.
Cheers,