在 C 中删除和重命名 2 个文本文件时出现分段问题

问题描述 投票:0回答:0

--所有文件都位于同一目录中。 当我第二次尝试删除和重命名

records.txt
temp.txt->records.txt
时,我遇到了分段错误。

我尝试在使用

close ()
之前和之后关闭
deleteThirdLineWithNumber
。它没有任何区别。

这就是问题:-

春天也在敲响“喝我的朋友”葡萄园的大门。 众所周知,葡萄喜欢照料和工作,但最重要的是它们喜欢 劳动,通常会得到丰收的回报。葡萄园 需要很多帮手,所以正在为这些人准备一个应用程序 谁想申请工作。

邀请那些可以在一周内工作的人申请 春季。申请时,他们应提供姓名和 他们一周中的哪几天可以上班。天数为 如下:周一周三周四。候选人将分开的日子 与一个空间。农场知道在哪几天需要多少工人 (星期一、星期二等)。如果一天已经满了,申请 那天不应该接受申请者。

申请人的数据将存储在一个文件中,此外 对数据条目,我们应该有修改,删除的可能性 并按地区创建申请人列表或完整列表 申请人。

#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;
}
c segmentation-fault text-files
© www.soinside.com 2019 - 2024. All rights reserved.