C++ 如何书写'';列出的记录'';在文本文件中?

C++ 如何书写'';列出的记录'';在文本文件中?,c++,output,C++,Output,我正在做一个项目,我想知道如何将记录保存在文本文件中,我可以在第二部分(列出记录)中看到这些信息 #include///用于printf、scanf等输入输出函数 #包括 #包括 #包含///用于与windows相关的函数(不重要) #包含///字符串操作 /**全局变量列表*/ COORD COORD={0,0};///窗口左上角 /** 功能:gotoxy @参数输入:x和y坐标 @参数输出:将光标移动到控制台的指定位置 */ void gotoxy(整数x,整数y){ 坐标X=X;坐标Y=

我正在做一个项目,我想知道如何将记录保存在文本文件中,我可以在第二部分(列出记录)中看到这些信息

#include///用于printf、scanf等输入输出函数
#包括
#包括
#包含///用于与windows相关的函数(不重要)
#包含///字符串操作
/**全局变量列表*/
COORD COORD={0,0};///窗口左上角
/**
功能:gotoxy
@参数输入:x和y坐标
@参数输出:将光标移动到控制台的指定位置
*/
void gotoxy(整数x,整数y){
坐标X=X;坐标Y=Y;
设置控制台或位置(GetStdHandle(标准输出句柄),坐标);
}
/**主功能已启动*/
int main(){
流输出文件;
outputFile.open(“program3data.txt”);
FILE*fp,*ft;///文件指针
另一方面,选择;
/**表示员工的结构*/
结构emp{
char name[40];///员工姓名
int年龄;///员工年龄
浮动bs;///员工基本工资
};
struct emp e;///结构变量创建
char empname[40];///用于存储员工姓名的字符串
long int recsize;///员工每条记录的大小
/**以二进制读写模式打开文件
*如果文件EMP.DAT已经存在,那么它将以读写模式打开该文件
*如果文件没有退出,只需创建一个新副本
*/
fp=fopen(“EMP.DAT”、“rb+”);
如果(fp==NULL){
fp=fopen(“环境数据”、“wb+”);
如果(fp==NULL){
printf(“无法打开文件”);
出口(1);
}
}
///每个记录的大小,即结构变量e的大小
recsize=sizeof(e);
///无限循环一直持续到遇到break语句为止
而(1){
系统(“cls”);///清除控制台窗口
gotoxy(30,10);///将光标从左上角移动到位置30,10
printf(“1.添加记录”);///添加记录的选项
gotoxy(30,12);
printf(“2.列出记录”);///用于显示现有记录的选项
gotoxy(30,14);
printf(“3.修改记录”);///用于编辑记录的选项
gotoxy(30,16);
printf(“4.删除记录”);///用于删除记录的选项
gotoxy(30,18);
printf(“5.Exit”);///退出程序
gotoxy(30,20);
printf(“您的选择:”;///输入选项1、2、3、4、5
fflush(stdin);///刷新输入缓冲区
choice=getche();///从键盘获取输入
开关(选择){
案例“1”:///如果用户按1
系统(“cls”);
fseek(fp,0,SEEK_END);///搜索文件并将光标移动到文件末尾
///此处0表示从文件末尾移动0距离
另一个='y';
while(other=='y'){///如果用户想添加另一条记录
printf(“\n输入名称:”);
scanf(“%s”,e.name);
printf(“\n输入年龄:”);
scanf(“%d”和“e.age”);
printf(“\n输入基本工资:”);
scanf(“%f”和e.bs);
fwrite(&e,recsize,1,fp);///在文件中写入记录
printf(“\n添加另一条记录(是/否)”);
fflush(stdin);
另一个=getche();
}
打破
案例“2”:
系统(“cls”);
倒带(fp);///这会将文件光标移动到文件的开头
而(fread(&e,recsize,1,fp)==1){///读取文件并获取记录,每次获取一条记录
printf(“\n%s%d%.2f”,e.name,e.age,e.bs);///打印姓名、年龄和基本工资
}
getch();
打破
案例“3”:///如果用户按3,则编辑现有记录
系统(“cls”);
另一个='y';
而(另一个=='y'){
printf(“输入要修改的员工姓名:”);
scanf(“%s”,empname);
倒带(fp);
而(fread(&e,recsize,1,fp)==1){///从文件中获取所有记录
if(strcmp(e.name,empname)==0){///如果输入的名称与文件中的名称匹配
printf(“\n输入新名称、年龄和bs:”);
scanf(“%s%d%f”、e.name、e.age和e.bs);
fseek(fp,-recsize,SEEK_CUR);///将光标从当前位置向后移动1步
fwrite(&e,recsize,1,fp);///重写记录
打破
}
}
printf(“\n修改另一条记录(y/n)”);
fflush(stdin);
另一个=getche();
}
打破
案例“4”:
系统(“cls”);
另一个='y';
而(另一个=='y'){
printf(“\n输入要删除的员工姓名:”);
scanf(“%s”,empname);
ft=fopen(“Temp.dat”、“wb”);///为临时存储创建一个中间文件
倒带(fp);///将记录移动到文件的开头
而(fread(&e,recsize,1,fp)==1){///从文件中读取所有记录
if(strcmp(e.name,empname)!=0){///如果输入的记录匹配
fwrite(&e,recsize,1,ft);///将所有记录(要删除的记录除外)移动到临时文件
}
}
fclose(fp);
fclose(ft);
删除(“EMP.DAT”);///删除原始文件
重命名(“Temp.dat”、“EMP.dat”);///将临时文件重命名为原始文件名
fp=fopen(“EMP.DAT”、“rb+”);
printf(“删除另一条记录(是/否)”);
fflush(stdin);
另一个=getche();
}
打破
案例“5”:
fclose(fp);///关闭文件
退出(0);///退出程序
}
}
返回0;
}
#include <stdio.h> ///for input output functions like printf, scanf
#include <stdlib.h>
#include <conio.h>
#include <windows.h> ///for windows related functions (not important)
#include <string.h>  ///string operations

/** List of Global Variable */
COORD coord = {0,0}; /// top-left corner of window

/**
    function : gotoxy
    @param input: x and y coordinates
    @param output: moves the cursor in specified position of console
*/
void gotoxy(int x,int y){
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

/** Main function started */



int main(){
    ofstream outputFile;
    outputFile.open("program3data.txt");
    FILE *fp, *ft; /// file pointers
    char another, choice;

    /** structure that represent a employee */
    struct emp{
        char name[40]; ///name of employee
        int age; /// age of employee
        float bs; /// basic salary of employee
};

struct emp e; /// structure variable creation

char empname[40]; /// string to store name of the employee

long int recsize; /// size of each record of employee

/** open the file in binary read and write mode
* if the file EMP.DAT already exists then it open that file in read write mode
* if the file doesn't exit it simply create a new copy
*/
fp = fopen("EMP.DAT","rb+");
if(fp == NULL){
    fp = fopen("EMP.DAT","wb+");
    if(fp == NULL){
        printf("Connot open file");
        exit(1);
    }
}

/// sizeo of each record i.e. size of structure variable e
recsize = sizeof(e);

/// infinite loop continues untile the break statement encounter
while(1){
    system("cls"); ///clear the console window
    gotoxy(30,10); /// move the cursor to postion 30, 10 from top-left corner
    printf("1. Add Record"); /// option for add record
    gotoxy(30,12);
    printf("2. List Records"); /// option for showing existing record
    gotoxy(30,14);
    printf("3. Modify Records"); /// option for editing record
    gotoxy(30,16);
    printf("4. Delete Records"); /// option for deleting record
    gotoxy(30,18);
    printf("5. Exit"); /// exit from the program
    gotoxy(30,20);
    printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
    fflush(stdin); /// flush the input buffer
    choice  = getche(); /// get the input from keyboard
    switch(choice){
        case '1':  /// if user press 1
            system("cls");
            fseek(fp,0,SEEK_END); /// search the file and move cursor to end of the file
                                    /// here 0 indicates moving 0 distance from the end of the file

            another = 'y';
            while(another == 'y'){ /// if user want to add another record
                printf("\nEnter name: ");
                scanf("%s",e.name);
                printf("\nEnter age: ");
                scanf("%d", &e.age);
                printf("\nEnter basic salary: ");
                scanf("%f", &e.bs);

                fwrite(&e,recsize,1,fp); /// write the record in the file

                printf("\nAdd another record(y/n) ");
                fflush(stdin);
                another = getche();
            }
            break;
        case '2':
            system("cls");
            rewind(fp); ///this moves file cursor to start of the file
            while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
                printf("\n%s %d %.2f",e.name,e.age,e.bs); /// print the name, age and basic salary
            }
            getch();
            break;

        case '3':  /// if user press 3 then do editing existing record
            system("cls");
            another = 'y';
            while(another == 'y'){
                printf("Enter the employee name to modify: ");
                scanf("%s", empname);
                rewind(fp);
                while(fread(&e,recsize,1,fp)==1){ /// fetch all record from file
                    if(strcmp(e.name,empname) == 0){ ///if entered name matches with that in file
                        printf("\nEnter new name,age and bs: ");
                        scanf("%s%d%f",e.name,&e.age,&e.bs);
                        fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from current position
                        fwrite(&e,recsize,1,fp); /// override the record
                        break;
                    }
                }
                printf("\nModify another record(y/n)");
                fflush(stdin);
                another = getche();
            }
            break;
        case '4':
            system("cls");
            another = 'y';
            while(another == 'y'){
                printf("\nEnter name of employee to delete: ");
                scanf("%s",empname);
                ft = fopen("Temp.dat","wb");  /// create a intermediate file for temporary storage
                rewind(fp); /// move record to starting of file
                while(fread(&e,recsize,1,fp) == 1){ /// read all records from file
                    if(strcmp(e.name,empname) != 0){ /// if the entered record match
                        fwrite(&e,recsize,1,ft); /// move all records except the one that is to be deleted to temp file
                        }
                    }
                    fclose(fp);
                    fclose(ft);
                    remove("EMP.DAT"); /// remove the orginal file
                    rename("Temp.dat","EMP.DAT"); /// rename the temp file to original file name
                    fp = fopen("EMP.DAT", "rb+");
                    printf("Delete another record(y/n)");
                    fflush(stdin);
                    another = getche();
                }
                break;
            case '5':
                fclose(fp);  /// close the file
                exit(0); /// exit from the program
        }
    }
    return 0;
}
fwrite(&variable, 1, sizeof(record), file_pointer);
fread(&variable, 1, sizeof(record), file_pointer);
  fseek(file_pointer, (Y * sizeof(record)), SEEK_SET);