c - 如何在C中获取源文件(我要复制的文件)和复制文件的信息

标签 c file unix copy copy-paste

当我想复制源文件时,我想获取源文件信息,然后当源文件已经被复制时,我想获取目标文件信息。代码的问题是我无法复制并获取源文件和目标文件信息。

如何修复我的代码以复制文件并获取源和目标信息?

代码:

#define BUFFER 100 // ** increased - file path can get pretty long
#define BUFFERSIZE      4096
#define COPYMODE        0644

void oops(char *, char *);
int file_exist(char *filename)
{
 struct stat buffer;
 return (stat (filename, &buffer) == 0);
}

int main(int argc, char *argv[])
{
char ch, source_file[20], target_file[20];
FILE *source, *target;

 //  printf("Enter name of file to copy\n");
 //  fgets(source_file, 20, stdin);
source_file = argv[20];
 source = fopen(source_file, "r");

  if( source == NULL )
  {
  printf("Press any key to exit...\n");
  exit(EXIT_FAILURE);
  }

  printf("Enter name of target file\n");
  fgets(target_file, 20 , stdin);

  target = fopen(target_file, "w");

  if( target == NULL )
  {
  fclose(source);
  printf("Press any key to exit...\n");
  exit(EXIT_FAILURE);
  }

   while( ( ch = fgetc(source) ) != EOF )
  fputc(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);
       struct stat sb;

       if (argc != 2) {
           fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
           exit(EXIT_FAILURE);
       }

       if (stat(argv[1], &sb) == -1) {
           perror("stat");
           exit(EXIT_SUCCESS);
       }

       printf("File type:                ");

       switch (sb.st_mode & S_IFMT) {
       case S_IFBLK:  printf("block device\n");            break;
       case S_IFCHR:  printf("character device\n");        break;
       case S_IFDIR:  printf("directory\n");               break;
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
       case S_IFLNK:  printf("symlink\n");                 break;
       case S_IFREG:  printf("regular file\n");            break;
       case S_IFSOCK: printf("socket\n");                  break;
       default:       printf("unknown?\n");                break;
       }

       printf("I-node number:            %ld\n", (long) sb.st_ino);


       exit(EXIT_SUCCESS);
}   

void oops(char *s1, char *s2)
{
fprintf(stderr,"Error: %s ", s1);
perror(s2);
exit(1);
}

最佳答案

除了评论中提到的错误之外,我不确定您的困难在哪里。我简化了您的代码,删除了位域掩码,因为我没有它们的定义。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
    int ch;                                 // <--- int not char
    struct stat sb;
    FILE *source, *target;

    if (argc < 3) {
        printf("Enter two args: source and destination file names\n");
        exit(EXIT_FAILURE);
    }
    source = fopen(argv[1], "r");
    if( source == NULL ) {
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }

    target = fopen(argv[2], "w");
    if( target == NULL ) {
        fclose(source);
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }

    while( ( ch = fgetc(source) ) != EOF )
        fputc(ch, target);
    fclose(source);
    fclose(target);
    printf("File copied successfully.\n");

    if (stat(argv[1], &sb) == -1) {
        perror("stat");
        exit(EXIT_SUCCESS);
    }
    printf("File %s type: 0x%04X Mode: 0x%04X\n", argv[1], (unsigned)sb.st_ino, (unsigned)sb.st_mode);

    if (stat(argv[2], &sb) == -1) {
        perror("stat");
        exit(EXIT_SUCCESS);
    }
    printf("File %s type: 0x%04X Mode: 0x%04X\n", argv[2], (unsigned)sb.st_ino, (unsigned)sb.st_mode);

    return 0;
}

程序输出:

>test test.c test2.c
File copied successfully.
File test.c type: 0x0000 Mode: 0x81B6
File test2.c type: 0x0000 Mode: 0x81B6

关于c - 如何在C中获取源文件(我要复制的文件)和复制文件的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30215462/

相关文章:

c - 为什么在这个 Linux 内核代码中截断参数?

C "%d"格式说明符

c - Winsock2 在 fd 0 (stdin) 上的 select() 失败

c - select() 和 read() 在串行端口读取时超时,但之前的 write() 成功?

将 mp3 文件的数据复制到另一个文件。 C

java - 将 BST 的遍历输出到文件

file - 编辑平面文件以在每行上包含特定数量的字符?

shell - 从 Solaris KornShell 运行 .ksh 脚本

linux - 如何使用 git-archive 获得 GNU tar 的 --strip-components 的效果?

bash - 尾-F log.log | grep 响应时间 |切-d = -f 2