为什么在我尝试打开文件时文件中的空白丢失了 [英] Why blank space in my file are missing when i tried to open a file

查看:104
本文介绍了为什么在我尝试打开文件时文件中的空白丢失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我写了一个小代码打开文件,我的文件内容如下,每行长度为94个字符,行终止符为\ r和\ n

101 111111111 1111111111010190542A094101
9000000000001000000000000000000000000000000000000000000000000

//与此文字相同的作品

101 111111111 1111111111010190608A094101
52001 1 1 CCD1 101019101019 1111000020000001 6201110000251 00000000011 1 1 0111000020000001 820000000100111000020000000000000000000001 11000020000001 9000001000001000000010011100002000000000001000000000000

Hi all, I write a small code to open the file my file contents are as follows each line length is 94 characters and line terminator is \r and \n

101 111111111 1111111111010190542A094101
9000000000001000000000000000000000000000000000000000000

// same Works for this text

101 111111111 1111111111010190608A094101
52001 1 1 CCD1 101019101019 1111000020000001 6201110000251 00000000011 1 1 0111000020000001 820000000100111000020000000000000000000000011 111000020000001 9000001000001000000010011100002000000000001000000000000

private void mnuOpen_Click(object sender, EventArgs e)
    {
        string strFilePath = string.Empty;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.FileName = string.Empty;
        openFileDialog1.RestoreDirectory = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            strFilePath = openFileDialog1.FileName;
            StreamReader sr = new StreamReader(strFilePath);
            string str = string.Empty;
            str = sr.ReadToEnd().Replace("\r", "").Replace("\n", "");
            sr.Close();
            if (str.Length % 94 == 0)
            {
                 //Do some thing
             }
           }



但是我在这里没有出现错误,任何人都可以说出原因吗?



But I am not getting an error here, can anyone tell why?

推荐答案

您显示的数据行不是每行94个字符.二是95.
检查您的输入.

1)不要使用魔术数字"-94在您的代码中是1.为什么是94?答:因为那是数据大小.因此使用一个常量,并将其称为"dataSize".这样,如果数据大小发生变化,则只需要在一个地方进行更改.

2)如果您正在处理基于行的数据,则将其读取为行:
The lines of data you have shown are not 94 characters per line.If you concatenate the first two, it is 95.
Check your inputs.

1) Don''t use "magic numbers" - 94 is one in your code. Why is it 94? Answer: because that is the data size. So use a constant, and call it "dataSize" of similar. That way, if the data size changes, you only have to change it in one place.

2) If you are working with line-based data, then read it as lines:
string[] lines = File.ReadAllLines(openFileDialog1.FileName);

将读取整个文件,而无需担心流,然后将每行放入一个单独的字符串中,为您删除\ r和\ n.然后,您可以分别处理每组数据,并报告单个错误行"的错误.您不应该使用行终止符:对于所有系统,它们都不相同.您正在使用的系统知道行的结尾-使用它!

Will read your whole file, without worrying about streams, and place each line into a separate string, removing the \r and \n for you. You can then process each group of data separately, and report errors for individual "bad lines". You should not faff about with line terminators: they are not the same for all systems. The system you are working on knows what lines end with - use it!


这篇关于为什么在我尝试打开文件时文件中的空白丢失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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