如何从文件中读取一行?

时间:2012-02-03 00:28:05

标签: c#

    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 (实际的文本文件中间没有空行)

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

如果您在记事本中打开文件,您可能会发现在其他文本编辑器中没有看到行间距。

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

很抱歉,如果你已经知道了这一点,但它让我疯了一会儿,直到我最终发现有线条空间我看不到。尝试记事本,尝试使用wordpad,..你看到有什么不同吗?

答案 2 :(得分:0)

对于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();

    }