LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1279|回复: 7

一个简单的c程序,大家帮忙看一下!

[复制链接]
发表于 2003-1-31 10:17:28 | 显示全部楼层 |阅读模式
/*this is a example of copy*/
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
main ()
{
  int a, b;
  char c[20];
  char d[20];
  char buf[1024];
  printf ("input file name of source:");
  scanf ("%s", c);
  printf ("input file name of destination:");
  scanf ("%s", d);
  a = open (c, O_RDWR);
  read (a, buf, 1024);
  close (a);
  printf ("%s", buf); /*test buf*/
  creat (buf, S_IRUSR | S_IWUSR);
  b = open (d, O_RDWR);
  write (b, buf, strlen (buf));
  close (b);
}
我的意思是文件复制,但总得到一个很怪的文件,如我文件11中内容为123 erty,我想复制给12,但总得到名为123 etry???的文件。
发表于 2003-1-31 13:06:35 | 显示全部楼层
先把文件名数组清空
 楼主| 发表于 2003-1-31 13:43:27 | 显示全部楼层
不好意思,出了个小错误,应该是 creat (d, S_IRUSR | S_IWUSR);就对了。
发表于 2003-2-6 01:54:45 | 显示全部楼层
harrypotter:请问你用的那些函数在那里可以找到说明呀。我一点也看不懂。如:open (c, O_RDWR);
read (a, buf, 1024);
close (a);
creat (d, S_IRUSR | S_IWUSR);
write (b, buf, strlen (buf));
 楼主| 发表于 2003-2-8 10:49:52 | 显示全部楼层
买本函数说明或有关gcc的书就ok了
发表于 2003-3-15 21:10:14 | 显示全部楼层
贴子深的太快了,书?有买的吗?没有见过你说的这两类书。有吗?
发表于 2003-3-15 22:22:24 | 显示全部楼层
By the way, you use read() to read in data from file, but you should not use strlen() to get the data length, as strlen len assume the string is terminated by a NULL (\0). use the return value of read to know how many byte you've read in from that file.
发表于 2003-3-17 11:47:02 | 显示全部楼层
标准的系统调用 任何一本操作系统都会讲的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表