Vous êtes sur la page 1sur 19

Introduction to Common File Dialog Boxes

Because files on a computer can be stored in various places, Microsoft Windows provides various means of creating, locating, and managing files through objects named Windows Common Dialog Boxes. Indeed, these dialog boxes are part of the operating system and are equipped with all the necessary operations pertinent to their functionality. The common dialog boxes available in the MFC share a parent as the CCommonDialogclass. The CCommonDialog class is derived from CDialog:

The only member the CCommonDialog class has a constructor that takes a point toCWnd as argument.

C File Processing
Introduction
The C language provides very impressive support for file processing through a structure and many functions. The fundamental structure used to perform file processing in C is the FILE structure. In reality, to use this structure, you must call another function and get its return value.

Practical Learning: Introducing File Processing


1. Start Microsoft Visual Studio 2. To create a new application, on the main menu, click File -> New Project... 3. In the middle list, click MFC Application 4. Set the Name to PayrollPreparation1 5. Click OK 6. In the first page of the MFC Application Wizard, click Next 7. In the second page of the wizard, click Dialog Box 8. Click Next 9. In the third page of the wizard, change the Dialog Title to Payroll Preparation 10. Click Next twice

11. Click Finish 12. On the dialog box, click TODO: and press Delete 13. Press the OK button and press Delete 14. Click the Cancel button to select it 15. In the Properties window, click Caption, type Close and press Enter 16. Design the dialog box as follows:

Control Static Text Edit Control Static Text Edit Control Static Text Static Text Edit Control Static Text

Caption Employee #:

ID

Right Align Text

IDC_EMPLOYEENUMBER True Employee Name: IDC_EMPLOYEENAME ________________ Gross Pay: IDC_GROSSPAY Deductions True

Static Text Edit Control Static Text Edit Control Edit Control Static Text Static Text Edit Control Button Static Text Static Text Edit Control Static Text Static Text Edit Control Static Text Static Text Edit Control Button Button Button Button

Federal Income Tax: IDC_FEDERALTAX FICA Tax: IDC_FICATAX IDC_FICARATE % State Tax: IDC_STATETAX Calcuate _________________ Total Deductions: IDC_DEDUCTIONS _________________ Net Pay: IDC_NETPAY =============== File Name: IDC_FILENAME Open Reset Save Close IDC_OPEN IDC_RESET IDC_SAVE IDCANCEL True True IDC_CALCULATE True True True True

17. Right-click each edit control, click Add Variable, select the category as Value, set the options as follows and click Finish on each: Control ID IDC_EMPLOYEENAME IDC_GROSSPAY IDC_FEDERALTAX Category Variable Type Variable Name int CString double double m_EmployeeNumber m_EmployeeName m_GrossPay m_FederalIncomeTax Value Value Value

IDC_EMPLOYEENUMBER Value

IDC_FICATAX IDC_FICARATE IDC_STATETAX IDC_DEDUCTIONS IDC_NETPAY IDC_FILENAME

Value Value Value Value Value Value

CString CString double CString CString CString

m_FICATax m_FICARate m_StateTax m_TotalDeductions m_NetPay m_FileName

