0
    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();
        }
    }
}

Im trying to read in a full line but ReadLine is string and the first thing im pulling from the file is: 123456

123456

15

2011

69.99

book

Problem_Solving_With_C++

Walter_Savitch (the actual text file doesn't have empty lines inbetween)

then 15 then 2011, which should I use? Read? Will I have to use some kind of loop to figure out when the end of the line is?

Nogg
  • 337
  • 1
  • 4
  • 18
  • Huh? What's wrong with `ReadLine()`? – SLaks Feb 03 '12 at 00:29
  • Im trying to save 123456 as an int. Its saying ReadLine saves as a string. Cannot save type string as type int. – Nogg Feb 03 '12 at 00:31
  • 1
    So your problem is not how to read line, but how to convert a string into a number or how to read number from file. Please rephrase your question. – Damian Leszczyński - Vash Feb 03 '12 at 00:33
  • 1
    possible duplicate of [Read numbers from a text file in C#](http://stackoverflow.com/questions/1968328/read-numbers-from-a-text-file-in-c-sharp) – Damian Leszczyński - Vash Feb 03 '12 at 00:35
  • 1
    @Nogg: I have no idea what you just asked. Vash is very right; your question is very misleading. – SLaks Feb 03 '12 at 00:38
  • @Nogg: You must understand that SO, is not for solving your personal issues, but for providing a solution of problem for everyone that might occur some obstacle developing software. – Damian Leszczyński - Vash Feb 03 '12 at 00:38
  • @Vash, please show me where it states that SO is not intended to help solve problems. You made me laugh when you said that SO is just for issues that everyone "might" occur. If that was the case then SO wouldn't be able to run. SO is for individual problems because SO is intended for anyone to ask a question and the hive mind of people who know more about the subject can help. I simply do not have the knowledge of C# as I do in C++. Thus why I come here for help, and up until now have been fine until you take it upon yourself to continuously target me. – Nogg Feb 03 '12 at 00:50
  • @Nogg, I did not have any interest to insult you. That continuously targeting of your person, is consequence of your attitude. To be honest I recognize only 3 users on SO and you are not any of them. You also don't have knowledge, how to behave or develop OO sofware or read with understanding which you have confirmed with your laugh. Nevertheless this is not a place for an argue, if you feel the victim feel free to write me a message on this profile. EOT. – Damian Leszczyński - Vash Feb 03 '12 at 01:10

3 Answers3

1

You need to parse the string into a number, using a function like int.Parse.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

If you open up the file in notepad, you may find that there are line spaces that you don't see in other text editors.

ReadinLine() should read until it finds the character '\n'

Sorry if you already knew this, but it drove me mad for a while until I eventually found that there were line spaces I couldn't see. Try notepad, and try wordpad,.. do you see any differences?

ThePerson
  • 3,048
  • 8
  • 43
  • 69
0

For the int values, you must still use ReadLine(), but then parse as an 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();

    }
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73