C语言 - 使用文件IO,结构,函数,数组,指针在文本文件中记录数据(添加,删除)

时间:2013-08-24 18:11:16

标签: c file-io text-files

我还在学习C语言的介绍。目前我的代码有问题。任务是要求用户输入有关员工的信息并将其写入文本文件。问题发生在:

1)text / .exe文件中的输出显示随机字符。

2)新输出将覆盖text / .exe文件中的现有信息

3)第一次删除文本文件中的人员信息后,删除功能无法正确删除。

如果我能解决这3个问题中的任何一个,我会很高兴:)

代码如下:

#include <stdio.h>
#include <stdlib.h>

void Menu();
void New_Staff();
void Delete_Staff();
void Export_Profile();

struct element{
    char id[20];
    char name[20];
    char gender[20];
    char phone[20];
    char email[20];
}profile;

int main(void){      //The program will continue running until option 4 selected                                   
    int a;  

    for(a=0;;a++){
       Menu();
    }
    return 0;
}

void Menu()      //Main Menu to select option
{
    int n;
    printf("\n**********************************************");
    printf("\nMAIN MENU-STAFF INFORMATION SYSTEM");
    printf("\n**********************************************");
    printf("\n1. Add profile Staff Profile");
    printf("\n2. Delete Staff Profile");
    printf("\n3. Export All Profiles to 'output.txt'");
    printf("\n4. Exit");
    printf("\n**********************************************");
    printf("\nPlease enter your option< 1 / 2 / 3 / 4 >: ");
    scanf("%d", &n);

    switch(n){
    case 1:
        New_Staff();
        break;
    case 2:
        Delete_Staff();
        break;
    case 3:
        Export_Profile();
        break;
    case 4:
        printf("\n*** Thanks for using the program! Goodbye. ***");
        exit(0);
        break;
    default:
        printf("\nError! Wrong Number is Entered\nPlease Try Again");
        break;
    }
}

void New_Staff()                        //Add New Staff Profile 
{   
    int x;
    struct element profile;

    printf("\n===Add New Staff Profile===");
    printf("\n\nPlease enter the following staff information.");

    printf("\n\nStaff ID: ");
    scanf("%s", &profile.id);

    printf("Name\t: ");
    fflush(stdin);
    fgets(profile.name,20,stdin);

    for(x=0 ; x<20 ; x++)
    {
    if(profile.name[x] == '\n')
        profile.name[x] = '\0';
    }

    printf("Gender\t: ");
    scanf("%s", &profile.gender);

    printf("Phone\t: ");
    scanf("%s", &profile.phone);

    printf("Email\t: ");
    scanf("%s", &profile.email);

    printf("\nSYSTEM: New Staff Profile is Added Successfully.");

}

void Delete_Staff()         //Delete Staff Profile
{                   
    FILE *fRead, *fWrite;
    char *TextFile;
    char c;
    int Delete_Id, temp = 1;

    TextFile="output.txt";

    fRead = fopen(TextFile, "r");
    c = getc(fRead);

    while (c != EOF){
    printf("%c", c);
    c = getc(fRead); 
    }

    rewind(fRead);

    printf("\nDelete Staff with ID: ");
    scanf("%d", &Delete_Id);

    Delete_Id=Delete_Id+1;

    fWrite = fopen("copy.c", "w");
    c = getc(fRead);
    while (c != EOF) {
    c = getc(fRead);
    if (c == '\n')
    temp++;
    if (temp != Delete_Id){
    putc(c, fWrite); 
       } 
    }

    fclose(fRead);
    fclose(fWrite);

    remove(TextFile);

    rename("copy.c", TextFile);
    printf("\nThe contents of file after being modified are as follows:\n");

    fRead = fopen(TextFile, "r");
    c = getc(fRead);
    while (c != EOF) {
    printf("%c", c);
    c = getc(fRead);
    }

    fclose(fRead);
}

void Export_Profile()           //Export Staff Profile to Text
{       
    struct element profile;

    FILE *fPtr;
    fPtr=fopen("output.txt","w");
    FILE *fPtr1;
    fPtr1=fopen("output.txt","a+");

    if (fPtr == NULL)
    printf("Error in opening file\n");
    if (fPtr1 == NULL)
    printf("Error in opening file\n");

    fprintf(fPtr,"%10s %10s %10s %10s %10s","Staff","ID",
             "Name","Gender","Phone","Email");

    fprintf(fPtr1,"\n%10s %10s %10s %10s %10s", profile.id, profile.name,   
             profile.gender, profile.phone, profile.email);

    printf("\n%10s %10s %10s %10s %10s", "Staff", "ID", "Name",
             "Gender", "Phone",  "Email");
    printf("\n%10s %10s %10s %10s %10s", profile.id, 
             profile.name, profile.gender, profile.phone, profile.email);

    printf("\nSYSTEM: All staff profile have been exported to output.txt file");

    fclose(fPtr);
    fclose(fPtr1);
}

我很抱歉长代码。我不确定问题在哪里。

输出结果如下:

**********************************************
MAIN MENU-STAFF INFORMATION SYSTEM
**********************************************
1. Add profile Staff Profile
2. Delete Staff Profile
3. Export All Profiles to 'output.txt'
4. Exit
**********************************************
Please enter your option< 1 / 2 / 3 / 4 >: 1

===Add New Staff Profile===

Please enter the following staff information.

Staff ID: 1
Name    : Carmen Gray
Gender  : Female
Phone   : 123-4567890
Email   : carmen@live.com

SYSTEM: New Staff Profile is Added Successfully.
**********************************************
MAIN MENU-STAFF INFORMATION SYSTEM
**********************************************
1. Add profile Staff Profile
2. Delete Staff Profile
3. Export All Profiles to 'output.txt'
4. Exit
**********************************************
Please enter your option< 1 / 2 / 3 / 4 >: 3

 Staff         ID       Name     Gender      Phone
   °²#                @Γg╕∙         ∙
SYSTEM: All staff profile have been exported to output.txt file
**********************************************
MAIN MENU-STAFF INFORMATION SYSTEM
**********************************************
1. Add profile Staff Profile
2. Delete Staff Profile
3. Export All Profiles to 'output.txt'
4. Exit
**********************************************
Please enter your option< 1 / 2 / 3 / 4 >: 2

 Staff         ID       Name     Gender      Phone
   °²#                @Γg╕∙        .

Delete Staff with ID: 1

The contents of file after being modified are as follows:
 Staff         ID       Name     Gender      Phone

,文本文件显示:

   Staff         ID       Name     Gender      Phone
     øý#                @âg¸ù         ù           

4 个答案:

答案 0 :(得分:2)

Export_Profile()中,profile变量未设置为适当的值。

void Export_Profile()           //Export Staff Profile to Text
{       
    struct element profile; // this has not been set to actual value.
    ...
    fprintf(fPtr1,"\n%10s %10s %10s %10s %10s", profile.id, profile.name,   
         profile.gender, profile.phone, profile.email);
    ....
}

您可能希望将数据存储在某些全局变量或变量数组或链接列表中。在New_Profile()中设置它们并使用其他函数,例如Export_Profile()

答案 1 :(得分:2)

3)第一次删除文本文件中的人员信息后,删除功能无法正确删除。

您正在使用计数器变量temp作为ID,因此假设1-10中有10个ID,现在您删除了6.
下次致电Delete_Staff()时,请从temp=1开始。假设您要删除7,id 7现在位于第6行,但是您删除了第7行 您应该将文件中的staff_id与要删除的ID进行比较 您可以使用atoi函数将字符串转换为整数。

答案 2 :(得分:1)

以“w”模式打开文件会导致删除其先前的内容。如果您想在不删除之前内容的情况下进行书写,请使用“a”,“a +”或“r +”。

答案 3 :(得分:0)

你应该使用“fread”和“fwrite”函数来读/写文件。并且所有i / o都是在二进制模式下完成的理想选择,“rb +”/“wb +”,fwrite和fread函数可以更有效地处理数据。我喜欢制作这样的菜单驱动程序:)。 最好使用“while(1)”无限循环而不是“(for(a = 0 ;; a ++)”。