18. Access the source file of the dialog box and change the initializations in the constructor as follows: 19. CPayrollPreparation1Dlg::CPayrollPreparation1Dlg(CWnd* pParent /*=NULL*/) 20. : CDialogEx(CPayrollPreparation1Dlg::IDD, pParent) 21. , m_EmployeeNumber(0) 22. , m_EmployeeName(_T("")) 23. , m_GrossPay(0) 24. , m_FederalIncomeTax(0) 25. , m_FICATax(_T("0.00")) 26. , m_StateTax(0) 27. , m_FICARate(7.65) 28. , m_TotalDeductions(_T("0.00")) 29. , m_NetPay(_T("0.00")) 30. , m_FileName(_T("")) 31. { 32. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } 33. Return to the dialog box 34. Double-click the Calculate button 35. Implement the event as follows: 36. void CPayrollPreparation1Dlg::OnBnClickedCalculate() 37. { 38. UpdateData(TRUE); 39. 40. double GrossPay = m_GrossPay; 41. double FederalIncomeTax = m_FederalIncomeTax; 42. double FICARate = m_FICARate; 43. double FICATax = m_GrossPay * FICARate / 100; 44. double StateTax = m_StateTax; 45. double TotalDeductions = FederalIncomeTax + FICATax + StateTax; 46. double NetPay = GrossPay - TotalDeductions; 47. 48. m_FICATax.Format(L"%.2f", FICATax); 49. m_TotalDeductions.Format(L"%.2f", TotalDeductions); 50. m_NetPay.Format(L"%.2f", NetPay); 51. 52. UpdateData(FALSE); } 53. Return to the dialog box 54. Double-click the Reset button 55. Implement the event as follows: 56. void CPayrollPreparation1Dlg::OnBnClickedReset() 57. { 58. m_EmployeeNumber = 0; 59. m_EmployeeName = L""; 60. m_GrossPay = 0.00;

61. 62. 63. 64. 65. 66. 67. 68. 69. }

m_FederalIncomeTax = 0.00; m_FICATax = _T("0.00"); m_FICARate = 7.65; m_StateTax = 0.00; m_TotalDeductions = _T("0.00"); m_NetPay = _T("0.00"); m_FileName = L""; UpdateData(FALSE);

Opening a File
The primary operation of file processing consists of opening one. In the strict sense, this means that you are initiating file processing. This can be done in C by calling one of the fopen-variant functions. Their syntax are: FILE *fopen(const char *filename, const char *mode); FILE *_wfopen(const wchar_t *filename, const wchar_t *mode); To support Unicode, you should use the _wfopen() version. In both cases, the first argument specifies the name of the file you want to work on. If you pass just the name of the file, the compiler will consider the directory of the current project. Otherwise, you can provide the complete path of the project. The second argument specifies the type of operation you want to perform. It is passed as a string. When this function has been called, if it succeeds in carrying its operation (we will see what types of operations are available), it returns a FILE object. If the function fails, it returns NULL.

Creating a File
To create a file, you can pass the second argument of fopen withe one the following values: "w": This specifies that you want to create a new file and write one or more values to it. If the file exists already, the compiler will replace it "w+": This means that you want to create a new file to read and write one or more values to it. If the file exists already, its contents will be replaced

Writing Values to a File


The C language provides various functions you can use to write values to a file. Some functions deal with strings while others are supposed to handle any type of value. All functions that a FILE object as argument. This is the object that holds the file. To write a character to a FILE object, you can call one of the following functions: int fputc(int c, FILE *stream); wint_t fputwc(wchar_t c, FILE *stream); The first version of these functions takes a character as argument. The second takes a wide character. The character would be written to the file. To write a string to a file, you can call one of these functions: int fputs(const char *str, FILE *stream); int fputws(const wchar_t *str, FILE *stream);

To write other types of values to a file, you can call one of the following functions: size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream); To format a value before writting it to the file, you can call one of these functions: int fprintf(FILE *stream, const char *format [, argument ]...); int _fprintf_l(FILE *stream, const char *format, locale_t locale [, argument ]...); int fwprintf(FILE *stream, const wchar_t *format [, argument ]...); int _fwprintf_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...);

Closing a Stream
After using a FILE object, you should close it to free the resources it was using. This is done by calling the fclose() or _fcloseall() function. Their syntaxes are: int fclose(FILE *stream); int _fcloseall(void);

Practical Learning: Writing Values to a Stream


