Question write to a text file

clone99

New member
Joined
Oct 17, 2022
Messages
3
Programming Experience
1-3
have makes this code and i want to writet the foreachlopp to to a textile. have try witch streamwriter but it not working . it only print out for one salerman,
can add that I am a beginner at programming
C#:
using System.IO;
using System.Text;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;

namespace Task2
{
    internal class salesman // class for the salesman
    {
        public string name;
        public string person;
        public string location;
        public int count;

        public salesman(string name, string person, string location, int number) // calls the variables
        {
            this.name = name; // this. = reference to the class
            this.person = person;
            this.location = location;
            this.number = number;
        }

    }
    
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" number of sales layers to be registered");
            int snus = int.Parse(Console.ReadLine() ?? "0"); // enter how many sellers are to be registered

            List<salesman> salesman = new List<salesman>(); // list for the class

            for (int j = 0; j < snus; j++)
            {

                Console.WriteLine();
                Console.WriteLine(" your name ");
                var fornman = Console.ReadLine() ?? "0";
                Console.WriteLine(" social security number ");
                var number = Console.ReadLine() ?? "0";
                Console.WriteLine(" enter palts/district ");
                var dist = Console.ReadLine() ?? "0";
                Console.WriteLine(" enter number ");
                var items = int.Parse(Console.ReadLine() ?? "0");

                Console.WriteLine();
                var sa = new salesman(firstman, number, dist, items);

                sälajre.Add(sa); //calls the method
            }

            var level1 = selajre.Where(n => n.number < 50); // level1
            method seller(1, level1); // calls the method

            var level2 = selajre.Where(n => n.count >= 50 && n.count < 100); // level2
            method seller(2, level2);
            var level3 = selajre.Where(n => n.count >= 100 && n.count < 200);// level3
            method seller(3, level3);

            var level4 = selajre.Where(n => n.number > 200); // level4
            method seller(4, level4);
            Console.ReadLine();
        }
        
        static async void method salesman(int level, IEnumerable<salesman> salesmen) // method salesman
        {
            if (!salesmen.Any())
            {
                return;
            }

            Console.WriteLine("-------------------");

            foreach (var rape in salesmen.OrderBy(n => n.number)) // foreach is used to repeat the narrowing
            {
                Console.WriteLine("Name: " + rape.name); // displays seller name
                Console.WriteLine("Personal ID: " + rape.person); //
                Console.WriteLine("District/Location: " + rape.location); // display location
                Console.WriteLine("Number sold " + rape.number); // shows how many items sold
                Console.WriteLine("Seller has reached level " + level); // determines what level the seller has reached
                Console.WriteLine("-----------------------------");
                Console.WriteLine("-----------------------------");
            }
            
            string filename = "test.txt";
            
            if (File.Exists(filename))
                File.Delete(filename);

            StreamWriter sw = new StreamWriter(filename);
            
            sw.Write("Name: \n " + rape.name);
            sw.Write("Personal identification number: \n " + rape.person);
            sw.Write("District/Location: \n " + rape.location);
            sw.Write("Number sold \n " + rape.number);
            sw.Write("Seller has reached level \n " + level);

            sw.Close();
        }
        
        Console.WriteLine(salesmen.Count() + " salesman has reached level " + level); // prints the level at which most sellers have sold
 
Last edited by a moderator:
You don't have the code that writes to the file inside a loop. If you expect to write to the file inside the loop, the code that writes to the file needs to be inside that loop.
 
You don't have the code that writes to the file inside a loop. If you expect to write to the file inside the loop, the code that writes to the file needs to be inside that loop.
yeah that work but i want to write multyp salesman, if I put
the code in the foreach like this. it only write only one of salesman.


C#:
 foreach (var rape in salesmen.OrderBy(n => n.number)) // foreach is used to repeat the narrowing
            {
                Console.WriteLine("Name: " + rape.name); // displays seller name
                Console.WriteLine("Personal ID: " + rape.person); //
                Console.WriteLine("District/Location: " + rape.location); // display location
                Console.WriteLine("Number sold " + rape.number); // shows how many items sold
                Console.WriteLine("Seller has reached level " + level); // determines what level the seller has reached
                Console.WriteLine("-----------------------------");
                Console.WriteLine("-----------------------------");
           
           
     string filename = "test.txt";
           
            if (File.Exists(filename))
                File.Delete(filename);

            StreamWriter sw = new StreamWriter(filename);
           
            sw.Write("Name: \n " + rape.name);
            sw.Write("Personal identification number: \n " + rape.person);
            sw.Write("District/Location: \n " + rape.location);
            sw.Write("Number sold \n " + rape.number);
            sw.Write("Seller has reached level \n " + level);

            sw.Close();
        }
}
 
Last edited by a moderator:
This is what happens when you write code without taking the time to think about the logic that the code needs to implement first. What happens when you create a new StreamWriter like that? If you've read the documentation, which you should have, then you know that it overwrites an existing file. If you create a new StreamWriter that way on every iteration of the loop, why would you expect to see anything other than the last set of data? Again, if you've read the documentation, you will know that you can specify that the file should be appended to instead of overwritten and how to do that. That's not a good approach though. Why open and close the same file repeatedly in quick succession at all? The logic thing to do is to open the file first, before the loop, then write all the data, then close the file. That would alleviate the problem of data being overwritten and make the whole process more efficient because it doesn't waste time opening and closing the same file multiple times.
 
Your code doesn't contain any meaningful comments. You don't know C# yet; you think in english. Write the algorithm of your program in comments first, in english, and then write the C# code underneath each comment. Leave the comment in. The comment should usually include the "reason why", not the "what"..

Yes:
C#:
//this app is going to compare some product, in a list of product change histories, with the previous one to see if the price has changed

//make a variable to track the current loop item, so we can also -1 off it to refer to the previous loop item
int idx = 1;

No:
C#:
//my first app

//make a variable called idx and assign 1 to it
int idx = 1;


This is no different to e.g. writing an essay in an exam - you should spend some minutes writing a plan of what you want to write about, and how long to spend on each section. This stops you getting lost, wandering off on a tangent, spendign ages writing about some minutiae worth only a few marks, forgetting your original point and writing a load of garbage. Coding is the same

Make a plan first; it'll stop this from happening. Beginners should write comments 100% of the time
 
Last edited:
Back
Top Bottom