I just can’t do and understand my assignment, so I decided to pour it in parts here. Since you need to do but need tips and instructions from professionals.
There is a certain class countFiles and in it there are fields user, address, date, traffic.
There is a constructor that initializes everything.
There is a parsingfiles method that parses the files.
Question: In what place did I make a mistake, file proparsil, saved the result in a variable list?
I create a new instance of the countFiles class to process the next file, and it was here that errors fell down.
class countFiles { public string user { get; set; } public string adres { get; set; } public int trafik { get; set; } public DateTime data { get; set; } public countFiles(string userr = "", string adress = "", int traffic = 0,DateTime datta=new DateTime()) { user = userr; adres = adress; trafik = traffic; data = datta; } public static countFiles parsingfiles(string lline) { string[] filelist = Directory.GetFiles(@"D:\logfiles", "*.txt"); List<countFiles> list = new List<countFiles>(); //здесь будет результат DateTime datat; int trafic; for (int i = 1; i < filelist.Length; i++) { if (filelist[i] == "") continue; string[] lineParts = filelist[i].Split(' '); list.Add(new countFiles(lineParts[0], lineParts[1], int.Parse(lineParts[2]),DateTime.Parse(lineParts[3]))); countFiles ekz = new countFiles(list[user],list[adres],trafic,datat);---Ошибки здесь return ekz; } } } Errors:
- The most appropriate overloaded method for "Lab_2.2.CountFiles.countFiles (string, string, int, System.DateTime)" has several invalid arguments
4 fields, all initialized but he swears that unacceptable. Why?
- Error 1 For a non-static field, method, or property "Lab_2.countFiles.user.get", an object reference is required.
That is, I need to do both class and constructor static? And if you do not want?
- Error 4 Argument "1": type conversion from "Lab_2.2.countFiles" to "string" is not possible.
This is a dead end, since the question is also interesting lines and sheet. Identical. Propalsil and brought, proparsil and brought that he does not like.
Problem number two.
static Statistics processFile(String file) { //если нет файла - то нет и статистики по нему if (!File.Exists(file)) { return new Statistics(); } StreamReader sr = System.IO.File.OpenText(file); String line; Statistics sc = new Statistics(); //читаем строки while ((line = sr.ReadLine()) != null) { //парсим var ekz = countFiles.parsingfiles(line); //обновляем статистику по критериям UInt64 bytes = 0; sc.userstat[ekz.user] = (sc.userstat.TryGetValue(ekz.user, out bytes) ? bytes : 0) + ekz.trafik; sc.adrestat[ekz.adres] = (sc.adrestat.TryGetValue(ekz.adres, out bytes) ? bytes : 0) + ekz.trafik; sc.trafikstat[ekz.data] = (sc.trafikstat.TryGetValue(ekz.data, out bytes) ? bytes : 0) + ekz.trafik; } return sc; } Error - The operator "+" cannot be applied to operands of the type "ulong" and "int".
I need to take statistics, and I have traffic there in general, and here is it? It turns out you need to change to another type or something?
Thanks for the tips to the classes.
Changed a little under the task, and added flows.
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Data; using System.Collections; using System.Text.RegularExpressions; using System.Threading; using System.Text; using System.Threading.Tasks; namespace LabWork2 { public class Line { public string User; public string Address; public int Traffic; public DateTime Date; public string ToCsv(bool header = false) { var s = ", "; return header ? String.Concat("User", s, "Address", s, "Traffic", s, "Date") : String.Concat( this.User, s, this.Address, s, this.Traffic, s, this.Date.ToString("d.MM")); } public static IEnumerable<Line> Parse(string logDir, string pattern, int skip = 0) { foreach (var file in Directory.EnumerateFiles(logDir, pattern)) foreach (var line in ParseFile(file, 1)) yield return line; } public static IEnumerable<Line> ParseFile(string path, int skip = 0) { return Parse(File.ReadLines(path).Skip(skip)); } } class Stat { public class Item { public string Key; public string Address; public int Traffic; public DateTime From; public DateTime To; public string ToCsv(bool header=false) { var s = ", "; return header ? String.Concat("Key", s, "Traffic", s, "From", s, "To") : String.Concat( this.Key, s, this.Traffic, s, this.From.ToString("d.MM"), s, this.To.ToString("d.MM")); } public string ToCsv1(bool header = false) { var s = ", "; return header ? String.Concat("Adress", s, "Traffic", s) : String.Concat( this.Address, s, this.Traffic, s); } public string ToCsv2(bool header = false) { var s = ", "; return header ? String.Concat( "Traffic", s, "From", s, "To") : String.Concat( this.Traffic, s, this.From.ToString("d.MM"), s, this.To.ToString("d.MM")); } } Dictionary<string, Item> tbl = new Dictionary<string, Item>(); public void Add(string key, Line line) { Item itm; if (tbl.TryGetValue(key, out itm) == false) { itm = new Item { Key = key, From = line.Date, To = line.Date }; tbl.Add(key, itm); } itm.From = new DateTime(Math.Min(itm.From.Ticks, line.Date.Ticks)); itm.To = new DateTime(Math.Max(itm.To.Ticks, line.Date.Ticks)); itm.Traffic += line.Traffic; } public IEnumerable<Item> Items { get { return tbl.Values; } } } class Program { static Queue<String> m_workFiles = new Queue<String>();//файлы на обработку static System.Collections.Generic.List<Stat> m_threadResult; //результат выполнения потока static bool m_iscomplete = false;//флаг завершения ввода static readonly object m_locker = new object();//мьютекс для регулирования доступа к очереди файлов на обработку public void processDirectory(String logDir)//извлечь все файлы очередь для обработки { if (!Directory.Exists(logDir)) { return; } lock (m_locker)//захватить мьютекс { foreach (var x in Directory.EnumerateFiles(logDir)) { m_workFiles.Enqueue(x); } } m_iscomplete = true;//установить флаг завершения } static void Main(string[] args) { Console.WriteLine("Ввод каталога:"); String logDir = Console.ReadLine(); Program pr = new Program(); pr.processDirectory(logDir); Line ln = new Line(); Stat st = new Stat(); string file; var userstat = new Stat(); Thread[] threads = new Thread[7]; for (int i = 0; i < 7; i++) { threads[i] = new Thread(new ThreadStart(Line.ParseFile(file,1))); threads[i].Name = String.Format("Работает поток {0}",i); } for (int i = 0; i < 7; i++) { threads[i] = new Thread(new ThreadStart(Stat.Add(1,line))); threads[i].Name = String.Format("Работает поток {0}", i); } foreach (var line in ln.ParseFile(logDir, "log*.txt", 1)) userstat.Add(line.User, line); Console.WriteLine(ln.ToCsv(true)); foreach (var si in userstat.Items) Console.WriteLine(si.ToCsv()); Console.WriteLine("Запись файлов-отчётов"); File.WriteAllLines(logDir + "userstat.txt",userstat.Items.Select(si => si.ToCsv())); File.WriteAllLines(logDir + "adrestat.txt",userstat.Items.Select(si => si.ToCsv1())); File.WriteAllLines(logDir + "datastat.txt",userstat.Items.Select(si => si.ToCsv2())); Console.WriteLine("Файлы записаны"); } } } Errors
threads[i] = new Thread(new ThreadStart(Line.ParseFile(file,1))); Error 2 Expected method name.
Before that, the compiler cursed that it was not necessary to create an instance, I directly addressed the class, and wrote that the name of the method was expected.
Or do you need to create a delegate?
threads[i] = new Thread(new ThreadStart(Stat.Add(1,line)));- Имя "line" отсутствует в текущем контексте Right here I honestly forgot, how do I call the method in the stream with the parameters that in the class they need to be declared in Maine or do the link?
public static IEnumerable<Line> ParseFile(string path, int skip = 0) { return Parse(File.ReadLines(path).Skip(skip));----- Ни одна из перегрузок метода "Parse" не принимает "1" аргументов,если напишу отдельно каждый всё равно компилятор светит ошибку. } What I represent under these variables and what I understood from the code corrected: logDir(root) is the name of the directory where I have files;pattern - file template (its contents);skip - the number of elements in which the Skip method of the same name will be traversed.
public static IEnumerable<Line> Parse(string logDir, string pattern, int skip = 0) { foreach (var file in Directory.EnumerateFiles(logDir, pattern)) foreach (var line in ParseFile(logDir,file, 1)) yield return line; } public static IEnumerable<Line> ParseFile(string path,string text, int skip = 0) { return Parse(File.ReadLines(path).Skip(skip),File.ReadAllText(text).Skip(skip),File.ReadAllText(text.ToString()).Skip(skip)); } In this line, return Parse(File.ReadLines(path).Skip(skip),File.ReadAllText(text).Skip(skip),File.ReadAllText(text.ToString()).Skip(skip));
A bunch of bugs:
Error 1 The most appropriate overloaded method for "System.IO.File.ReadAllText (string)" has several invalid arguments
Error 2 The most appropriate overloaded method for "LabWork2.Line.Parse (string, string, int)" has several invalid arguments
Error 3 Argument "1": type conversion from "System.Collections.Generic.IEnumerable" to "string" is impossible
Error 4 Argument "2": type conversion from "System.Collections.Generic.IEnumerable" to "string" is impossible
Error 5 Argument "3": type conversion from "System.Collections.Generic.IEnumerable" to "int" is not possible
The Parse() method is called with 3 arguments. That is, I read the path and template of the file and the number of elements.
If I understood the first, then with the second and third misunderstandings.
Using ReadAllText I already read the file name or its contents (2nd argument).
Concerning the 3rd time, the argument is an integer or ToString or ToInt?
Regarding the transformations, I think that after errors with arguments, errors can and will go away.