使用C#创建动态txt文件 [英] Make a dynamic txt file with C#

查看:83
本文介绍了使用C#创建动态txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于某些输入创建一个txt文件。



如果语句查看特定情况并且如果是真的:



将特定段落或句子输出到txt文件中。



如何构建它,以便在某些情况下不存在时,它不会留下大量的空白区域。



Ex:此特定文档中不存在操作3,但可能在其他文档中存在。

text.txt输出



动作1

(动作1多线)

动作2

动作4

动作5



我尝试了什么:



我不知道该往哪里走得更远有了这个。单靠字符串是不够的。我需要连接到下一个字符串的下一个空行。



string text =第一句输出



System.IO.File.WriteAllText(@C:\ Test \ Test.txt,text);

解决方案

很难在没有看到你的代码的情况下给出一个确切的答案,但这些问题通常是由这样的代码引起的;



 StringBuilder输出=  new  StringBuilder(); 
while true
{
Console.Write(< span class =code-string> >);
string line = Console.ReadLine();
output.AppendLine(line);
Console.WriteLine( 输出到目前为止);
Console.WriteLine( -------------);
Console.WriteLine(output.ToString());
Console.WriteLine();
}





这里我们读取一行并每次附加输入,所以如果该行为空或空格我们得到产出缺口。解决方案是在将文本添加到输出之前检查文本是否为空;



 StringBuilder输出=  new  StringBuilder(); 
while true
{
Console.Write(< span class =code-string> >);
string line = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(line))
{
output.AppendLine(line);
}
Console.WriteLine( 输出到目前为止);
Console.WriteLine( -------------);
Console.WriteLine(output.ToString());
Console.WriteLine();





如果这没有帮助,你需要发布你的代码。


我想基于某些输入创建一个txt文件。



要创建一个文本文件你可以使用 StreamWriter Class(System.IO) [ ^ $



if语句查看特定情况并且如果为true:将特定段落或句子输出到txt文件中。





  if (SpecificCase )
{
MyStreamWriter.WriteLine( 特定段落或句子。 );
// 或者如果你想看上去花哨
// MyStreamWriter.Write(特定段落或句子\ n);
}





如何构建它,以便在某些情况下不存在时,它不会留下大量的空白区域。



编辑:白线是由调用WriteLine而不是添加到新行引起的。所以,我建议你不要叫WriteLine或Write,如果它是空的,你不想只是一个空的空间。



这是一个例子来自使用switch语句:



 使用(StreamWriter sw =  new  StreamWriter(  myTextFile.txt)) 
{
string LineToWrite = String .Empty;
switch (SpecificCase)
{
case Case1
LineToWrite = < span class =code-string>特定消息;
break ;
case Case2
LineToWrite = 不同的消息;
break ;
}

sw.WriteLine(LineToWrite);
}





编辑:如果您使用case default,您基本上可以插入任何代码在几个if开关之后你会添加到else。如果我没有弄错的话,如果没有找到其他匹配就会一直这样。有关详细信息,请访问 http://www.dotnetperls.com/switch [ ^ ]



使用流读取或写入时作家一定要使用使用声明来确保你的StreamWriter正确处理。



最好的问候,

- Eddie


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);

解决方案

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

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;

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.


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.


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:

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


这篇关于使用C#创建动态txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