Jump to content

  • Log In with Google      Sign In   
  • Create Account

Accessing a text file from resource folder in MS visual studio.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 demongunsman   Members   -  Reputation: 136

Like
0Likes
Like

Posted 13 February 2013 - 05:23 AM

Ive got this text file Im trying to read from. Its located in my resources folder in microsoft visual studio. Im trying to access that text file so I can parse the text from that file to an integer and store it in my integer array called Armor[,] .The contents of the text file is a 12x11 chart of numbers which I would like to parse to an integer and store into the 2D Armor[,] array. Here are the contents of the text file:

 

http://pastebin.com/Nq4XFCYM

 

When I run the program I get an error saying:

Value cannot be null.
Parameter name: path

 

The error that microsoft visual studio's throwing is because of the line:

 

          

StreamReader armorStream = new StreamReader(Resources.Armor);

 

//namespace used do directly access Resource.FileName
using TurfBattles.Properties;
 
public void LoadArmor()

{

//Create a variable for storing the armor defense values

int[,] Armor = new int[12, 11];
 
//Create a string variable for storing the stream readers input
string myString = new string();


int Row = 0;


//Create a stream reader for reading the Armor.txt file

StreamReader armorStream = new StreamReader(Resources.Armor);


//While the stream reader is able to read the next line

//do this:

while ((myString = armorStream.ReadLine()) != null)

{

//Loop through each value of the myStringSplit array

for (int Column = 0; Column < 11; Column++)

{

//This splits the text up and stores it into an array of strings

//It also stores the white spaces into a character array

string[] myStringSplit = myString.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);


//Store the current value of myStringSplit into the Armor Array

Armor[Row, Column] = int.Parse(myStringSplit[Column]);

}

Console.WriteLine();


Row++;

}

armorStream.Close();


Console.ReadKey();

}


Edited by demongunsman, 13 February 2013 - 05:45 AM.


Sponsor:

#2 Ravnock   Members   -  Reputation: 294

Like
0Likes
Like

Posted 13 February 2013 - 05:32 AM

Could we see the text file content?



#3 demongunsman   Members   -  Reputation: 136

Like
0Likes
Like

Posted 13 February 2013 - 05:36 AM

http://pastebin.com/Nq4XFCYM



#4 Ravnock   Members   -  Reputation: 294

Like
0Likes
Like

Posted 13 February 2013 - 07:57 AM

http://pastebin.com/Nq4XFCYM

 

It seems ReadLine is expecting a '\r' or '\n', do you have it in your file? If the \r or \n isn't found and it reaches the eof, a null is returned, this fits with your issue...

 

From http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

 

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached.



#5 demongunsman   Members   -  Reputation: 136

Like
0Likes
Like

Posted 13 February 2013 - 12:54 PM

When I run the program I get an error saying:

Value cannot be null.
Parameter name: path

 

The error that microsoft visual studio's throwing is because of the line:

 

          

StreamReader armorStream = new StreamReader(Resources.Armor);

 

The problem is that I am trying to access a file, but the arguments within the parameters of the stream reader takes a file path.



#6 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 14 February 2013 - 02:34 PM

When that error is thrown, what's the value of "Resources.Armor"?  VS IDE should let you hover over to check.


Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS