質問

I'm currently working on an Inventory app assignment on Windows Forms C# My main Form displays 3 options which are:

  1. Register a product
  2. Purchase a product and
  3. Exit

For options 1 and 3 I already have what I need. However, for option 2, which is where the user should be able to purchase products already registered, I don't know how to "look for the product" in the files where it's saved.

The file stores the products info like this: (showing the name of the product, the quantity, the price how many pieces have been sold, from top to bottom)

Chair

100
10
0

Mouse

95
15
5

Laptop

50
13
4

I did this before in Console Application, but I didn't store info in Files, I did it with arrays and simply used a "for" cycle to find the product I needed and from there I could do the rest...

I was told in class that I needed to read the file line by line until I find the product I need and turn it into a variable? How can I do that in a Forms platform?

役に立ちましたか?

解決

I was told in class that I needed to read the file line by line until I find the product I need and turn it into a variable? How can I do that in a Forms platform?

Assuming the file format is strongly consistent, something like this should work:

    //A class to hold the individual pieces of data
    public class Item
    {
        public string Name = "";
        public int Qty = 0;
        public double Price = 0;
        public int QtySold = 0;
    }
    public Item FindItem(string filename, string itemname)
    {
        //An object of type Item that will hold the specific values
        Item output = new Item();
        //The using block handles automatic disposal of the streamreader
        using(StreamReader sr = new StreamReader(filename))
        {
            //Read the file until the end is reached
            while(!sr.EndOfStream)
            {
                //Check the string from the file against the item name you're
                //looking for.
                string temp = sr.ReadLine().Trim();
                if(temp == itemname)
                {
                    //Once we find it, throw away the empty line and start
                    //assigning the data to the output object.
                    sr.ReadLine();
                    output.Name = temp;
                    output.Qty = int.Parse(sr.ReadLine());
                    output.Price = double.Parse(sr.ReadLine());
                    output.QtySold = int.Parse(sr.ReadLine());
                    //Since we found the item we're looking, there's no need
                    //to keep looping
                    break;
                }
            }

        }
        //The file is closed and output will either have real data or an empty
        //name and the rest all 0's
        return output;
    }

他のヒント

You can get the file contents as a string array using the File.ReadAllLines method. Then lookup the string you want and get corresponding data as needed.

Pseudo-code (Assuming your file structure is as shown)

string[] values = File.ReadAllLines(filepath);
int count = values.Length;

for (i = 0 to count - 1)
     if(i%6 == 0) <compare your desired object to the objects from file>
     if (match_found) <get  the  corresponding data for object from next 4 lines>

I guess you are asking for a help on how reading a file, am I right?

The following link can provide you with an example:

Edit:

using (StreamReader sr = new StreamReader("TestFile.txt")) 
{
    string line;
    // Read and display lines from the file until the end of  
    // the file is reached. 
    while ((line = sr.ReadLine()) != null) 
    {
        Console.WriteLine(line);
    }
}

Hope this can help you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top