1. On the dialog box, double-click the Save button 2. Implement its event as follows: 3. void CPayrollPreparation1Dlg::OnBnClickedSave() 4. { 5. FILE *fstPayroll; 6. UpdateData(TRUE); 7. 8. if( m_EmployeeNumber == 0 ) 9. { 10. AfxMessageBox(L"You must enter an employee number."); 11. return; 12. } 13. 14. if( m_EmployeeName == L"" ) 15. { 16. AfxMessageBox(L"You must enter an employee name."); 17. return; 18. } 19. 20. if( m_FileName == L"" ) 21. { 22. AfxMessageBox(L"You must enter a name for the file."); 23. return; 24. } 25. 26. fstPayroll = _wfopen(m_FileName + L".txt", L"w+"); 27. 28. if( fstPayroll == NULL ) 29. AfxMessageBox(L"The file was not created"); 30. else

31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. }

{ fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fwprintf(fstPayroll, fclose(fstPayroll); } OnBnClickedReset(); L"%d\n", m_EmployeeNumber); L"%s\n", m_EmployeeName); L"%.2f\n", m_GrossPay); L"%.2f\n", m_FederalIncomeTax); L"%s\n", m_FICATax); L"%.2f\n", m_FICARate); L"%.2f\n", m_StateTax); L"%s\n", m_TotalDeductions); L"%s\n", m_NetPay);

46. Execute the application to test 47. Enter the employee number as 281548 48. Enter the employee name as Paul Martin Sutters 49. In the Gross Pay, enter 428.79 50. In the Federal Income Tax, enter 71.46 51. In the State Tax, enter 28.86

52. Click Calculate 53. Enter the file name as pms

54. Click Save 55. Close the dialog box and return to your programming environment

Reading Values From a File


in the same way, C provides various functions to read values from a file. Like their writing counterparts, function readers take a FILE object as argument. To read a character from a FILE object, you can call one of the following functions: int fgetc(FILE *stream); wint_t fgetwc(FILE *stream); The first version reads a regular character and the second reads a wide character. To read a string from a file, you can call one of these functions: char *fgets(char *str, int n, FILE *stream); wchar_t *fgetws(wchar_t *str, int n, FILE *stream); To read other types of values from a file, you can call one of the following functions: size_t fread(void *buffer, size_t size, size_t count, FILE *stream); To read values that were formatted, you can call one of these functions: int fscanf(FILE *stream, const char *format [, argument ]...); int _fscanf_l(FILE *stream,

int int int int int int

const char *format, locale_t locale [, argument ]...); fwscanf(FILE *stream, const wchar_t *format [, argument ]...); _fwscanf_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...); fscanf_s(FILE *stream, const char *format [, argument ]...); _fscanf_s_l(FILE *stream, const char *format, locale_t locale [, argument ]... ); fwscanf_s(FILE *stream, const wchar_t *format [, argument ]...); _fwscanf_s_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...);

Practical Learning: Reading Values From a Stream


1. On the dialog box, double-click the Open button 2. Implement its event as follows: 3. void CPayrollPreparation1Dlg::OnBnClickedOpen() 4. { 5. FILE *fstPayroll; 6. wchar_t EmployeeNumber[10]; 7. wchar_t EmployeeName[60]; 8. wchar_t GrossPay[10]; 9. wchar_t FederalIncomeTax[10]; 10. wchar_t FICATax[10]; 11. wchar_t FICARate[10]; 12. wchar_t StateTax[10]; 13. wchar_t TotalDeductions[10]; 14. wchar_t NetPay[10]; 15. 16. UpdateData(TRUE); 17. 18. if( m_FileName == L"" ) 19. { 20. AfxMessageBox(L"You must enter a name for the file."); 21. return; 22. } 23. 24. fstPayroll = _wfopen(m_FileName + L".txt", L"r+"); 25. 26. if( fstPayroll == NULL ) 27. AfxMessageBox(L"The file was not opened"); 28. else 29. { 30. fgetws(EmployeeNumber, 10, fstPayroll); 31. fgetws(EmployeeName, 60, fstPayroll); 32. fgetws(GrossPay, 10, fstPayroll); 33. fgetws(FederalIncomeTax, 10, fstPayroll); 34. fgetws(FICATax, 10, fstPayroll); 35. fgetws(FICARate, 10, fstPayroll); 36. fgetws(StateTax, 10, fstPayroll); 37. fgetws(TotalDeductions, 10, fstPayroll); 38. fgetws(NetPay, 10, fstPayroll); 39.

40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. }

m_EmployeeNumber m_EmployeeName m_GrossPay m_FederalIncomeTax m_FICATax m_FICARate m_StateTax m_TotalDeductions m_NetPay UpdateData(FALSE); fclose(fstPayroll); }

