将用户条目从类写入.txt文件

时间:2016-04-13 23:40:07

标签: c++

好的,我花了最后8个小时,阅读书籍和无数线程,但无法找到/理解解决方案。 我需要创建一个有2个班级的大学图书馆。会员和书。所有的花花公子,我都成功了。我进行了设置,以便您输入名字,姓氏和班级组(很快就会有一个年龄,当我开始工作时)。一旦全部进入系统,系统会将其读回给您,然后您输入另一个成员或返回/退出应用程序。现在差不多凌晨1点,我很感激如何将用户条目写入.txt文件 我有它去.txt文件,但它是垃圾(即:PÿPÿcmd.d{/‡P |ÿPÿcmd.exe  PP.exe)

任何有关解释的帮助都将是一个巨大的帮助!

 // include files - library
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h> 
using namespace std;
// Declare variables
int iCounter = 0; // Starting at 0 
char sBlank[2]= " "; // Puts a blank space
char iOpt; // Options for the main menu
char start(); // function to load the menu
char write(); // Append the database
char read(); // Display the content from the database.txt file
char create(); // Creates a new database  (overwrites database.txt)
int close(); // For exiting the program

struct NODE {
    char sFirstName[8]; // Allows a first name upto 8 characters
    char sLastName[12]; // Allows a last name upto 12 characters
    char iBirth[3]; // Age value
    char sClass[6];
}; // End of structure

struct NODE *pRecord; // Pointer notation

char sLine[49+1]; // 1-D table
char sTable[49+1][49+1]; // 2-D table

class STUDENT 
{
private: // Private Data Member
string sFirst;
string sLast;
string sGroup;
int iBirth;
public: // Public Member Functions

STUDENT();  // constructor and destructor fns.
~STUDENT() { } // could and often would include code in the destructor
char iSrnClr(void);
char iStudentStart(void);
char sStudentGet(void);
char iMarkReport(void);

}; // basic class outline - member function definitions follow

STUDENT::STUDENT()
{
// To Do list
string sFirst = "Default";
string sLast = "Name";
string sGroup = "ITN6";
int iBirth = 18;
} // Constructor - Initilises data

// Clear Screen member function
char STUDENT::iSrnClr()
{
// To Do list - these functions are non-standard [TurboC], replace them with shell commands...
system("COLOR 79");
system("CLS");
puts("\n \t \t College Library System\n");

return(0);
} // end of iScrClr() STUDENT class member function


// Initilize Student Title Page 
char STUDENT::iStudentStart() 
{
// To Do List
cout << "\n \t\t STUDENT PAGE \n\n"<< endl;
return(0);
} // end of iStudentStart STUDENT class member function


char STUDENT::sStudentGet() //iMarkGet() member function of STUDENT CLASS
{
// To Do List
char sFirstName[12]; //Initlize input buffer
char sLastName[12]; //Initlize input buffer
char sClass[6];

    // Initialise memory space
    pRecord = (struct NODE *) malloc(sizeof(struct NODE));
    FILE * fDatabase; // Write to file

    // Setting up the screen
    system("color 79");
    system("cls");
    fDatabase = fopen ("students.txt","w"); // Writes to the file 'Database.txt'

    for (iCounter = 0; iCounter < 1; iCounter++)
    {
        cout << "\n\tPlease enter the first name" << endl;
        cin >> sFirstName;
        sFirst = sFirstName;
        cout << "\n\tPlease enter the Last Name" << endl;
        cin >> sLastName;
        sLast = sLastName;
        cout << "\n\tPlease enter the group" << endl;
        cin >> sClass;
        sGroup = sClass;

        strcat(sLine,pRecord->sFirstName); // Prints clients First name
        strcat(sLine,pRecord->sLastName); // Prints clients Last name
        strcat(sLine,pRecord->sClass); // Prints clients Age
        fprintf(fDatabase, "\n \t %s \n", sLine); // Prints each string and creates in 'database.txt' file also
        strcpy(sTable[iCounter],sLine);
        strcpy(sLine,sBlank);
        } // end loop

fclose(fDatabase); // Close the 'database.txt' file to stop writing
} // end of iMarkGet() member function of STUDENT class


char STUDENT::iMarkReport()
{
cout << "Students First Name is: " << sFirst << endl; 
cout << "Their Last Name is: " << sLast << endl;
cout << "They are in class group: "<< sGroup <<endl;
return(0);
}

int main(void)
{
    // Setting up the screen 
    system("color 79"); // Grey background, blue font

    const char password[15] = "login"; // const will not allow the password to change. The password is "login"
    char pass[15]; //the users enters a password, upto 15 characters
    char h; // h is a placeholder
    int position = 0; // position is the position of the password (i.e. how many characters where entered) 

    printf("\t Patrick Lowe - Mobile Phone Network Client List - ITN5 - January \n \n");
    printf("%s", " \t \t Password: ");
    do { // function to gather user input while also hidding printed characters with an *
        h = getch();

        if( isprint(h) ) // isprint makes sure the character can be printed
        {
            pass[ position++ ] = h;
            printf("%c", '*'); // everytime a character is entered it is replaced or 'hidden' behind and asterisk
        }

        else if( h == 8 && position ) // everytime a character is deleted, it removes the entry and also the asterisk
        {
            pass[ position-- ] = '\0'; 
            printf("%s", "\b \b");
        }
    } while( h != 13 );

    if(strcmp(pass, password) != 0) // this will compare the correct password and the one entered, if incorrect do the following
    {
        printf("\n \n Invalid password!\n \n Abort! \n \n");
    }

    else // otherwise, if correct do this
    {
        printf("\n \n Access Granted! \n \n");
        getch(); // press enter to continue
        system("cls"); // clear the screen
        start(); // load the start function (begin the database management)
    }

    getchar(); // if the password is invalid, press a key to continue
    return 0; // For safe exit
}


char start(void)
{
    do
{   
    // Prompt for and capture the data
    printf("\t Patrick Lowe - College Library Manager - ITN6 - January");
    printf("\n \n Please Select an option: \n");
    printf("\t 1. Create a new Member \n");
    printf("\t 2. Add a new client \n");
    printf("\t 3. View current clients \n");
    printf("\t 4. Exit \n");
    printf("\n Database Function: \t ");
    scanf("\n %c", &iOpt); // Captures users option

    switch(iOpt)
{
    // Process the data

        case '1':       // Creating a new client list
            create();   // Load function to create a new client list
            break;

        case '2':       // Adding a new client
            write();    // load function to append current client list
            break;

        case '3':       // Read or view current client list
            read();     // Load function to view the client list
            break;

        case '4':       // Exit the program
            close();    // Load function to exit program
            break;

        default:
            printf("\n \t Invalid Entry!");
} // End of Option Switch
    getch();

    // Generate options menu 
    printf("\n \n Do you wish to continue(Y/N):\t ");
    scanf("\n %c", &iOpt);  

    system("cls");
    printf("Thank you for using my database \n \n \t Patrick Lowe \n \n Press any key to Continue");
    getch();
    system("cls");
}   // End of Do
    while(iOpt == 'y');

} // End of Main


char write() // Append a database file
{   } // ENd of Write

char create() // Creates a new database (Overwrites the database.txt file)
{   
// declare variables
STUDENT PAT; // object instance of STUDENT class
char cReply = 'y' ;

while ( cReply == 'y' || cReply == 'Y' ) // into the loop if true
{
PAT.iSrnClr(); // Clear Screen
PAT.iStudentStart(); // Initialize Page
PAT.sStudentGet(); // Capture Data
PAT.iSrnClr(); // Clear Screen
PAT.iMarkReport(); // Output Multiplication Table
// exit routine - Prompt User for another input
cout << "Do you want another Input? (y or n)" ;
cin >> cReply ;
} // end while

exit(0);
} // End of Create

char read()  // Reads a database file
{
        FILE *fDatabase; // Write to file
        char buf[260]; // Prints upto 260 characters

        fDatabase = fopen ("database.txt", "r"); // Opens the database.txt file to be read
        if(!fDatabase)
            return 0;
        while (fgets(buf,260, fDatabase)!=NULL)
                printf("\n \t %s",buf); // Prints the database file with a limit of 260 characters
                printf("\n Press Any Key To Continue \n");

        fclose(fDatabase); // Closes the database.txt file
        return 0;
} // End of Read

int close() // for exiting the program
{
    printf("\n \t Thank you! \n");
    exit(0); //closes the program
}

0 个答案:

没有答案
相关问题