Index
Would you like to react to this message? Create an account in a few clicks or log in to continue.

making and using a .txt file in c#?

5 posters

Go down

making and using a .txt file in c#? Empty making and using a .txt file in c#?

Post by Cloud Nine 25/11/2012, 9:41 pm

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
Cloud Nine
Cloud Nine
Tier 1 (Registered)
Tier 1 (Registered)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by Snow 26/11/2012, 1:37 am

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.
Snow
Snow
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by Jiri 26/11/2012, 2:21 pm

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

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
Jiri
Tier 3 (300 posts)
Tier 3 (300 posts)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by bs merchants 30/11/2012, 5:42 pm

C# allows C and C++ code as well, AFAIK. Perhaps try

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
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by Ben348 1/12/2012, 5:43 am

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:
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);
                }
            }
If you did wish to do it Jiris way then you would have to put the streamreader in a try block and call the Dispose in the finally block.
Ben348
Ben348
Tier 1 (Registered)
Tier 1 (Registered)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by bs merchants 1/12/2012, 10:20 am

Hmm, in which case disregard my previous post.
bs merchants
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by Ben348 1/12/2012, 10:29 am

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
Ben348
Tier 1 (Registered)
Tier 1 (Registered)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by bs merchants 1/12/2012, 2:02 pm

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.
bs merchants
bs merchants
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

making and using a .txt file in c#? Empty Re: making and using a .txt file in c#?

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum