C重命名程序不重命名

时间:2015-11-09 21:00:54

标签: c rename

我所知道的关于C的一切(不多)告诉我这个程序应该正常工作。但事实并非如此。

在OS X中打开时,终端只是说“中止陷阱:6”并且没有结果。

在Xcode调试器中运行时,重命名函数中的filename和fullname变量是正确的。帮助

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

int main(void)
{
    int i, j, k;

    char value1[26], value2[70], idcode[11], filename[16], fullname[64], filenamestring[16], fullnamestring[64];

    // Opening file.

    FILE *nomes;
    nomes = fopen("/Users/EA/Desktop/Songs/backup.txt", "rt");
    if(nomes == NULL)
    {
        printf("Erro ao abrir backup.txt");
        exit(1);
    }

    // Skipping beggining of file until first value.

    for(i=0; i<10; i++)
        {
            fscanf(nomes, "%*s");
        }

    // Reading first value, and repetition.

    while(fscanf(nomes, "%s", value1) == 1)
    {
        j = k = 0;

        // Extracting idcode from value1.

        for(i=7; i<18; i++)
        {
            idcode[j] = value1[i];
            j++;
        }

        // Filling the complete filename.

        strcat(filename, ".mp4");

        // Reading second value, the "human" filename.

        fscanf(nomes, "\n%[^\n]", value2);
        for(i=6; i<70; i++)
        {
            fullname[k] = value2[i];
            k++;
        }

        // Transforming filenames into strings for rename function.

        strncpy(filenamestring, filename, 16);
        strncpy(fullnamestring, fullname, 64);

        // Renaming the files.

        rename(filename, fullname);

        // Skipping useless data before the next cycle.

        for(i=0; i<9; i++)
        {
            fscanf(nomes, "%*s");
        }
    }

    // Closing file and ending program.

    fclose(nomes);
    return(0);
}

0 个答案:

没有答案