Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要在c语言中将两个字符数组合并成第三个字符数组_C - Fatal编程技术网

我需要在c语言中将两个字符数组合并成第三个字符数组

我需要在c语言中将两个字符数组合并成第三个字符数组,c,C,我正在尝试将两个字符数组组合成第三个字符数组,让我们看看以下示例: 在这段代码中,我已经得到了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 当我运行代码时,会出现一个错误:预期为“;”在字符串常量之前。 我认为这不是问题所在 我已经找到了答案,这是所有的基本代码,谢谢你的帮助,如果没有很好的

我正在尝试将两个字符数组组合成第三个字符数组,让我们看看以下示例:

在这段代码中,我已经得到了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);
}
#包括
#包括
#包括
#包括/*听者使用统计系统
召唤*/
#定义缓冲区大小4096
#定义复制模式0644
void oops(char*,char*);
main(int ac,char*av[])/*参数向量*/
{
int in_fd,out_fd,n_chars;
charbuf[BUFFERSIZE];
如果(ac!=3){/*参数帐户“ac”*/
fprintf(标准,“用法:%s源目标\n”,*av);
出口(1);
}
printf(“%s”,av[2]);/*代码工作后测试删除*/
struct stat src,dst;//struct是一个
将所有类型合并为一个的变量
stat(av[1],&src);//stat系统调用
stat(av[2],&dst);//stat系统调用
if(dst.st_mode&S_IFDIR){//检查
如果第二个参数int,则数组是文件或目录
printf(“\n它是一个目录\n”);
printf(“%s”,av[2]);
strcat(av[2],“/”;/*它连接两个字符串或字符*/
strcat(av[2],av[1]);/*它接受两个参数,即两个字符串或字符数组,并将结果连接字符串存储在参数中指定的第一个字符串中*/
printf(“\n%s”,av[2]);/*测试值是否与
期望*/
printf(“\n%s”,av[1]);
}
如果((src.st_-dev==dst.st_-dev)&&(src.st_-ino==dst.st_-ino)){/*与索引节点号的文件属性和设备的id相匹配*/
printf(“\n目标文件和源文件相同\n”);
}
否则{
如果((in_fd=open(av[1],O_RDONLY))=-1)
oops(“无法打开”,av[1]);
如果((out_fd=creat(av[2],src.st_mode))==-1)/*“st_mode”表示对文件的权限,则说明文件的模式*/
oops(“无法创建”,av[2]);
而((n_chars=read(in_fd,buff,BUFFERSIZE))>0)
如果(写(输出fd、buf、n字符)!=n字符)
oops(“写入错误”,av[2]);
如果(n_字符==-1)
oops(“读取错误”,av[1]);
如果(关闭(输入fd)=-1 | |关闭(输出fd)=-1)
oops(“关闭文件时出错”,”);
}
}
无效oops(字符*s1,字符*s2)
{
fprintf(标准,“错误:%s”,s1);
perror(s2);
出口(1);
}

非常基本的例子:

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

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

printf("%s", a);
输出:

你好,世界


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

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

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

我不确定我是c语言的新手,所以我不确定它是否是c89。我什么都不懂。首先,我看不到三个cgaracter数组。我不知道表达式av[1]和av[2]的类型。显示数组的声明及其初始化方式。您从何处获取此声明main(char*av[])?