= _wtoi(EmployeeNumber); = EmployeeName; = _wtof(GrossPay); = _wtof(FederalIncomeTax); = FICATax; = _wtof(FICARate); = _wtof(StateTax); = TotalDeductions; = NetPay;

53. Execute the application to test it 54. Enter the file name you had previously saved (pms) 55. Click Open 56. Close the dialog box and return to your programming environment

The MFC library provides it own version of C's file processing. This is done through a class named CStdioFile. The CStdioFile class is derived from CFile. As done for the FILE structure, to start normal file input/output operations, declare a variable of type CStdioFile. This class has five constructors whose syntaxes are: CStdioFile(); CStdioFile(CAtlTransactionManager* pTM); CStdioFile(FILE* pOpenStream); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags, CAtlTransactionManager* pTM); The default constructor allows you to initiate file processing without giving much detail. If you use it, you can then call the Open() method that the CStdioFile class inherits fromCFile. Pass the same arguments you would for a CFile variable.

Practical Learning: Introducing MFC's Standard File Processing


1. To create a new application, on the main menu, click File -> New Project... 2. In the middle list, click MFC Application 3. Set the Name to LoanPreparation1 4. Click OK 5. In the first page of the MFC Application Wizard, click Next 6. In the second page of the wizard, click Dialog Box 7. Click Next 8. In the third page of the wizard, change the Dialog Title to Loan Preparation 9. Click Next twice 10. Click Finish 11. On the dialog box, click TODO: and press Delete 12. Press the OK button and press Delete 13. Click the Cancel button to select it 14. In the Properties window, click Caption, type Close and press Enter 15. Design the dialog box as follows:

Control Static Text Edit Control Static Text Edit Control Static Text Static Text Edit Control Static Text Edit Control Static Text Static Text Edit Control Static Text Button

Caption Prepared By:

ID

Right Align Text

IDC_CUSTOMERNAME Prepared For: IDC_EMPLOYEENAME ________________ Loan Amount: IDC_PRINCIPAL Interest Rate: IDC_INTERESTRATE % Paid In: IDC_PERIODS Months Evaluate IDC_EVALUATE True True True

Static Text Static Text Edit Control Static Text Edit Control Static Text Static Text Edit Control Button Static Text Edit Control Button Static Text Button Button

_________________ Future Value: IDC_FUTUREVALUE Monthly Payment: IDC_MONTHLYPAYMENT True _________________ Save As: IDC_FILESAVE Save File to Open: IDC_FILEOPEN Open File Name: Reset Close IDC_RESET_BTN IDCANCEL IDC_OPEN_BTN True IDC_SAVE_BTN True True

16. Right-click each edit control, click Add Variable, select the category as Value, set the options as follows and click Finish on each: Control ID IDC_CUSTOMERNAME IDC_EMPLOYEENAME IDC_PRINCIPAL IDC_INTERESTRATE IDC_PERIODS IDC_FUTUREVALUE IDC_FILESAVE IDC_FILEOPEN Category Variable Type Value Value Value Value Value Value Value Value CString CString CString CString CString CString CString CString CString Variable Name m_CustomerName m_EmployeeName m_Principal m_InterestRate m_Periods m_FutureValue m_MonthlyPayment m_FileSave m_FileOpen

IDC_MONTHLYPAYMENT Value

