[.net] i don't really have a name for this problem [SOLVED]

Started by
6 comments, last by Bob Janova 17 years, 11 months ago
Let's say I have a class:

class A()
{
   public foo(string fileName)
   {
       //do something with the file
   }

   static void main()
   {
      foo(@"somefile.txt");
   }
}
The first time I run code like this, it works fine (assuming somefile.txt exists). Then, on subsequent runs, if somefile.txt doesn't exist and I change the '@"somefile.txt"' to something like '@"adifferentfile.txt"' and adifferentfile.txt does exist, the debugger will complain that somefile.txt doesn't exist. Even if I build the project for subsequent runs, I still get this exception thrown. Is this a known problem or is it just something wrong with my code? Thanks. -AJ [Edited by - u235 on May 3, 2006 6:36:31 PM]
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Advertisement
It's almost certainly your code. You are using that file name somewhere else in the program.

You can also use File.Exists() to help you avoid the exception.

Your debugger should be breaking when you hit the exception. If you look at the contents of the variable being passed you will probably see the wrong file name in there.
You need to make sure you are closing your file. Just like memory leaks there are resource leaks. My guess is you are leaking your file because you are not closing it, this will make it not avaliable by the later run.

theTroll
Try reading/writing to your file in with a "using" construct like this:

using (FileStream stream = new FileStream(fileName, FileMode.Read)){   //Do something with the file}//File is automatically closed for you


It's doing exactly what the other members have suggested (close the file), but will do it automatically at the end of the "using" block.
Well to start off, I was closing my file, sorry for not saying that. But as it is, that's not the problem. I took DaWanderer's suggesstion, but still to no avail. Here's the code I am using after making the changes:

//The function that takes the string parameterpublic void LoadHeightData(string fileName)        {            using (StreamReader sr = new StreamReader(fileName))            {                short x = Int16.Parse(sr.ReadLine());                short y = Int16.Parse(sr.ReadLine());                heightData = new int[x, y];                for (int i = 0; i < y; i++)                {                    for (int b = 0; b < x; b++)                    {                        int high = Int32.Parse(sr.ReadLine());                        heightData[x - 1 - b, y - 1 - i] = high;                    }                }                //FileInfo f = new FileInfo(fileName);                //f.Delete();                width = x;                height = y;            }        }//The function using the function from aboveprivate void Visualize_Load(object sender, EventArgs e)        {            terrain.InitializeDevice(this, cam, out device);            //No matter what file name I use here, it always complains that            //the first file name used doesn't exist. Unless, of course,            //that file does exist.            terrain.LoadHeightData(@"C:\anotherperlin.lot");            terrain.InitInput(this);            terrain.CameraPositioning(device, this);            terrain.VertexDeclaration(device);            terrain.IndicesDeclaration(device);        }


-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
You're sure you're not calling that function from elsewhere? Try putting a breakpoint (or a Console.WriteLine) on line 1 of LoadHeightData to find out what it's being passed, or whether it's being called twice.

There's nothing wrong with the code posted as far as I can see.
*nervous laugh* Hey guys. I figured out the problem. *another nervous laugh* I was editing the wrong source file. The code was just fine, although I do appreciate the suggesstion to use the using (FileStream...) statement to ensure the scope and everything. Thanks to all who helped me out. [smile]

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Hehe. Oh, that's so easy to do. (That, or editing the right file but from the wrong project so it doesn't get compiled.)

Glad you sorted it out :).

This topic is closed to new replies.

Advertisement