473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from and writing to a text file.

5 New Member
Hello all:
I'm pretty stumped on this one. I'm not looking for Code, I'm just trying to figure out the best way to start this since I am new to reading and writing from files. I can't figure out what section (main/btnOpen/frmExceptionsLoad) to put the code and all tutorials have either reading from or writing to and I am confused about putting it together along with the calculations of course.

I have to write a program that when I click a Button control, it will read five scores (separate lines) from a text file , then calculate the average of the scores and then write the average of those scores in a separate text file while also making sure my program handles IOExceptions. :|

I am completely lost. Again, I'm not looking for code, just a push in right direction so I can figure this out. Can anyone help?
Aug 1 '09 #1
4 3206
GaryTexmo
1,501 Recognized Expert Top Contributor
Well, for starters, take a look at the classes in the System.IO namespace. There are various stream readers/writers that will handle the file IO for you. If you get stuck, try a google search for "C# read write file" or something like that, there's tons of references.

For the general program flow, it depends on what you want to accomplish. It sounds like the event handler for the button click event is as good a place as any for your code. If you want to get more elaborate, you can have controls to display the scores and manipulate the data.

For example, you can have a button that uses an OpenFileDialog to get a file name, then loads the scores into a text or list box. Then you can have a button called operate (or something) which does the operations and updates the text box. Then another button called save, which does the obvious.

C# is like programming with Lego. I hope that gives you some ideas and a starting place. Have fun, and good luck! :)
Aug 1 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
As for some general tips on program structure... That's more a matter of someone's style. But in general one should try to keep methods some and have descrete reusable purposes, rather than one big function.

I'm not a fan of putting all your code in the actual MyButton_click handler. That makes it hard to call again from someplace else. At first it may seem more convoluted to do it this way, but in as your programs get longer, this can really save you.

Expand|Select|Wrap|Line Numbers
  1. #region Properties and variables
  2. List<int> MyScores;
  3. String FilePath;
  4. #endregion Properties and variable
  5.  
  6. #region Construction/Destruction methods
  7. // The new form method
  8. // The Form_Load method
  9. // Form closing method
  10. #region Construction/Destruction methods
  11.  
  12. MyButton_click(object sender, eventargs e)
  13. {
  14.     DoMyProcess(); // Go to a method for handling this
  15. }
  16.  
  17. DoMyProcess()
  18. {
  19.      DoStepOne();
  20.      DoStepTwo();
  21.      DoStepThree();
  22. }
  23.  
  24. DoStepOne()
  25. {
  26.      // Code for reading the file into a list.
  27. }
  28.  
  29. DoStepTwo()
  30. {
  31.     // Code to do  your calculations
  32. }
  33.  
  34. DoStepThree()
  35. {
  36.     // Code to update your results form
  37. }
  38.  
Aug 1 '09 #3
pbj2009
5 New Member
Thank you for the tips!
Aug 3 '09 #4
GaryTexmo
1,501 Recognized Expert Top Contributor
@tlhintoq
Tlhintoq makes a great point here... I'm somewhat embarrassed because I should have thought of that! To redeem myself, I'll throw out another tip to help you make your event handlers as generic as possible, to allow you to better reuse them.

As a part of the event handler, they give you the reference to the object that triggered the event itself, which is the parameter named sender. In wherever you handle this, you can cast sender to the type you're expecting, and if it's not null operate on it. A common thing I notice is that when people write event handlers, they access the member variable itself, instead of operating on sender, which means that handler is good for that object only.

Allow me to demonstrate with a quick little code snippet that should make my point pretty quickly. Make a new windows application and insert the following...

(Please note, I'm coding the controls via the constructor, not the designer, so you can better get the idea of what's going on)

Expand|Select|Wrap|Line Numbers
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.  
  5.             Button button1 = new Button();
  6.             button1.Text = "Button1";
  7.             button1.Location = new Point(8, 8);
  8.  
  9.             Button button2 = new Button();
  10.             button2.Text = "Button2";
  11.             button2.Location = new Point(button1.Location.X + button1.Size.Width + 8, 8);
  12.  
  13.             CheckBox checkbox1 = new CheckBox();
  14.             checkbox1.Text = "CheckBox1";
  15.             checkbox1.Location = new Point(8, button1.Location.Y + button1.Size.Height + 8);
  16.  
  17.             button1.Click += new EventHandler(GenericClickHandler);
  18.             button2.Click += new EventHandler(GenericClickHandler);
  19.             checkbox1.CheckedChanged += new EventHandler(GenericClickHandler);
  20.  
  21.             this.Controls.Add(button1);
  22.             this.Controls.Add(button2);
  23.             this.Controls.Add(checkbox1);
  24.         }
  25.  
  26.         void GenericClickHandler(object sender, EventArgs e)
  27.         {
  28.             Button senderButton = sender as Button;
  29.  
  30.             if (senderButton != null)
  31.             {
  32.                 MessageBox.Show("Button click event from: " + senderButton.Text);
  33.             }
  34.             else
  35.             {
  36.                 MessageBox.Show("Sender was not a button!");
  37.             }
  38.         }
  39.  
If you noticed, I threw in a checkbox and tied it to the same handler to show what happens when you get an object you're not expecting. I hope you found this informative :)
Aug 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1764
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
4
6396
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
4
9807
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
2
3054
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
1
1995
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
9
2743
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
6
5243
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
42
4821
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
2
4547
by: Clive Green | last post by:
Hello peeps, I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and writing MP3 tags). I have been using PHP on the command line (Mac OS X Unix shell, to be precise), and am...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.