0

Solved - Accidentally wiped the actual file of data but the ide doesn't update that file unless you reopen it so I thought the data was still in there.

I'm working on a project in C# that reads in a fake inventory and then transactions to go with that inventory in another class. I know the code is quite messy at the moment, i'm just trying to figure out one thing specifically.

this code here will read in my file "Inventory.in", check if the file exists(which it does) and then start my streamreader and readline into a string.

private void btnFill_Click(object sender, EventArgs e)
    {
        string strInputLine;
        string inFile = "Inventory.in";
        StreamReader iSR;

        InvRec currInvent;

        if (File.Exists(inFile))
        {
            iSR = new StreamReader(inFile);

            strInputLine = iSR.ReadLine();

            while (strInputLine != null)
            {

all of it isn't there but the point of emphasis is that the

"strInputLine = iSR.ReadLine();"

does indeed give me the correct value from that file.

now moving on to my question, the second block of code from another button is below:

private void btnProcess_Click(object sender, EventArgs e)
    {
        string strInputVal;
        string inFileName = "Transactions.in";
        TransRec currTrans;
        StreamReader iSR2;

        transListClass transactionList = new transListClass();

        InvRec inventItem = inventoryList.Retrieve();

        if (File.Exists(inFileName))
        {
            iSR2 = new StreamReader(inFileName);

            strInputVal = iSR2.ReadLine();

            while (strInputVal != null)
            {

my problem lies when I try and do my ReadLine() into iSR2. it gives me null instead of the value from the file I'm supposed to get. Everything is the same otherwise, it just refuses to give me the correct value. I'm following the debugger in Visual Studio 2010, so I know the file is found and exists, I see it being opened, just a null value instead of my string I need.

Thank you to anybody in advance, I appreciate it. -Anthony

edit: the second block of code is supposed to read from "Transactions.in" instead of the first one that reads from "Inventory.in"

Important edit: I have noticed when i changed "Transactions.in" to "Inventory.in" in the second block of code for the process button, it reads the values from Inventory.in and gives me a proper string unlike null from Transactions.in. The file is found and is just a text file named Transactions.in that was provided by my teacher, nobody else had a problem with it.

  • Is the code you provided copied from your project, or did you type it in as a sample? – Mike Dinescu May 12 '14 at 00:58
  • Everything is not the same. ;) You're reading in Inventory.in for the first code sample but Transactions.in for the second. Would that happen to be the issue? – eshs May 12 '14 at 00:59
  • it is the code copied and pasted, not made up @MikyDinescu – SiggyxLeGiiT May 12 '14 at 01:00
  • @eshs the second one had a correct file, it's supposed to be a different one. perhaps i didnt make it clear. but it still gives a null value on the ReadLine when it is a document full of values – SiggyxLeGiiT May 12 '14 at 01:01
  • @user3626735 Is the first line in that file empty? – Simon Whitehead May 12 '14 at 01:02
  • @SimonWhitehead no, it is not. i copied that line to ReadLine 5 times, still gives a null value. – SiggyxLeGiiT May 12 '14 at 01:02
  • @SiggyxLeGiiT Perhaps try File.ReadAllLines and process each one like that? Or at least see if that works. – eshs May 12 '14 at 01:05
  • 1
    Wouldn't it be great if both of these event handlers invoked a single routine that contained the logic for reading a file so you didn't have different behavior for what should be identical logic? – Preston Guillot May 12 '14 at 01:07
  • What happens if you use ReadToEnd instead? – Mick May 12 '14 at 01:07
  • @eshs i posted an edit at the bottom of the post. – SiggyxLeGiiT May 12 '14 at 01:10
  • @PrestonGuillot the two input files have two distinct different formats. – SiggyxLeGiiT May 12 '14 at 01:10
  • Parsing each line of text and reading lines of text in a file until you have no more lines of text are two different things - what you've shown here is clearly copy/paste logic. – Preston Guillot May 12 '14 at 01:12
  • I found the problem, rookie mistake. Transactions.in is shown with values inside of my IDE, but I must have opened the file without closing it so wiped it on mistake. I opened the actual file and pasted the data back in so I have data. Thank you @SimonWhitehead – SiggyxLeGiiT May 12 '14 at 01:14

0 Answers0