1. 如何实现c语言中复制文件的程序
1 以只读方式打开第一个文件
2 以写方式打开第二个文件
3 循环读取字符,并写入第二个文件。直到遇到EOF结束。
4 关闭文件。
代码:
#include<stdio.h>
intmain()
{
FILE*f1,*f2;
intc;
f1=fopen("in.txt","rb");
f2=fopen("out.txt","wb");//将in.txt复制为out.txt;
while((c=fgetc(f1))!=EOF)
fputc(c,f2);
fcloseall();
return0;
}
2. 用C语言实现文件拷贝
两种方法:
1. 用c语言的文件操作:读出文件argv[1]的内容,再把读出的内容写入到文件argv[2]中
2. 使用函数system来执行dos中的文件拷贝命令
char command[100]=" ";
strcat(command, argv[1]);
strcat(command," ");
strcat(command,argv[2]);
system( command );
3. C语言实现字符串拷贝函数的几种方法
首先是使用库函数
比如下面代码
void ourStrCopy(char S1[] , char S2[]){ strcpy(S1, S2); //该函数还有另一个版本可以按长度截取 }
还有一个函数是memcpy,这个是内存拷贝,原型是
void memcpy(void *dest, const void *src, size_t n); 需要注意的是这个函数第一个和第二个指针都是void型且第二个指针不能被修改,第三个参数是需要拷贝的内存长度按字节记。
然后是用指针引用,注意这个并非赋值,而是引用,这种操作需要注意内存。
char s1[] = "abcdefg";//定义一组字符串char *s2 = s1;//按照指针拷贝字符串
第三种方法就是直接赋值了
void outStrCopy(char s1[] , char s2[]){ int len1 = strlen(s1);//获取第一个字符串的长度 int len2 = strlen(s2);//获取第二个字符串的长度 int len = 0; //字符串总长度 if(len1 <= len2){ len = len2; //选择COPY的长度 }else{ len = len1; } for(int i = 0 ; i < len ; i++){ s1[i] = s2[i]; //实现数据拷贝 }}
4. 用c语言写一个字符串拷贝函数
*q='\0';
return
*q;
所以函数返回的是‘\0’。然后主函数里面你的printf打印输出的是函数的返回值而不是num这个数组,你把%s后面的参数改成num就行了。
5. c语言 文件拷贝
#include <stdio.h>
main(){
FILE *FI, *FO;
char last=0,c;
FI=fopen("f.in","r");
FO=fopen(f.out","w");
while(! feof(FI)){
c=fgetchar(FI);
if (last!=32 && last!=9 || c!=32 && c!=9) fputchar(FO,c);
last=c;
}
fclose(FI);
fclose(FO);
}
6. C语言表示拷贝的命令
用 system("COPY ....."); 调DOS COPY 命令就可以。路径 的单 \ 用 双 \\ 表示。
例如:
system("COPY abc.txt data\\abc.txt");
7. C语言中如何复制数组的内容
C语言中复制数组的内容源代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
void show_array(const int ar[], int n);
int main()
{
int values[SIZE] = {1,2,3,4,5,6,7,8,9,10};
int target[SIZE];
double curious[SIZE / 2] =
{2.0, 2.0e5, 2.0e10, 2.0e20, 5.0e30};
puts("memcpy() used:");
puts("values (original data): ");
show_array(values, SIZE);
memcpy(target, values, SIZE * sizeof(int));
puts("target ( of values):");
show_array(target, SIZE);
puts("
Using memmove() with overlapping ranges:");
memmove(values + 2, values, 5 * sizeof(int));
puts("values -- elements 0-5 copied to 2-7:");
show_array(values, SIZE);
puts("
Using memcpy() to double to int:");
memcpy(target, curious, (SIZE / 2) * sizeof(double));
puts("target -- 5 doubles into 10 int positions:");
show_array(target, SIZE/2);
show_array(target + 5, SIZE/2);
system("pause");
return 0;
}
void show_array(const int ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ar[i]);
putchar('
');
}
(7)C语言换码序列的拷贝扩展阅读
1、C语言编程中,将常用的操作封装成函数进行调用,可以大大简化程序的编写,而且在代码的维护性及可读性方面也提供了便利。
2、不同地方需要对处理后的数组内容多次进行显示,并且很多情况下并非显示数组里面的全部内容,而仅仅是想观察数组中的部分数据内容,若每次显示时都用printf函数写的话,可以写一个自定义的通用函数,用来根据需要显示数组中的内容。
8. C语言实现字符串拷贝函数有几种方式
首先是使用库函数
比如下面代码
void ourStrCopy(char S1[] , char S2[]){ strcpy(S1, S2); //该函数还有另一个版本可以按长度截取 }
还有一个函数是memcpy,这个是内存拷贝,原型是
void memcpy(void *dest, const void *src, size_t n); 需要注意的是这个函数第一个和第二个指针都是void型且第二个指针不能被修改,第三个参数是需要拷贝的内存长度按字节记。
然后是用指针引用,注意这个并非赋值,而是引用,这种操作需要注意内存。
char s1[] = "abcdefg";//定义一组字符串char *s2 = s1;//按照指针拷贝字符串
第三种方法就是直接赋值了
void outStrCopy(char s1[] , char s2[]){ int len1 = strlen(s1);//获取第一个字符串的长度 int len2 = strlen(s2);//获取第二个字符串的长度 int len = 0; //字符串总长度 if(len1 <= len2){ len = len2; //选择COPY的长度 }else{ len = len1; } for(int i = 0 ; i < len ; i++){ s1[i] = s2[i]; //实现数据拷贝 }}
9. c语言拷贝文件的代码
system(" D:\\a\\123.exe D:\\b\\123.exe");
用system 调DOS复制命令。单斜杠用双斜杠。
同名时,也可以用 *.*
system(" D:\\a\\123.exe D:\\b\\*.*");
---
文件读写方法复制,要用 "b" (二进制方法打开),一个字节一个字节读写并检查是否到了EOF,到了则关闭文件不再写。
10. C语言串拷贝(strcpy)和内存拷贝(memcpy)函数有什么不同
strcpy()函数只能拷贝字符串。strcpy()函数将源字符串的每个字节拷贝到目录字符串中,当遇到字符串末尾的null字符(\0)时,它会删去该字符,并结束拷贝。
memcpy()函数可以拷贝任意类型的数据。因为并不是所有的数据都以null字符结束,所以你要为memcpy()函数指定要拷贝的字节数。
在拷贝字符串时,通常都使用strcpy()函数;在拷贝其它数据(例如结构)时,通常都使用memcpy()函数。以下是一个使用strcpy()函数和memcpy()函数的例子:
#include <stdio. h>
#include <string. h>
typedef struct cust-str {int id ;char last_name [20] ;
char first_name[l5];} CUSTREC;void main (void);
void main (void){char * src_string = "This is the source string" ;
char dest_string[50];
CUSTREC src_cust;
CUSTREC dest_cust;
printf("Hello! I'm going to src_string into dest_string!
");
/ * Copy src_ string into dest-string. Notice that the destination
string is the first argument. Notice also that the strcpy()
function returns a pointer to the destination string. * /
printf("Done! dest_string is: %s
" ,
strcpy(dest_string, src_string)) ;
printf("Encore! Let's one CUSTREC to another.
") ;
prinft("I'll src_cust into dest_cust.
");
/ * First, intialize the src_cust data members. * /
src_cust. id = 1 ;
strcpy(src_cust. last_name, "Strahan");
strcpy(src_cust. first_name, "Troy");
/ * Now, Use the memcpy() function to the src-cust structure to
the dest_cust structure. Notice that, just as with strcpy(), the
destination comes first. * /
memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));