在C中删除和重命名2个文本文件时出现分割错误

ilmyapht  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(109)

所有文件都位于同一个目录中。当我第二次尝试删除和重命名records.txttemp.txt->records.txt时,我遇到了分段错误。
我在使用deleteThirdLineWithNumber之前和之后都试过使用close(),没有任何区别。
这就是问题所在:
Spring也在敲着“喝我的朋友”葡萄园的门。众所周知,葡萄喜欢照料和工作,但最重要的是它们喜欢劳动,而劳动通常会带来好收成。葡萄园需要很多帮手,因此正在为那些想申请工作的人准备一份申请。
申请邀请那些可以在 Spring 工作的人。申请时,他们应该给予他们的名字以及他们可以在一周中的哪一天上班。日期如下:星期一星期三星期四。候选人将用空格分隔日子。农场知道哪天需要多少工人(星期一,星期二等)。如果一天已经满了,申请不应该接受那天的申请人。
申请人的数据将被存储在一个文件中,除了数据输入,我们应该有可能修改,删除和创建一个申请人的地区或完整的申请人名单。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
    
enum days_of_week {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};
void deleteThirdLineWithNumber(const char* filename) {
    FILE* fp = fopen(filename, "r+");
    if (fp == NULL) {
        printf("Error opening file\n");
        return;
    }
    char buffer[100];
    int lineNumber = 1;
    int numberFound = 0;
    
    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
        if (lineNumber == 3 && isdigit(buffer[0])) {
            numberFound = 1;
            break;
        }
        lineNumber++;
    }
    
    if (numberFound) {
        fseek(fp, 0, SEEK_SET);
        int currentLine = 1;
        FILE* tmp = fopen("temp2.txt", "w");
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
            if (currentLine != 3) {
                fprintf(tmp, "%s", buffer);
            }
            currentLine++;
        }
        fclose(fp);
        fclose(tmp);
        remove(filename);
        rename("temp2.txt", filename);
    }
    else {
        printf("No number found on third line\n");
        fclose(fp);
    }
}

void addApplicant(char *name, char *day, int week,char *filename) {
    int weekIndex = 0;
    if(strcmp(day, "MONDAY") == 0){
        weekIndex = week*7 + MONDAY;
    }
    else if(strcmp(day, "TUESDAY") == 0){
        weekIndex = week*7 + TUESDAY;
    }
    else if(strcmp(day, "WEDNESDAY") == 0){
        weekIndex = week*7 +WEDNESDAY;
    }
    else if(strcmp(day, "THURSDAY") == 0){
        weekIndex = week*7 + THURSDAY;
    }
    else if(strcmp(day, "FRIDAY") == 0){
        weekIndex = week*7+FRIDAY;
    }
    else if(strcmp(day, "SATURDAY") == 0){
        weekIndex = week*7+THURSDAY;
    }
    else if(strcmp(day, "SUNDAY") == 0){
        weekIndex = SUNDAY  + 7*week;
    }
    if (weekIndex < 0 || weekIndex >= 84) {
        printf("Sorry, %s, week %d does not exist.\n", day, weekIndex);
        return;
    }
    FILE *fp,*temp;
    fp = fopen(filename, "r");
    temp = fopen("temp.txt", "w+");
    if (fp == NULL && temp ==NULL) {
        printf("Error opening file\n");
        return;
    }
    char buffer[100];
    
    for (int i = 0; i < 84*3; i++)
    {
        
         
        if(i % 3 == 0)
        {
            fprintf(temp,"%s",fgets(buffer, sizeof(buffer), fp));
            fgets(buffer, sizeof(buffer), fp);
              
        }
        else if ( i % 3 == 1)
        {
            int numberOfHelpers = atoi(buffer);
            char ch[100];
            if (i == (weekIndex*3 +1))
            {
                if (numberOfHelpers <= 0) {
                    printf("Sorry, the maximum number of helpers has been reached for %s, week %d.\n", day, week);
                
                }
           
                fprintf(temp, "%d\n", numberOfHelpers-1);
            }
            fprintf(temp, "%d\n", numberOfHelpers);
            fgets(buffer, sizeof(buffer), fp);
        }
        else
        {
            if (i == (weekIndex*3 +2))
            {
                char* names =(buffer);
                int pos = strcspn(names, "\n"); // find position of newline character
                if (pos < strlen(names)) {
                    names[pos] = '\0'; // replace newline character with null character
                }
                strcat(names, name);  
                printf("%s",names);
       
                fprintf(temp,"%s\n",names);
            }
        }
    }
    
    fclose(fp);
    fclose(temp);
    deleteThirdLineWithNumber("temp.txt"); remove(filename); 
    rename("temp.txt",filename); 
}
void initialisingFile(int maximumnumberOfhelper)
{
    char day[10];
    FILE *fp;
    fp = fopen("records.txt", "w");
    if (fp == NULL) {
        printf("Error opening file\n");
        return;
    }
    for (int i = 0; i < 84; i++) {
        if (i % 7 == 0) {
            strcpy(day, "MONDAY   ");
        } else if (i % 7 == 1) {
            strcpy(day, "TUESDAY  ");
        } else if (i % 7 == 2) {
            strcpy(day, "WEDNESDAY");
        } else if (i % 7 == 3) {
            strcpy(day, "THURSDAY ");
        } else if (i % 7 == 4) {
            strcpy(day, "FRIDAY   ");
        } else if (i % 7 == 5) {
            strcpy(day, "SATURDAY ");
        } else {
            strcpy(day, "SUNDAY   ");
        }
        fprintf(fp, "%s\n", day);
        fprintf(fp, "%d\n", maximumnumberOfhelper);
        fprintf(fp, "%s\n", "people:");
    }
       
    fclose(fp);
}
int main() {
    initialisingFile(10);
    addApplicant("sarveshwar", "MONDAY", 0,"records.txt");
    addApplicant("sarveshwar", "MONDAY", 0,"records.txt");
    return 0;
}
hivapdat

hivapdat1#

您只在添加申请者的当天写入people:行。因此,文件不再具有预期的84*3行。当您越过文件末尾时,对fgets()的调用失败,并发生未定义的行为。
在这种情况下,您需要一个else块来将原始people:行写入文件。
你还应该在people:行的人之间放一个分隔符。我在他们之间加了一个空格。

else
        {
            if (i == (weekIndex*3 +2))
            {
                char* names =(buffer);
                int pos = strcspn(names, "\n"); // find position of newline character
                if (pos < strlen(names)) {
                    names[pos] = '\0'; // replace newline character with null character
                }
                strcat(names, " "); // put space between people
                strcat(names, name);
                printf("%s",names);
                fprintf(temp,"%s\n",names);
            } else {
                fprintf(temp, "%s", buffer); // write the original people line
            }
        }

相关问题