简体   繁体   English

StreamReader.ReadLine()出现意外结果

[英]Unexpected results with StreamReader.ReadLine()

I am currently testing an application that stores the user's contact details in a file. 我目前正在测试一个将用户的联系方式存储在文件中的应用程序。 This information is also stored in a local compact database as the primary method - storing their details in this file is a backup in case those details are lost. 此信息也作为主要方法存储在本地压缩数据库中-如果将这些详细信息丢失,则将其详细信息存储在此文件中是一种备份。

The file I'm using for testing has my personal data in it, so I hope you understand that I have replaced the lines with placeholders! 我用于测试的文件中包含我的个人数据,因此希望您理解我已将这些行替换为占位符! The structure of this file is as follows (minus the first line): 该文件的结构如下(减去第一行):

File: 


Business Name
Mr.
Joe
Bloggs
user@email.com

Address Line 1
Address Line 2
City
Postcode
Country        




07777123456

Below, I have some code that reads this file and stores each line as a variable. 下面,我有一些代码读取该文件并将每一行存储为变量。 The structure of the file will never change, hence the very simple code: 文件的结构永远不会改变,因此非常简单的代码:

public static bool RestoreBusinessTable(out string title, out string busName, out string mobileNumber, out string firstName, out string lastName)
        {    
            string email = "", referral = "", contactNo, addressLine1 = "", addressLine2 = "", city = "", postcode = "", country = "", district = "";
            busName = null;
            mobileNumber = null;
            firstName = null;
            lastName = null;
            title = null;

            try
            {
                if (!File.Exists(fileName))
                    return false;
                StreamReader sr = new StreamReader(fileName);
                string work;
                work = sr.ReadLine();   // Empty line
                work = sr.ReadLine();   // Empty line
                busName = sr.ReadLine();
                title = sr.ReadLine();
                firstName = sr.ReadLine();
                lastName = sr.ReadLine();
                email = sr.ReadLine();
                referral = sr.ReadLine();
                addressLine1 = sr.ReadLine();
                addressLine2 = sr.ReadLine();
                city = sr.ReadLine();
                postcode = sr.ReadLine();
                country = sr.ReadLine();
                work = sr.ReadLine();  // Empty line
                work = sr.ReadLine();   // Empty line
                contactNo = sr.ReadLine();
                district = sr.ReadLine();
                mobileNumber = sr.ReadLine();
                sr.Close();
                // Add to database here
                return true;
            }
            catch
            {
                return false;
            }
        }

When running this code, I noticed that busName , title , firstName , and lastName all had a value of 07777123456 . 运行此代码时,我注意到busNametitlefirstNamelastName的值均为07777123456 The data looked like this: 数据如下所示:

07777123456
07777123456
07777123456
07777123456
user@email.com

Address Line 1
Address Line 2
City
Postcode
Country




07777123456

I do not have any asynchronous processes or threads which are writing to the file at the same time. 我没有任何异步进程或线程正在同时写入文件。 Could anyone shed any light as to what is happening here, and why the first four lines would appear as the mobile number? 任何人都不能对这里发生的事情有所了解,为什么前四行会显示为手机号码?

One way to do that would be for the calling code to supply the same variable's/field's address to the various out parameters: 一种方法是调用代码向各种out参数提供相同的变量/字段的地址:

string tmp;
RestoreBusinessTable(out tmp, out tmp, out tmp, ...);

Here the same address is passed in each location, so no matter whether your code assigns to title , busName , etc it is writing to the same actual location . 这里在每个位置都传递相同的地址,因此无论您的代码是否分配给titlebusName等,它都将写入相同的实际位置

Since mobileNumber is assigned last, the value assigned for the mobile number will be the value that appears for all the values. 由于mobileNumber是最后分配的,因此分配给移动号码的值将是为所有值显示的值。

The key point here is that title , busName , etc are not each a reference to a string - because of the out (or ref , equally) they are each a reference to a reference to a string . 这里的关键点是titlebusName等并不是每个字符串引用 -因为out (或ref ,同样)它们都是对字符串的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM