Search code examples
c#newlinesaving-data

c# problem with saving to txt and new line in data.txt


I have a problem. I have to save figures to txt ( its not important what it means). I would like to have space between numbers. When I try to save it without spaces it works good This is my code without space

if (x_dolny > x1_sqr && x_dolny < x2_sqr)
{ 
   using (StreamWriter streamW = new StreamWriter("C:\\Users\\Desktop\\outputdata.txt", true))
   {
      streamW.Write("1");
   }
}
else if (y_prawy < y1_sqr && y_prawy > y2_sqr)
{
   using (StreamWriter streamW = new StreamWriter("C:\\Users\\Desktop\\outputdata.txt", true))
   {
      streamW.Write("1");
   }
}
else if (delta >= 0.0)
{
   using (StreamWriter streamW = new StreamWriter("C:\\Users\\Desktop\\outputdata.txt", true))
   {
      streamW.Write("2");
   }
}
else
{
    using (StreamWriter streamW = new StreamWriter("C:\\Users\\Desktop\\outputdata.txt", true))
      {
         streamW.Write("0");
      }
}
using (StreamWriter streamW = new StreamWriter("C:\\Users\\Desktop\\outputdata.txt", true))
{
   streamW.WriteLine("New line");
}

And here is a screenshot outputdata.txt screen without spaces

and then I tried to add "\t" So now I have

streamW.Write("1 \t");
streamW.Write("2 \t");
streamW.Write("0 \t");

but the file looks like this Code with spaces

I dont understand why it cuts my line and move to the next line... Any ideas?


Solution

  • As @Sinatr mentioned in the comments, this could just be a word-wrap issue in your text editor.

    Off topic though, this code could really use some refactoring to make it simpler and much easier to read and understand.

    using (var streamWriter = new StreamWriter(@"C:\Users\Desktop\outputdata.txt", true))
    {
        string message = "0";
    
        if (MessageIs1(x_dolny, x1_sqr, x2_sqr, y_prawy, y1_sqr, y2_sqr))
            message = "1";
        else if (delta >= 0.0)
            message = "2";
    
        streamWriter.Write(message);
        streamWriter.WriteLine("New line");
    }
    
    // ...
    
    private static bool MessageIs1(int x_dolny, int x1_sqr, int x2_sqr, 
                                   int y_prawy, int y1_sqr, int y2_sqr)
    {
        return (x_dolny > x1_sqr && x_dolny < x2_sqr) 
            || (y_prawy < y1_sqr && y_prawy > y2_sqr);
    }
    

    Here's some suggestions:

    1. You reused the StreamWriter in every branch of your if statements and even after. Instead of typing the using statements several times, wrap it all in one using statement. Try to follow the DRY principle (Don't Repeat Yourself).

    2. You can use var when the data type is obvious from the assignment (such as creating new objects).

    3. Try to make your identifiers (variable names and method names) as clear and concise as possible. Others reading your code shouldn't have to look around a lot to figure out what your variables or methods are doing; it should be obvious just from the name. Avoid abbreviations because others may not know what the abbreviation stands for or may assume it means something else. I didn't know what your variables were for because I couldn't look at the full source, but I would definitely rename the following from my example above: x_dolny, x1_sqr, x2_sqr, y_prawy, y1_sqr, y2_sqr, MessageIs1.

    4. Extract complex boolean logic into a method so it is easier to read and change if you or someone else needs to later.

    5. Use verbatim string literals for file paths.

    6. Try to adhere to the KISS Principle (Keep It Simple, Stupid) as much as possible. Most developers over-engineer. Simple code is easy to read, understand, maintain, and extend.