Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
1.89/5 (5 votes)
See more:
I want to create a txt file based on certain inputs.

if statements looking at the specific cases and if true:

Output a specific paragraph or sentence into the txt file.

How can I build it so when some cases dont exist it doesn't just leave a lot of white space.

Ex: Action 3 happened to not exist in this specific document, but may in others.
text.txt output

Action 1
(action 1 multi-lined)
Action 2
Action 4
Action 5

What I have tried:

I am not sure where to go further with this. The string alone isn't enough. I would need to attatch to the next blank line available for the next string.

string text = "first sentence output"

System.IO.File.WriteAllText(@"C:\Test\Test.txt", text);
Posted
Updated 13-May-16 1:25am
v2
Comments
Sergey Alexandrovich Kryukov 12-May-16 17:55pm    
The whole idea seems quite bad; maybe even the idea to use file (text file or not) is already bad. It sounds pointless, it sounds awful low-tech.
If you explain your ultimate purpose, it may help to advise you something more appropriate.
—SA
George Jonsson 12-May-16 18:15pm    
The only way this question makes sense is if it is homework.
Sergey Alexandrovich Kryukov 12-May-16 18:16pm    
I'm afraid as a homework it makes even less sense. :-)
—SA
George Jonsson 12-May-16 18:22pm    
I have seen some pretty weird student exercises before, but who knows.
Sergey Alexandrovich Kryukov 12-May-16 18:33pm    
I'm not saying it's not homework, I doubt it would be reasonable homework.

Often, "weird" home assignment is not bad thing; it can really be interesting and often very useful. I'm talking about much worst things.

Here is what I mean: Recently, I wrote about my CodeProject experience. I found that, judging by a good number of student's questions, they study software engineering at "school" where the "professors" have no clue on the subject, which makes whole school pure fake. Student pays money, that's it... This is really sad...

—SA

It's hard to give an exact answer without seeing your code, but these problems are normally caused by code like this;

C#
StringBuilder output = new StringBuilder();
while (true)
{
    Console.Write("> ");
    string line = Console.ReadLine();
    output.AppendLine(line);
    Console.WriteLine("Output so far");
    Console.WriteLine("-------------");
    Console.WriteLine(output.ToString());
    Console.WriteLine();
}


Here we read a line and append the input every time, so if the line is empty or spaces we get gaps in the output. The solution is to check if the text is empty before adding it to the output;

C#
StringBuilder output = new StringBuilder();
while (true)
{
    Console.Write("> ");
    string line = Console.ReadLine();
    if (!string.IsNullOrWhiteSpace(line))
    {
        output.AppendLine(line);
    }
    Console.WriteLine("Output so far");
    Console.WriteLine("-------------");
    Console.WriteLine(output.ToString());
    Console.WriteLine();


If that doesn't help you'll need to post your code.
 
Share this answer
 
I want to create a txt file based on certain inputs.

To create a text file you can use the StreamWriter Class (System.IO)[^]

if statements looking at the specific cases and if true: Output a specific paragraph or sentence into the txt file.


C#
if(SpecificCase)
{
  MyStreamWriter.WriteLine("A specific paragraph or sentence.");
  // Or if you want to look fancy
  // MyStreamWriter.Write("A specific paragraph or sentence \n");
}


How can I build it so when some cases dont exist it doesn't just leave a lot of white space.

Edit: White lines are caused by calling "WriteLine" and not adding to the new line. So, I advise you to just not call "WriteLine" or "Write" if it's empty and you don't want just an empty space.

Here's an example by using a switch statement:

C#
using(StreamWriter sw = new StreamWriter("myTextFile.txt"))
{
string LineToWrite = String.Empty;
switch(SpecificCase)
{
case "Case1":
LineToWrite = "A specific message";
break;
case "Case2":
LineToWrite = "A different Message";
break;
}

sw.WriteLine(LineToWrite);
}


Edit: If you use "case default" you can basically insert any code you'd add to the "else" after a couple of "if" switches. It's the case it will always do if no other match has been found if I'm not mistaken. More information can be found at http://www.dotnetperls.com/switch[^]

While reading or writing using the stream writer be sure to use a "using statement" to make sure your StreamWriter disposes properly.

Best Regards,
- Eddie
 
Share this answer
 
v7
Comments
Simon_Whale 13-May-16 8:00am    
with the Little information that you have given I would do something like this

using(StreamWriter sw = new StreamWriter("myTextFile.txt"))
{
string LineToWrite = String.Empty;
switch(SpecificCase)
{
case "Case1":
LineToWrite = "A specific message";
break;
case "Case2":
LineToWrite = "A different Message";
break;
}

sw.WriteLine(LineToWrite);
}

You will possibly need to do some loop around the switch statement if you need to write more than one line to the file while it's open. There are some other things that you will need to think about. I.e. what happens when a condition is not met you dont want to write a blank line to the file. But hopefully this will give you a starting point.
It's Eddie! 13-May-16 9:59am    
Ofcourse a switch statement is advised when there's only a few cases, but from OPs post I have no idea how many cases - I just assumed he meant either something specific or nothing.

Edit: Turns out I misread some of the stuff he wrote - tried to answer it the best I could.

Great example I'll add it to my post.
- Eddie

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