C一个备份点文件的程序

时间:2016-01-30 17:36:57

标签: c segmentation-fault

所以我写了一个小程序,用git存储库继续更新我的dotfiles在另一个目录中。我只是想继续练习我的C编码。

该计划严重违反DRY原则,也不起作用。 这是代码:

#include <stdio.h>


int main(void)
{
    FILE *files_from[7];
    files_from[0] = fopen(".vimrc", "r");
    files_from[1] = fopen(".profile", "r");
    files_from[2] = fopen(".tmux_conf", "r");
    files_from[3] = fopen(".zshrc", "r");
    files_from[4] = fopen(".bashrc", "r");
    files_from[5] = fopen(".bash_aliases", "r");
    files_from[6] = fopen(".gitconfig", "r");

    FILE *files_to[7];
    files_to[0] = fopen("dotFiles/.vimrc", "w");
    files_to[1] = fopen("dotFiles/.profile", "w");
    files_to[2] = fopen("dotFiles/.tmux.conf", "w");
    files_to[3] = fopen("dotFiles/.zshrc", "w");
    files_to[4] = fopen("dotFiles/.bashrc", "w");
    files_to[5] = fopen("dotFiles/.bash_aliases", "w");
    files_to[6] = fopen("dotFiles/.gitconfig", "w");


    for(int i = 0; i < 7; ++i)
    {
        int character;
        while( (character = fgetc(files_from[i])) != EOF)
        {
            fputc(character, files_to[i]); 
        }
    }



}

为什么会出现分段错误? (无论如何要保持干燥?)

编辑:你没事。其中一个是NULL。我只是在while循环之前添加了一个if语句来检查来自任一数组的当前文件是否为NULL。

现在我怎么能做这个干?

2 个答案:

答案 0 :(得分:1)

您的代码中有很多可能会出现段错误。由于您没有检查任何fopen(),因此一个丢失的文件将导致fgetc()发生段错误。与fputc()相同。您可以使用ltrace确切地查看哪个库调用导致了段错误。

答案 1 :(得分:1)

正如其他人写的那样,SegFault有很多可能的原因 添加一些错误控制将有所帮助。

关于如何使它干,这是一种可能性:

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

#define MAX_PATH (50)
#define dim(x) (sizeof(x)/sizeof(x[0]))


static const char sFiles[][MAX_PATH] = {
  ".vimrc",
  ".profile",
  ".tmux_conf",
  ".zshrc",
  ".bashrc",
  ".bash_aliases",
  ".gitconfig", 
};
static const char sDestDir[] = {"dotFiles/"};



int main(void)
{
  long num = dim(sFiles);
  printf("Size: %lu \n", num);
  FILE *files_from;
  FILE *files_to;
  char destPath[MAX_PATH];
  int character;
  int s = 0;

  for (int i=0; i<num; i++) {
    errno = 0;
    printf("loop: %d\n",i);
    strncpy(destPath, sDestDir, MAX_PATH);
    files_from = fopen(sFiles[i], "r");
    if (files_from == NULL) {
      fprintf(stderr, "File %s open failed: %s\n", sFiles[i], strerror(errno));
      continue;
    }
    strncat(destPath, sFiles[i], MAX_PATH);
    files_to = fopen(destPath, "w");
    if (files_to == NULL) {
      fprintf(stderr, "File %s open failed: %s\n", destPath, strerror(errno));
      s = 0;
      s = fclose(files_from);
      if (EOF == s) {
        perror("Close Failed");
      }
      continue;
    }
    printf("Now Copying %s to %s\n", sFiles[i], destPath);
    while( (character = fgetc(files_from)) != EOF) {
      fputc(character, files_to);
    }
    s = 0;
    s = fclose(files_to);
    if (EOF == s) {
      perror("Close Failed");
    }
    s = 0;
    s = fclose(files_from);
    if (EOF == s) {
      perror("Close Failed");
    }
  }

  return 0;
}

我添加了一些错误控制,即使其中一个文件丢失,程序也会继续工作。