【问题标题】:How replace a string of a specific person in a file如何替换文件中特定人的字符串
【发布时间】:2015-03-06 04:16:55
【问题描述】:

我有点问题。我已将字符串“缺席”设置为文件中的所有学生。但是,当输入分配给学生的正确 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;
}

更新 #3

#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;

} information;


int main (void) {
    int i;
  char ffname[28], flname[28], ans, ans1, 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);



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

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

        printf("Are you %s %s?", array[i].fname, array[i].lname);
        printf("\n");
        printf("[y/n only]: ");
        scanf(" %c", &ans);


        isPresentInFile  = true;
        break;
    }//end of if statement
}//end of for loop

if(isPresentInFile)
{
    strcpy(status, "present");
}//end of if statement
switch (ans){
    case 'y':
        ufile= fopen("update.txt","w");

     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)); 


}//end of for loop
fclose(ufile);
printf("Continue?\n");
printf("[y/n]: ");
scanf(" %c", &ans1);

if(ans1 == 'y'){
    break;
}//end of if statements
    else if (ans1 == 'n'){
        exit(EXIT_SUCCESS);
    }//end of else statement to check if ans1 is equal to 'n'

    case 'n':
        goto Next;
        break;
    default:
        printf("invalid entry. Try again");
}//end of switch case statememt
j++;
}//end of while





getch();
return 0;
}

【问题讨论】:

  • correct ID 是什么意思?是否有正确 ID 的定义? `值应该在特定范围内吗?

标签: c string file


【解决方案1】:

根据您的问题,我不知道您想将 status 从 "Absent" 更改为 "present" 的确切时间。

但是您可以使用相同的strcpy() 方法来覆盖您的status 变量。

...
strcpy(status, "Absent");
if(//condition)
{
    strcpy(status, "present");
}

更新 1:

阅读您的评论后,我了解到您会提示用户输入 ID。如果它与文件中存在的 ID 匹配,则需要将 status 更新为 "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
.....

更新 2: 阅读更新后的问题后,我注意到您最后正在写入文件!但是每次都会覆盖状态变量。这样,您最终会为文件中的所有条目写入 "Absent" 或 "Present"(请注意,它与最后输入其 ID 的候选人有关!)

为避免这种情况,请执行以下任一操作:

  1. 知道候选人的状态后立即写入文件。

    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++;
    }
    
  2. 在结构information 中还有一个名为status 的字段。 默认情况下,为所有候选人保留"Absent"。 然后将其设为"Present",仅适用于输入其 ID 的人。最后,将其写入文件。

【讨论】:

  • 我想在学生输入他们的 id 并且它与他们在文件中的 id 匹配后更改它。那么通过您刚刚发送的 sn-p,它是否只会覆盖该特定学生的“缺席”?还是会覆盖文件中的每个“缺席”?
  • @SantoshA 我的意思是正确的 ID,程序提示用户输入他们的 ID,它会在文件中搜索相同的 ID。如果它与文件中的任何内容匹配,它将将“缺席”更改为“存在”
  • @Kkkkkkkkkkkkk :确保将其添加到问题中以获得更具体的答案。
  • @Kkkkkkkkkkkkk 从你所做的来看,我没有看到任何scanf 要求用户提供他们的 ID。
  • @GargAnkit 感谢您的回复,我现在要试试这个并回复您
猜你喜欢
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 2021-02-14
  • 2014-06-21
  • 2022-06-16
  • 1970-01-01
相关资源
最近更新 更多