使用c#编写文件 [英] Writing a file using c#

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

问题描述

我有3个字符串,每个字符串都有固定长度,即使字符串的长度小于实际长度,我也必须将其写入默认长度的文件中。





str1 =1234;(默认长度为4)

str2 =56;(默认长度为2)

str3 =789;(默认长度为3)



因此,我的文件中的字符串应该看起来像123456789,当str1 =123时我的字符串应该看起来像123 56789。



因此,当字符串的长度小于实际长度时,应该有一个空格。

解决方案

你可以试试以下



1.创建一个名为 FixedLengthString (例如)。

在类中你有一个构造函数,其长度为参数。

然后你可以实现一个明确的转换运算符 [ ^ ](它作为赋值运算符的重载),因此它填充的字符串短于固定长度并截断一个较长的字符串或抛出一条错误信息。

您还应该覆盖 ToString 方法以修剪尾随空格。

实现方法 void GetString(string fileContent,int index)从特定索引复制子字符串。



2.创建一个容器类,其列表包含FixedLengthString, List< FixedLengthString> ,作为成员变量以及序列化和反序列化方法。

在serialize方法中,循环遍历列表并将字符串附加到 StringBuilder 。然后将StringBuilder变量的内容写入文件。

在Deserialize方法中,将文件读入字符串然后循环遍历列表并使用 GetString 方法。

每次迭代都用当前列表成员的长度更新索引。



不要忘记如果文件内容要短,则添加错误处理等。



如果字符串数量可变或将随着时间的推移而增加,这将为您节省大量麻烦。


使用 alignment 选项 [ ^ ]格式字符串:

< pre lang =cs> string str1 = 123;
string str2 = 4;
string str3 = 5;
string result = string .Format( {0,-4} {1,-2} {2,-3},str1,str2,str3);
// result ==123 4 5


尝试做类似的事情。

 List< string> stringData =  new  List< string> {  1234  56  789}; 
int [] dataLength = { 4 4 3 };
string spaces = ;
StringBuilder sb = new StringBuilder();
int i = 0 ;
foreach var s in stringData)
{
sb.Append(s).Append(spaces.Substring( 0 ,dataLength [i] - s.Length) );
i ++;
}
Console.WriteLine(sb);


I have 3 strings and each string has a fixed length and I have to write it in a file with default length even when the length of the string is lesser than the actual length.


str1="1234";(default length is 4)
str2 = "56";(default length is 2)
str3 = "789";(default length is 3)

So, my string in file should look like "123456789" and when str1 = "123" my string should look like "123 56789".

So there should be a empty space when the length of the string is lesser than actual length.

解决方案

You could try the following

1. Create a class called FixedLengthString (for example).
In the class you have a constructor with a the length as a parameter.
Then you can implement an explicit conversion operator[^] (it works as an overload of the assignment operator) so it pads a string shorter than the fixed length and truncates a longer string or throws an error message.
You should also override the ToString method in order to trim the trailing space.
Implent a method void GetString(string fileContent, int index) that copies a sub-string from a specific index.

2. Create a container class that has a list of FixedLengthString, List<FixedLengthString>, as a member variable and a Serialize and Deserialize method.
In the serialize method you loop through your list and append the strings to a StringBuilder. Then write the content of the StringBuilder variable to the file.
In the Deserialize method you read the file into a string and then you loop through the list and use the GetString method.
Update the index with the length of the current list member for every iteration.

Don't forget to add error handling if the file content is to short etc.

This will save you a lot of hassle if the number of strings are variable or will be increased over time.


This is simple to accomplish using the alignment option[^] in a format string:

string str1 = "123";
string str2 = "4";
string str3 = "5";
string result = string.Format("{0,-4}{1,-2}{2,-3}", str1, str2, str3);
// result == "123 4 5  "


Try doing something like this.

List<string>stringData= new List<string>{"1234","56","789"};
           int[] dataLength =  { 4, 4, 3 };
           string spaces = "                          ";
           StringBuilder sb= new StringBuilder();
           int i = 0;
           foreach (var s in stringData)
           {
               sb.Append(s).Append(spaces.Substring(0, dataLength[i] - s.Length));
               i++;
           }
           Console.WriteLine(sb);


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

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