17. Access the source file of the dialog box and change the initializations in the constructor as follows: 18. CLoanPreparation1Dlg::CLoanPreparation1Dlg(CWnd* pParent /*=NULL*/) 19. : CDialogEx(CLoanPreparation1Dlg::IDD, pParent) 20. , m_CustomerName(_T("")) 21. , m_EmployeeName(_T("")) 22. , m_Principal(_T("0.00"))

23. 24. 25. 26. 27. 28. 29. { 30. }

, , , , , ,

m_InterestRate(_T("0.00")) m_Periods(_T("0")) m_FutureValue(_T("0.00")) m_MonthlyPayment(_T("0.00")) m_FileSave(_T("")) m_FileOpen(_T(""))

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

31. Return to the dialog box 32. Double-click the Evaluate button 33. Implement the event as follows: 34. void CLoanPreparation1Dlg::OnBnClickedEvaluate() 35. { 36. UpdateData(TRUE); 37. 38. double Principal = _wtof(m_Principal); 39. double InterestRate = _wtof(m_InterestRate) / 100; 40. double Periods = _wtof(m_Periods) / 12; 41. double InterestAmount = Principal * InterestRate * Periods; 42. double FutureValue = Principal + InterestAmount; 43. double MonthlyPayment = FutureValue / _wtof(m_Periods); 44. 45. m_FutureValue.Format(L"%.2f", FutureValue); 46. m_MonthlyPayment.Format(L"%.2f", MonthlyPayment); 47. 48. UpdateData(FALSE); } 49. Return to the dialog box 50. Double-click the Reset button 51. Implement the event as follows: 52. void CLoanPreparation1Dlg::OnBnClickedResetBtn() 53. { 54. m_CustomerName = L""; 55. m_EmployeeName = L""; 56. m_Principal = L"0.00"; 57. m_InterestRate = L"0.00"; 58. m_Periods = L"0.00"; 59. m_FutureValue = L"0.00"; 60. m_MonthlyPayment = L"0.00"; 61. m_FileSave = L""; 62. m_FileOpen = L""; 63. 64. UpdateData(FALSE); }

Writing to Standard Stream


Naturally the primary operation you would want to perform on a file consists of writing one or more values to it. To let you continually write a string without any concern for the end of line, you can use the Write() method that the CStdioFile gets from its parent. If you want the compiler to add a new line at the end of each written string, you can use theWriteString() method of the CStdioFile class. Its syntax is: virtual void WriteString(LPCTSTR lpsz);

Practical Learning: Writing Values to a Stream


