我目前正在处理一个C程序问题,以便让我保持目录同步。它的主旨是:
我在获取需要打开和删除的文件的完整路径时遇到问题。所以我决定使用C函数chdir(path)。
我能够创建一个新文件并删除文件,但由于某种原因,即使在打开两个文件流之后,文件中的数据也不会被复制,我不知道为什么。代码肯定是凌乱的,但我无法弄清楚我哪里出错了。洞察力将不胜感激。我是C的新手,处理缓冲区和内存管理是我现在还不太擅长的事情。有什么想法吗?
#include "csapp.h"
#include <stdio.h>
#include <dirent.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
// prototypes
bool inOtherDir(char*, char*);
void copyFile(char*, char*, char*, char*);
int isNewer(char*, char*, char*, char*);
int main(int argc, char **argv ) {
// variables
struct dirent *dirone;
struct dirent *dirtwo;
char* dirAPath = argv[1]; // this will be used for absolute paths
char* dirBPath = argv[2];
char cwd[256];
char* cwdptr = cwd;
// get the CWD for use later
if(getcwd(cwd, sizeof(cwd)) == NULL) {
perror("getcwd() error()");
return 1;
}
// Testing
// printf("CWD is: %s\n", cwd);
if (argc < 3) {
printf("Not enough arguments provided, please provide two directories.\n");
return 1;
}
// open directory a
DIR *ptrone = opendir(argv[1]);
// open directory b
DIR *ptrtwo = opendir(argv[2]);
if (ptrone == NULL || ptrtwo == NULL) {
printf("Could not open directory\n");
return 1;
}
printf("Checking Sync Status: \n");
// Open the directory and check to see if the file is a regular file
// if it is regular, then do work on it based on if its in the other
// directory or not.
while ((dirone = readdir(ptrone)) != NULL) {
if (dirone->d_type == DT_REG) {
if(inOtherDir(dirone->d_name, argv[2])) {
printf("File %s is in both directories.\n", dirone->d_name);
} else {
printf("File %s is in Directory A but not Directory B.\n", dirone->d_name);
printf("Copying %s in Directory A to Directory B.\n",dirone->d_name);
copyFile(dirone->d_name,dirAPath, dirBPath,cwdptr);
}
}
}
// check for files only in directory B, then delete them
while ((dirtwo = readdir(ptrtwo)) != NULL) {
if (dirtwo->d_type == DT_REG) {
if(inOtherDir(dirtwo->d_name, argv[1])) {
printf("File %s is in both directories.\n", dirtwo->d_name);
} else {
printf("File %s will be removed from the directory.\n",dirtwo->d_name);
chdir(dirBPath);
remove(dirtwo->d_name);
chdir(cwd);
}
}
}
closedir(ptrone);
closedir(ptrtwo);
return 0;
}
// Look through the directory to see if the same file is in there returns
// true if the file is in that directory and returns false if not.
bool inOtherDir(char* filename, char* direct) {
struct dirent *buf;
DIR *directory = opendir(direct);
if(directory == NULL) {
printf("Could not open directory.");
exit(1);
}
while((buf = readdir(directory)) != NULL) {
if ((strcmp(filename, buf->d_name)) == 0) {
return true;
}
}
closedir(directory);
return false;
}
// Copies the file from one directory to the other
void copyFile(char* filename, char* directory_from,char* directory_to, char* cwd) {
FILE *file, *copyfile;
char c;
chdir(directory_from);
file = fopen(filename,"r");
if(file == NULL) {
printf("Cannot open the file %s in copyFile.\n", filename);
}
chdir(cwd);
chdir(directory_to);
copyfile = fopen(filename, "w+");
if(copyfile == NULL) {
printf("Cannot open copy of the file %s in copyFile.\n", filename);
}
c = fgetc(file);
while(c != EOF){
fputc(c, copyfile);
c = fgetc(file);
}
printf("Contents copied successfully.\n");
fclose(file);
fclose(copyfile);
chdir(cwd);
return;
}