Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I put that code into streamwriter and streamreader. I have tried several techniques but didnt work.

here is what i want to achieve:
id;
1
name:
jones
salary:
1200

id:
2
name:
gale
salary:
1300

id:
3
name:
bel
salary:
1800

but first i have to record it in a file rekisteri.txt. Then streamwrite it and finally streamread it.

What I have tried:

using System IO;

namespace ConsoleApp1
{
    class Ohjelma
    {
        public static void Main(string[] args)
        {
            string data = "E:\\visual studio 2017\\ConsoleApp1\\rekisteri.txt";

            FileStream fOutStream = File.Open(data, FileMode.Append, FileAccess.Write);

            StreamWriter sWriter = new StreamWriter(fOutStream);

            int id;
            string name;
            float salary;

            label:

            Console.WriteLine("Give id: ");
            id = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Give name: ");
            name = Convert.ToString(Console.ReadLine());

            Console.WriteLine("Give salary: ");
            salary = float.Parse(Console.ReadLine());

            
            if (id <= 3)
            {
                goto label;
            }
            else
            {
                Console.WriteLine();
            }
            Console.ReadLine();


            /*StreamReader code here*/
            
        }
    }
  
}        
Posted
Updated 26-Feb-20 4:23am

You are only writing the data to the console, you also need to write it to your sWriter stream. When there is no more data you need to close the stream. To read it back you just use a StreamReader.

See StreamWriter Class (System.IO) | Microsoft Docs[^] and StreamReader Class (System.IO) | Microsoft Docs[^].
 
Share this answer
 
For starters, get rid of labels and goto - you should use a loop construct like for or while instead. In fact, forget goto even exists for a couple of years - by then you will understand when it is appropriate, which in almost never in "normal" code.

You also should never use Convert methods on user input - they make mistakes, and it's kinda annoying when your app crashes because of a miskey.
C#
int id;
while (true)
   {
   Console.Write("Enter ID: ");
   if (int.TryParse(Console.ReadLine(), out id))
      {
      break;
      }
   Console.WriteLine($"\"{val}\" is not a valid integer.");
   }
Is a lot more friendly!

Write a method that takes four parameters:
C#
private static void WriteLine(StreamWriter sw, int id, string name, float salary)
   {
   }
And make it write the values to the stream. That way, it's more "organised" and clearer to read. And trivial: StreamWriter has a WriteLine method.

Then reading back is pretty simple too - just loop through the file until it runs out of data, and read each item back. Use the same TryParse code to convert teh values and it's pretty easy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900