C rename()似乎无法让它工作

时间:2017-10-30 15:00:13

标签: c

我正在尝试创建一个程序,该程序接收列表文件名的数据库并将其与另一个列表进行比较。如果匹配,则应将具有该名称的文件重命名为相应的relationNumber。

在代码的最后我尝试重命名该文件,但它不允许我。是因为我试图传递chars而不是const chars?如果是这样,我该如何解决这个问题呢?在我尝试重命名文件之前,我打印出字符串以查看其中的内容并且字符串包含; “Zaalstra legitimatie.txt”和“1.17234842.txt”这就是我想要的。提前谢谢。

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

#define relationLen 10
#define true 1
#define false 0
#define resetptr NULL

typedef int bool;

char* strcasestr(char* haystack, char* needle) {
    size_t i, j;

    if (!needle[0])
        return (char*) haystack;

    for (i = 0; haystack[i]; i++) {
        bool matches = true;

        for (j = 0; needle[j]; j++) {
            if (!haystack[i + j])
                return NULL;

            if (tolower((unsigned char)needle[j]) != tolower((unsigned char)haystack[i + j])) {
                matches = false;
                break;
            }
        }
        if (matches) 
            return (char *)(haystack + i);
    }
    return NULL;
}

int main(void) {
    char database[1024][30];
    char name[30] = {0};
    char newName[30] = {0};
    char extension[1024][5];
    char lined[256], linef[256];
    char relationNumber[] = "1.1";
    char newRelationNumber[relationLen] = {0};
    char *ret, *number;
    int i, j, c, k, len = 0;

    FILE* filef = fopen("filelist.txt", "r");
    for(i = 0; (c = fgetc(filef)) != EOF; i++) {
        fgets(linef, sizeof(linef), filef);
        strcpy(database[i], (char[2]){(char) c, '\0'});
        strcat(database[i], linef);
        len = strlen(database[i]);
        for(j = 0, k = 5; j < 4; j++, k--) {
            extension[i][j] = database[i][len-k];
        }
        printf("%s ", extension[i]);
        printf("%s", database[i]);
    }

    FILE* filed = fopen("Database.txt", "r");
        fgets(lined, sizeof(lined), filed);
    printf("%s", lined);

    number = strstr(lined, relationNumber);

    for(i = 0; lined[i] != 9; i++)
        newName[i] = lined[i];
    newName[i] = '\0';

    for(i = 0; i < relationLen; i++)
        newRelationNumber[i] = number[i];
    newRelationNumber[i] = '\0';


    number = resetptr;

    strcat(newRelationNumber, ".txt");
    ret = strcasestr(database[i], newName);

    printf("%s", database[0]);
    printf("%s", newRelationNumber);
    i = rename(database[0], newRelationNumber);
    printf("\n%d", i);

    return 0;
}

0 个答案:

没有答案
相关问题