• Advertisement
Sign in to follow this  

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

This topic is 1804 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

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:

 

           [source]StreamReader armorStream = new StreamReader(Resources.Armor);[/source]

 

[source]

//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();
}
[/source]

Edited by demongunsman

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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:

 

           [source]StreamReader armorStream = new StreamReader(Resources.Armor);[/source]

 

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement