C 如何替换文件中特定人员的字符串

C 如何替换文件中特定人员的字符串,c,string,file,C,String,File,我有点问题。我已将字符串“缺席”设置为文件中的所有学生。但是,当输入分配给学生的正确ID时,我想将“缺席”替换为“出席”。换言之,“缺席”只会在某一时间针对特定的人更改为“出席”。我不知道如何实现这一点,我恳请有人能帮忙。提前谢谢 更新::==== #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h&

我有点问题。我已将字符串“缺席”设置为文件中的所有学生。但是,当输入分配给学生的正确ID时,我想将“缺席”替换为“出席”。换言之,“缺席”只会在某一时间针对特定的人更改为“出席”。我不知道如何实现这一点,我恳请有人能帮忙。提前谢谢

更新::====

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>


typedef struct record {
  char *fname;
  char *lname;
  int code;
  char *stat;
} information;


int main (void) {
    int i;
  char ffname[28], flname[28], ans, status[15];

  int fID, j, id_, x;
  FILE *kfile, *ufile;
  x = 0;
  j = 0;
  i = 0;



  char buf[150];
  time_t curtime;
  struct tm* loc_time;
  information array[100];

  printf("          **********Attendance Recording System**********\n");
  printf("                                 MENU                     \n");
//Getting current time of system
  curtime = time (NULL);
// Converting current time to local time
  loc_time = localtime (&curtime);
  strftime (buf,150, "%I:%M %p.\n", loc_time);


//prints error message if file cannot be found within the system

    if ((kfile = fopen("information.txt", "r")) == NULL)    //If the file path is incorrect, an error message is displayed
    {
        fprintf(stderr, "Error while opening file  (%d: %s)\n",errno, strerror(errno)); //Error message that will be displayed if file path is incorrect

        return;
    }

    //while the file is opened and not at the end, the strings are stored into variables which forms an array of strings
     for (x = 0; x < 200; x++) {
      if (fscanf(kfile, "%s %s %d", ffname, flname, &fID) != 3)  //Reads the contents of the file
      break;
      array[x].fname = strdup(ffname);
      array[x].lname = strdup(flname);
      array[x].code = fID;
    }
   fclose(kfile);

   ufile= fopen("update.txt","w");
        strcpy(status, "Absent");
     fprintf(ufile,"First Name     Last Name     ID     Status     Time Arrived\n");
     for (i = 0; i < x; i++) {
     fprintf(ufile,"%-15s%-14s%2d%12s ",(array[i].fname), (array[i].lname), (array[i].code), status);
     fprintf(ufile,"%16s",(buf)); 

}
fclose(ufile);

while(j < x){
printf("Enter you ID: ");
scanf("%d", &id_);

strcpy(status, "Absent");
bool isPresentInFile = false;
for(i=0; i<x; i++)
{
    if(array[x].code == id_)
    {
        printf(" %s %s?", array[x].fname, array[x].lname);
        isPresentInFile  = true;
        break;
    }
}

if(isPresentInFile)
{
    strcpy(status, "present");
}

j++;
}

 fprintf(ufile,"First Name     Last Name     ID     Status     Time Arrived\n");
     for (i = 0; i < x; i++) {
     fprintf(ufile,"%-15s%-14s%2d%12s ",(array[i].fname), (array[i].lname), (array[i].code), status);
     fprintf(ufile,"%16s",(buf)); 

}
     fclose(ufile);


getch();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类型定义结构记录{
char*fname;
char*lname;
int代码;
字符*stat;
}信息;
内部主(空){
int i;
字符ffname[28],flname[28],ans,状态[15];
int fID,j,id_ux;
文件*kfile,*ufile;
x=0;
j=0;
i=0;
char-buf[150];
时间短了;
结构tm*定位时间;
信息阵列[100];
printf(“*************考勤记录系统*********\n”);
printf(“菜单\n”);
//获取系统的当前时间
curtime=时间(空);
//将当前时间转换为本地时间
loc_时间=本地时间(&curtime);
标准时间(buf,150,“%I:%M%p.\n”,定位时间);
//如果在系统中找不到文件,则打印错误消息
如果((kfile=fopen(“information.txt”,“r”))==NULL)//如果文件路径不正确,将显示错误消息
{
fprintf(stderr,“打开文件(%d:%s)时出错”,errno,strerror(errno));//如果文件路径不正确,将显示错误消息
返回;
}
//当文件被打开而不是结束时,字符串被存储到变量中,这些变量形成一个字符串数组
对于(x=0;x<200;x++){
if(fscanf(kfile,“%s%s%d”,ffname,flname,&fID)!=3)//读取文件的内容
打破
数组[x].fname=strdup(ffname);
数组[x].lname=strdup(flname);
数组[x]。代码=fID;
}
fclose(kfile);
ufile=fopen(“update.txt”,“w”);
strcpy(状态为“缺席”);
fprintf(ufile,“名姓氏ID状态时间到达\n”);
对于(i=0;i对于(i=0;i从您的问题中,我没有得到您希望在何时将
状态从
“缺席”
更改为
“出席”

但是您可以使用相同的
strcpy()
方法覆盖
状态变量

...
strcpy(status, "Absent");
if(//condition)
{
    strcpy(status, "present");
}
更新1:

阅读您的评论后,我了解到您将提示用户输入ID。如果该ID与文件中的ID匹配,则需要将
状态更新为
“present”
,然后写入新文件

我能想到这样的事情:

...
// read from file
.....
int ID;
printf("Enter you ID: ");
scanf("%d", &ID);

strcpy(status, "Absent");
bool isPresentInFile = false;
for(int i=0; i<x; ++i)
{
    if(array[x].code == ID)
    {
        isPresentInFile  = true;
        break;
    }
}

if(isPresentInFile)
{
    strcpy(status, "present");
}

...
// write to file
.....
  • 在结构
    信息
    中还有一个名为
    状态
    的字段。 默认情况下,为所有候选对象保留它“缺席”
  • 。 然后将其设置为仅针对那些输入ID的人“显示”
    。最后,将其写入文件


    我想在学生输入他们的id并与文件中的id匹配后更改它。因此,根据您刚才发送的代码片段,它会只覆盖该特定学生的“缺席”吗?还是会覆盖文件中的每个“缺席”?@SantoshA使用正确的id,我的意思是,程序会提示用户输入他们的id并进行搜索具有相同ID的文件。如果它与文件中的任何文件匹配,它将更改为“缺席”。@KKKKK:确保将其添加到问题中,以获得更具体的答案。@KKKKKK根据您所做的操作,我没有看到任何
    scanf
    请求用户输入其ID的文件。@GargAnkit谢谢您的回复,我现在就尝试一下,然后回来对你来说,正确的ID是什么意思?正确的ID有什么定义吗?值应该在特定的范围内吗?
    ...
    // read from file
    .....
    int ID;
    printf("Enter you ID: ");
    scanf("%d", &ID);
    
    strcpy(status, "Absent");
    bool isPresentInFile = false;
    for(int i=0; i<x; ++i)
    {
        if(array[x].code == ID)
        {
            isPresentInFile  = true;
            break;
        }
    }
    
    if(isPresentInFile)
    {
        strcpy(status, "present");
    }
    
    ...
    // write to file
    .....
    
    while(j < x)
    {
    printf("Enter you ID: ");
    scanf("%d", &id_);
    
    strcpy(status, "Absent");
    bool isPresentInFile = false;
    for(i=0; i<x; i++)
    {
        if(array[x].code == id_)
        {
            printf(" %s %s?", array[x].fname, array[x].lname);
            isPresentInFile  = true;
            break;
        }
    }
    
    if(isPresentInFile)
    {
        strcpy(status, "present");
    }
    
    // write to file here
    
    j++;
    }