So I have tried looking up many tutorials and examples for creating, writing, and readin text files. I dont understand how to create a text file outside of my c# program folder because I always get errors. I want to create a text file to my desktop, write "I wrote in your file!" in it, and then read it and display it in a console window. Can I see some help or examples? Thanks!
static
void Main(string[] args){
FileStream file = new FileStream("C://Desktop/guess.txt", FileMode.CreateNew , FileAccess.Write); // write a line of text to the file StreamWriter sw = new StreamWriter(file);sw.Write(
"Hello file system world!");sw.Close();
}
markPosted Aug 12, 2007, 2:45 PM
This is my first time getting into hangman and reading files and also OOP. I just changed my program for better reading. The next thing I want to do is create a for loop.. like this
for(int i = 0; i < the number of characters in the string; ++i)
{
character[i] = '-';
}
So all the elements in character would have a value of a dash. Ill show whatever I have..
gameComponents.cs
public
class gameComponents{
public int wordLength; public char[] wordCharacters; public char[] guessedCharacters; public string wordPicked; public int wordCount; public bool play; public bool playing;}
Program.cs
FileStream
file = new FileStream("C:/Users/Marky/Desktop/HangmanWords.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); Random random = new Random(); //Assign all game components a valuegameComponents game =
new gameComponents();game.wordPicked =
"Not Picked Yet";game.wordLength = 0;
game.play =
true;game.playing =
true;game.wordCount = 0;
//Get a number to pick a random word in HangmanList.txtgame.wordCount = random.Next(1519);
//Go to the correct word in txt file and choose it for our word for (int i = 0; i < game.wordCount - 1; ++i){
game.wordPicked = sr.ReadLine();
}
//resize our array to the number of letters in the word Array.Resize(ref game.wordCharacters, game.wordPicked.Length); //give our char array values from the picked wordgame.wordCharacters = game.wordPicked.ToCharArray();
for (int i = 0; i < game.wordPicked.Length; ++i){
game.guessedCharacters[i] =
'-';}
//Opening text for game Console.WriteLine("Welcome to Hangman!"); Console.WriteLine("-------------------"); while (game.playing){
Console.WriteLine("Playing"); break;}
sr.Close();
Console.ReadKey();AlanPosted Aug 12, 2007, 4:57 AM
Your code is in fact doing exactly the same thing as mine (with numWords == 1519) except that you've used a 'for' statement and I used a 'while' statement. In fact, I like yours better ;)
To convert a string into a char array you can do this:
char[] chars = line.ToCharArray();
char first = chars[0];
char last = chars[chars.Length - 1];
If you don't need to change those chars, you can also use the string's indexer and not bother creating a char array:
char first = line[0];
char last = line[line.Length - 1];
markPosted Aug 11, 2007, 10:30 PM
Also, how to I rea one letter in the word? I am going to have a char array the size of the word, and each element of the array is going to hold each letter of the word. How would I go about doing so?
Thanks,
Mark
markPosted Aug 11, 2007, 7:34 PM
Can you look at the following piece of code and tell me if it's an "unprofessional" way to go. It works just fine.
string
line = ""; Random random = new Random(); FileStream file = new FileStream("C:/Users/Marky/Desktop/HangmanWords.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); int wordCount = random.Next(1519); for (int i = 0; i < wordCount - 1; ++i){
line = sr.ReadLine();
}
Console.WriteLine(line);Basically, its just getting a random number and the number is used for the line in the notepad file. So if the numbers 230, it picks the 229th word in the text file. It's simple for me to use, but do you think I SHOULD use a different way?
AlanPosted Aug 11, 2007, 5:53 PM
I'm assuming that each word is on a separate line in the file.
If you don't know in advance how many words there are in the text file, then you'll need to read the file first and store the words in a List:
static Random rand = new Random(); // it's best to use a static field for this
StreamReader sr = new StreamReader(file);
List words = new List();
string line = null;
while((line = sr.ReadLine()) != null)
{
words.Add(line);
}
sr.Close();
string randomWord = words[rand.Next(0, words.Count)];
If you do know how many words are in the file (numWords say), then you could do this:
StreamReader sr = new StreamReader(file);
int randomLine = rand.Next(0, numWords);
string line = null;
string randomWord = null;
int count = 0;
while((line = sr.ReadLine()) != null)
{
if (count == randomLine)
{
randomWord = line;
break;
}
count++;
}
sr.Close();
markPosted Aug 11, 2007, 4:34 PM
Well what if I want to create a hangman game. I am going to have a wordlist in a text file, randonmly choose a word, and play. How could I choose a certain line?
Mark
AlanPosted Aug 11, 2007, 2:06 PM
This code will read, and then write to the console, each line in the file. The end of the file has been reached when 'line' returns null:
StreamReader sr = new StreamReader(file);
string line = null;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr.Close();
To write out only certain lines, you would need to keep a count of the lines read until you get to the ones you want to display.
markPosted Aug 11, 2007, 1:42 PM
You were correct. Thanks.
Now for my next question, how do I read certain lines from a text file? I can only read one. Here's the code.
FileStream
file = new FileStream("C:/Users/Marky/Desktop/list.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); string line = sr.ReadLine(); Console.WriteLine(line);sr.Close();
Munir ShaikhPosted Aug 11, 2007, 5:40 AM
Initially it seems that to save on desktop folder your should change the path which will look something like "C:\Documents and Settings\All Users\Desktop\FolderName"
& you should have permission to read, write to particular folder
Regards,
>>Munnamax