making and using a .txt file in c#?
5 posters
Page 1 of 1
making and using a .txt file in c#?
I am learning C# by myself and the way i am learning it is basically, me trying to make a program and making thinking of ways to make it more complex which requires me to research the things i may need to do.
The program i am doing at the moment is a simple game which works to how i expected it to, the things i wanted to do is make it so that the program makes a .txt file and then store the high scores in it and then read each entry line by line if the user wants to.
Also i want it to be able to remember a username and password added at the beginning which the user makes up (its not that usefull but i am using it for practice). i want it to remember multiple usernames and passwords too.
Thanks anyone who could help
Cloud Nine
The program i am doing at the moment is a simple game which works to how i expected it to, the things i wanted to do is make it so that the program makes a .txt file and then store the high scores in it and then read each entry line by line if the user wants to.
Also i want it to be able to remember a username and password added at the beginning which the user makes up (its not that usefull but i am using it for practice). i want it to remember multiple usernames and passwords too.
Thanks anyone who could help
Cloud Nine
Cloud Nine- Tier 1 (Registered)
Re: making and using a .txt file in c#?
I assure you that, if you google that, you will find MORE than enough answers.
I only say it because I've been down the path with quite a few languages.
stackoverflow is your friend.
I only say it because I've been down the path with quite a few languages.
stackoverflow is your friend.
Snow- Grandmaster (2000 posts)
Re: making and using a .txt file in c#?
Like Snow said above, stackoverflow is a programmer's friend.
Anyways, you are looking to work with the classes StreamReader and StreamWriter
Here is a very easy example on how to put a textbox value into a .txt file
This should get you started and you should understand the basics now, if you need help on searching through a text file for something, feel free to pm me.
Anyways, you are looking to work with the classes StreamReader and StreamWriter
Here is a very easy example on how to put a textbox value into a .txt file
- Code:
private void readButton_Click(object sender, EventArgs e)
{
string sourcePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string myFile = Path.Combine(sourcePath, "myfile.txt");
StreamReader inputStream = File.OpenText(myFile);
string line = inputStream.ReadLine();
while (line != null)
{
fileTextBox.AppendText(line);
fileTextBox.AppendText(Environment.NewLine);
line = inputStream.ReadLine();
}
inputStream.Close();
}
This should get you started and you should understand the basics now, if you need help on searching through a text file for something, feel free to pm me.
Jiri- Tier 3 (300 posts)
Re: making and using a .txt file in c#?
C# allows C and C++ code as well, AFAIK. Perhaps try
This will create a textfile, or overwrite it if it already exists (losing any stored data previously) and open it for writing. Then address it by
I'm sure there are many awesomely better OO methods for this in C#, but I only know C but that code should work.
- Code:
FILE* MyFile = fopen("./mytxt.txt", w);
This will create a textfile, or overwrite it if it already exists (losing any stored data previously) and open it for writing. Then address it by
- Code:
fprintf(MyFile, StrToPrint, [argv]);
I'm sure there are many awesomely better OO methods for this in C#, but I only know C but that code should work.
bs merchants- Forum Fanatic (1000 posts)
Re: making and using a .txt file in c#?
The C# compiler will not allow for C or C++ code be compiled in it, they are all seperate languages which is why you should generally only use the compiler built for that language to compile it. If you did wish to go down this route the only way would be to create a Dynamic-link library and use either PInvoke or COM interop (two seperate interop techniques) and call the methods that way. Honestly I don't know why anyone would want to go down that route for something as simple as this so I'd advise to you not to do that.
The code above which Jiri postes is correct and what you need, however there are a few points I'd like to point out.
For any kind of unmanaged resources they must impliment the IDisposable interface. As a rule when you use an IDisposable object you should instantiate it in a using statement. This ensures that the Dispose is called even if an exception occurs.
So here is the modified code:
The code above which Jiri postes is correct and what you need, however there are a few points I'd like to point out.
For any kind of unmanaged resources they must impliment the IDisposable interface. As a rule when you use an IDisposable object you should instantiate it in a using statement. This ensures that the Dispose is called even if an exception occurs.
So here is the modified code:
- Code:
string line;
using (StreamReader inputStream = File.OpenText(@"C:\Users\Ben\Desktop\file.txt"))
{
while ((line = inputStream.ReadLine()) != null)
{
fileTextBox.AppendText(line);
fileTextBox.AppendText(Environment.NewLine);
}
}
Ben348- Tier 1 (Registered)
Re: making and using a .txt file in c#?
Hmm, in which case disregard my previous post.
bs merchants- Forum Fanatic (1000 posts)
Re: making and using a .txt file in c#?
If I were doing this I would probably go for C or C++, for something this simple and small not much point having the large .NET overhead with it. Having saying that, for a beginner it will be a lot easier to do in C# (Although you should start learning with C in my opinion - it will set you up better for the future).
Ben348- Tier 1 (Registered)
Re: making and using a .txt file in c#?
Learning C is never a mistake, I would definitely recommend it. It also gives you a huge understanding and appreciation for everything else you do with regards to programming, independent of language.
I second what Ben said, playing around with text files is easy enough in a procedural language, it doesn't require the huge OO approach.
I second what Ben said, playing around with text files is easy enough in a procedural language, it doesn't require the huge OO approach.
bs merchants- Forum Fanatic (1000 posts)
Similar topics
» Adding an insert sound file/mp3 button to posts
» Making of: Cille
» Money Making on 07'
» Making Mil's Vouches
» KEC money making guide
» Making of: Cille
» Money Making on 07'
» Making Mil's Vouches
» KEC money making guide
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum