【问题标题】:How to read a line from a file?如何从文件中读取一行?
【发布时间】:2012-02-03 00:28:05
【问题描述】:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace as2
{
    class Program
    {
        static void Main(string[] args)
        {
            int id = 0, stock = 0, published = 0, newstock = 0;
            double price = 0.00;
            string type = " ", title = " ", author = " ";

            Program inventroy = new Program();
            inventroy.read_one_record(ref id, ref stock, ref published, ref price, ref type, ref title, ref author);

            Console.WriteLine("Update Number In Stock");
            Console.WriteLine("=======================");
            Console.Write("Item ID: ");
            Console.WriteLine(id);
            Console.WriteLine("Item Type: ");
            Console.Write(type);
        }

        void read_one_record(ref int id, ref int stock, ref int published, ref double price, ref string type, ref string title, ref string author)
        {
            StreamReader myFile = File.OpenText("Inventory.dat");

            id = int.Parse(myFile.ReadLine());
            stock = int.Parse(myFile.ReadLine());
            published= int.Parse(myFile.ReadLine());
            stock = int.Parse(myFile.ReadLine());
            price = double.Parse(myFile.ReadLine());
            type = myFile.ReadLine();
            title = myFile.ReadLine();
            author = myFile.ReadLine();

            myFile.Close();

        }
        void write_one_record(int id, int newstock, int published, double price, string type, string title, string author)
        {
            StreamWriter myFile = new StreamWriter(File.OpenWrite("Inventory.dat"));


            myFile.WriteLine(id);
            myFile.WriteLine(newstock);
            myFile.WriteLine(published);
            myFile.WriteLine(price);
            myFile.WriteLine(type);
            myFile.WriteLine(title);
            myFile.WriteLine(author);

            myFile.Close();
        }
    }
}

我试图读一整行,但 ReadLine 是字符串,我从文件中提取的第一件事是:123456

123456

15

2011

69.99

Problem_Solving_With_C++

Walter_Savitch (实际的文本文件之间没有空行)

然后是 15,然后是 2011,我应该使用哪个?读?我是否必须使用某种循环来确定行尾是什么时候?

【问题讨论】:

  • 嗯? ReadLine() 怎么了?
  • 我试图将 123456 保存为 int。它说 ReadLine 保存为字符串。无法将类型字符串保存为 int 类型。
  • 所以你的问题不是如何读取行,而是如何将字符串转换为数字或如何从文件中读取数字。请改写您的问题。
  • @Nogg:我不知道你刚刚问了什么。 Vash 是非常正确的;您的问题非常具有误导性。

标签: c#


【解决方案1】:

您需要使用int.Parse 之类的函数将字符串解析为数字。

【讨论】:

  • 谢谢,这行得通,但是当我执行我的代码时出现错误。有什么帮助吗?我会更新我的代码。
  • 未处理异常:System.FormatException:输入字符串格式不正确
  • 这意味着该字符串不是有效数字。
【解决方案2】:

对于 int 值,您仍然必须使用 ReadLine(),然后解析为 int。

    void read_one_record(ref int id, ref int stock, ref int published, ref double price, ref string type, ref string title, ref string author)
    {
        StreamReader myFile = File.OpenText("Inventory.dat");

        id = int.Parse(myFile.ReadLine());
        stock = int.Parse(myFile.ReadLine());
        published= int.Parse(myFile.ReadLine());
        stock = int.Parse(myFile.ReadLine());
        price = double.Parse(myFile.ReadLine());
        type = myFile.ReadLine();
        title = myFile.ReadLine();
        author = myFile.ReadLine();

        myFile.Close();

    }

【讨论】:

    【解决方案3】:

    如果您在记事本中打开文件,您可能会发现有些行空格是您在其他文本编辑器中看不到的。

    ReadinLine() 应该读取直到找到字符'\n'

    很抱歉,如果您已经知道这一点,但它让我发疯了一段时间,直到我最终发现有些行空间我看不到。试试记事​​本,试试写字板,.. 有什么不同吗?

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2017-10-23
      • 2013-12-17
      • 2013-07-28
      • 2017-06-23
      • 2018-08-20
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多