Sign in to follow this  

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

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 parameter
public 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 above
private 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
*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

Share this post


Link to post
Share on other sites

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this