我需要将两个char数组组合成C语言的第三个char数组

时间:2019-05-18 12:35:39

标签: c

我正在尝试将两个char数组合并为第三个char数组,让我们看下面的示例:

在这段代码中,我已经在av [1]和av [2]中获得了值。

仅以值的示例为例,让av [1] = ab和av [2] = fg

main (char *av[])
{
av[2] = av[1] "/" av[2]

printf ("%s" , av[2]);
}

我期望的输出是:     ab / fg

当我运行代码时,发生错误,提示:预期';'在字符串常量之前。 我不认为这是问题所在。


我找到了答案,这是所有唯一的代码,感谢您的帮助,对不起,如果它组织得不好,我仍在学习。 下面的代码实际上完成了cp在Linux终端中的工作,它只是cp的重复功能。它可能无法完成cp可以做的所有事情,但却可以完成大部分工作。

#include        <stdio.h>
#include        <unistd.h>
#include        <fcntl.h>
#include    <sys/stat.h>                    /*hearder to use Stat system 
call*/

#define BUFFERSIZE      4096
#define COPYMODE        0644

void oops(char *, char *);

main(int ac, char *av[])/*argument vector*/
{
int     in_fd, out_fd, n_chars;
char    buf[BUFFERSIZE];

if ( ac != 3 ){ /* argument account"ac"*/
    fprintf( stderr, "usage: %s source destination\n", *av);
    exit(1);
}




printf("%s", av[2]);                    /*Test deleat after code works*/


struct stat src, dst;                                       // struct is a 
variable that combies all types into one


stat(av[1], &src);                                      //stat system call

stat(av[2], &dst);                                      //stat system call


if( dst.st_mode & S_IFDIR ){                                    // checks 
 if the second argument int the array is a file or a directory

printf ("\n It is a directory \n");

printf("%s", av[2]);


strcat(av[2],"/");              /* it concatenates two string or character*/
strcat(av[2],av[1]);                /* It takes two argument, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument.*/

printf("\n %s",av[2]);              /* testing if values are the same as 
expected*/
printf("\n %s",av[1]);









    }




if ((src.st_dev == dst.st_dev) && (src.st_ino == dst.st_ino)) {                 /* compering the file attribute of an inode number and the id of device*/

printf("\n Destination file and source file are same \n");                  

}

else {


if ( (in_fd=open(av[1], O_RDONLY)) == -1 )
    oops("Cannot open ", av[1]);

if ( (out_fd=creat( av[2], src.st_mode)) == -1 )                    /* "st_mode" indicates the permissions on the file, tells the modes on a file.*/
    oops( "Cannot creat", av[2]);







while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
    if ( write( out_fd, buf, n_chars ) != n_chars )
        oops("Write error to ", av[2]);
if ( n_chars == -1 )
    oops("Read error from ", av[1]);


if ( close(in_fd) == -1 || close(out_fd) == -1 )
    oops("Error closing files","");
}
}

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

2 个答案:

答案 0 :(得分:0)

尝试使用strcat中的<string.h>功能

非常基本的示例:

char a[15] = "Hello";
char b[] = "World";

strcat(a, " ");
strcat(a, b);

printf("%s", a);

输出:

  

Hello World

只需确保目标字符数组有足够的空间来容纳整个串联的字符串。

答案 1 :(得分:-1)

如果您只想打印出结果,则只需执行以下操作即可:

getSupportFragmentManager().beginTransaction().remove(FragA).commit();
getSupportFragmentManager().beginTransaction().remove(FragB).commit();
getSupportFragmentManager().beginTransaction().remove(FragC).commit();