GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average.
  #21  
Old 27-Feb-2004, 22:07
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Standard I/O - Example #3 (Windows)

This example creates a binary file of structures. The application is a windows application. The screen will have five input boxes: name, social security number, age, and gpa. There is a save button on the screen. When the user fills in all input boxes and clicks the save button, the application places the data in a structure and writes the structure to a binary file. If the file has other structures in it, the new structure is appended at the end of the file.

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

#define NAMESIZE 10
#define SSNSIZE  11

typedef struct
{
  char  name[NAMESIZE+1];
  char  ssn[SSNSIZE+1];
  int   age;
  float gpa;
  float salary;
}PERSON;

void __fastcall StudentDataForm::SaveBtnClick(TObject *Sender)
{
  int    result;
  PERSON student;
  FILE  *binfile;

  // get the data from the Edit boxes.
  strcpy(student.name, Name->Text.c_str());
  strcpy(student.ssn, SSN->Text.c_str());
  student.age = atoi(Age->Text.c_str());
  student.gpa = atof(GPA->Text.c_str());
  student.salary = atof(Salary->Text.c_str());
  
  // open the file in binary append mode
  if ((binfile = fopen("c:\\data.dat","ab") == NULL)
    MessageBox(NULL,"Cannot open output file.",NULL,0);
  else
    if (fwrite(&student,sizeof(PERSON),1,binfile) != 1)
      MessageBox(NULL,"Cannot write data to the file.",NULL,0);

  fclose(binfile);
  return 0;
}
In this example we get the data from the edit boxes. We assume in this example that we have designed this program to be robust and that there can only be valid data in the edit boxes.

Once the data has been stored in the structure, we open the output file in append mode in binary format. If the opening of the file succeeded, we write (append) the structure to the file. We then close the file. If there is a problem either opening the file or writing to the file, we have an error window displayed.

Always remember that failing to test all file I/O will result in poor code, poor execution, and unhappy customers.

If you liked this tutorial, please give me a bump on my reputation. If not, just leave it alone ;-)

To discuss this tutorial: please follow the discussion here http://www.gidforums.com/t-2632.html.
Last edited by JdS : 09-May-2004 at 09:26. Reason: Split discussion into it's own thread
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Formatting C / C++ code WaltP C Programming Language 1 07-Jan-2008 00:59
Re: Naming Conventions WaltP C Programming Language 8 06-Jun-2004 23:22
New wi-fi standard approved - here comes 24-54Mbps jrobbio Open Discussion Forum 1 17-Jul-2003 01:29
[Tutorial] XSL Basics Pt. I pcxgamer Web Design Forum 15 22-Apr-2003 07:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.