1. On the dialog box, double-click the Save button 2. Implement its event as follows: 3. void CLoanPreparation1Dlg::OnBnClickedSaveBtn() 4. { 5. CStdioFile sioLoan; 6. UpdateData(TRUE); 7. 8. if( m_EmployeeName.GetLength() == 0 ) 9. { 10. AfxMessageBox(L"You must specify the name of the employee who prepared this loan."); 11. return; 12. } 13. 14. if( m_CustomerName.GetLength() == 0 ) 15. { 16. AfxMessageBox(L"You must specify the name of the customer for whom the loan was prepared."); 17. return; 18. } 19. 20. if( m_FileSave.GetLength() == 0 ) 21. { 22. AfxMessageBox(L"You must enter a name for the file to save."); 23. return; 24. } 25. 26. sioLoan.Open(m_FileSave + L".txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText); 27. 28. sioLoan.WriteString(m_EmployeeName); 29. sioLoan.WriteString(L"\n"); 30. sioLoan.WriteString(m_CustomerName); 31. sioLoan.WriteString(L"\n"); 32. sioLoan.WriteString(m_Principal); 33. sioLoan.WriteString(L"\n"); 34. sioLoan.WriteString(m_InterestRate); 35. sioLoan.WriteString(L"\n"); 36. sioLoan.WriteString(m_Periods); 37. sioLoan.WriteString(L"\n"); 38. sioLoan.WriteString(m_FutureValue); 39. sioLoan.WriteString(L"\n"); 40. sioLoan.WriteString(m_MonthlyPayment); 41. 42. sioLoan.Close(); 43. 44. OnBnClickedResetBtn(); 45. UpdateData(FALSE); } 46. Execute the application to test 47. Enter the Prepared By value as Ernestine Stanley 48. Enter the Prepared For name as Benjamin Greens 49. Enter the Loan Amount as 5000.00

50. Enter the Interest Rate as 12.25 51. Enter the Paid In value as 36

52. Click Evaluate 53. In the Save As edit control, type BG as the name of the file

54. Click Save 55. Close the dialog box and return to your programming environment

Reading From a Standard Stream


To read a continuous string from a stream, you can call the Read() method that the CStdioFileclass gets from its parent. If you have a document made of various paragraphs separated by new lines, to let you read each line, the CStdioFile class is equipped with the ReadString() method. It comes in two versions whose syntaxes are: virtual LPTSTR ReadString(LPTSTR lpsz, UINT nMax); virtual BOOL ReadString(CString& rString); The first version is used to write a C string to the file and you must specify the number of characters you are writing, as the second argument. The second version can be more convenient as it accepts a CString value.

Practical Learning: Reading Values From a Stream


1. On the dialog box, double-click the Open button 2. Implement its event as follows: 3. void CLoanPreparation1Dlg::OnBnClickedOpenBtn() 4. { 5. UpdateData(TRUE); 6. 7. if( m_FileOpen.GetLength() == 0 ) 8. { 9. AfxMessageBox(L"You must enter a name for the file to open."); 10. return; 11. } 12. 13. CStdioFile sioLoan; 14. sioLoan.Open(m_FileOpen + L".txt", CFile::modeRead | CFile::typeText); 15. 16. sioLoan.ReadString(m_EmployeeName); 17. sioLoan.ReadString(m_CustomerName); 18. sioLoan.ReadString(m_Principal); 19. sioLoan.ReadString(m_InterestRate); 20. sioLoan.ReadString(m_Periods); 21. sioLoan.ReadString(m_FutureValue); 22. sioLoan.ReadString(m_MonthlyPayment); 23. 24. UpdateData(FALSE); 25. sioLoan.Close(); } 26. Execute the application to test the dialog box 27. In the File to Open edit control, enter the name of the file you previously created (BG) and click Open 28. Close the dialog box and return to your programming environment

Letsface Tasks 13th August to 20th August, 2011

S No 1

Task Description { Venue adding Venue editing }

Kind of task formatting of page

Pre-requisite Location of all HTML pages

Pre-requisites identified 8mouths\admin.letsface.com\sys tems\webapp\modules\admin\te mplates PHP time(), date(), $time_output=date("d-m-Y H:i:s",$time_input);

2 { user statistics, check-in correct for month and per day. (check-in includes registrations) user statistics, and registrations correct for month and per day. (registration is first time check-in, new user) }

Correction and create a PHP function.

How these values are calculated and which pages we deliver them How OpenPNE delivers them onto the page

Monthly Enrolment: (a=page_user_analysis_date_m onth) Daily Enrolment: (a=page_user_analysis_date_da y) Monthly check-in: (a=page_checkin_analysis_date _month) Monthly Returns: (a=page_monthlyReturns) $time_output=date("d-m-Y H:i:s",$time_input); Update c_member SET r_date=$time_output

3 statistics - registration per month - start 2010 June (not 1970) 4 { -- add function so when you click on day, from morning to evening as they checkedin/registered. -- name, time, picture, which club they registered/checked in at (display club logo), number, anything relevant on that user. (later depending on your authority different information will be displayed) } { think about auto refresh, so i can keep logged in on stat page and view users checking in } Modify MySQL database Database field to modify

Create New function

Modify present HTML pages to deliver the content

AJAX

Modify present HTML pages to deliver the content

AJAX connection to monitor check-in time continuously with respect to current timestamp

Vous aimerez peut-être aussi