LinuxSir.cn,穿越时空的Linuxsir!

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

cat不能显示输入的内容?

[复制链接]
发表于 2003-2-12 19:18:53 | 显示全部楼层 |阅读模式
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
main ()
{
  char name[8];
  char buf[1024];
  int a,b;
  printf ("input content:");
  gets (buf);
  printf ("input file name:");
  scanf ("%s", name);
  open (name, O_WRONLY);
  a = open (name, O_WRONLY);
  write (a, buf,strlen(buf));
  close (a);
}
以上程序运行后,向一个空文件,如aa,中输入一个自符串,`用ls看文件字节是增加了,但cat aa却显示不出内容,这是为何?
发表于 2003-2-13 09:56:38 | 显示全部楼层

Re: cat不能显示输入的内容?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
main ()
{
  char name[8];
  char buf[1024];
  int a,b;
  printf ("input content:");
  gets (buf);
  printf ("input file name:");
  scanf ("%s", name);
  open (name, O_WRONLY);          /*这句open干什么用的?,ykwj121*/
  a = open (name, O_WRONLY);      /*如果没有这个文件,你却要新建的话,你必须带上O_CREAT,即否则O_WRONLY|O_CREAT,没有open就会直接返回错误,根本不会写文件,ykwj121*/
  write (a, buf,strlen(buf));
  close (a);
}

我试过,修改后的是可以的。cat也没有问题。
 楼主| 发表于 2003-2-13 10:54:05 | 显示全部楼层
我设想就是对一个存在的空文件写入,还是不行.
发表于 2003-2-13 12:48:03 | 显示全部楼层
我贴出代码,你编译试试:

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. main ()
  7. {
  8.         char name[8];
  9.         char buf[1024];
  10.         int a,b;
  11.         printf ("input content:");
  12.         gets (buf);
  13.         printf ("input file name:");
  14.         scanf ("%s", name);
  15.         if((a = open (name, O_WRONLY|O_CREAT))<0)
  16.                 printf("open error\n");
  17.         write (a, buf,strlen(buf));
  18.         close (a);
  19. }
复制代码
 楼主| 发表于 2003-2-13 14:10:18 | 显示全部楼层
[root@localhost C-lib]# ls -l aa
-rw-r--r--    1 root     root            0  2月 12 14:09 aa
[root@localhost C-lib]# gcc -o test test.c
/tmp/ccCGVUVI.o: In function `main':
/tmp/ccCGVUVI.o(.text+0x2e): the `gets' function is dangerous and should not be
used.
[root@localhost C-lib]# ./test
input content:aa
input file name:i like linux!
[root@localhost C-lib]# cat aa
[root@localhost C-lib]#
你的代码是test.c
 楼主| 发表于 2003-2-13 14:13:33 | 显示全部楼层
错了,是这次:
[root@localhost C-lib]# ls -l aa
-rw-r--r--    1 root     root            0  2月 12 14:14 aa
[root@localhost C-lib]# ./test
input content:i like linux!
input file name:aa
[root@localhost C-lib]# ls -l aa
-rw-r--r--    1 root     root           13  2月 12 14:14 aa
[root@localhost C-lib]# cat aa
[root@localhost C-lib]#
发表于 2003-2-13 15:19:13 | 显示全部楼层
那偶没招了。你看我的:

[root@abcdefg test]# ls -al aa
----------    1 root     root            4 Feb 13 15:15 aa
[root@abcdefg test]# ./testcat
input content:I like Linux!
input file name:aa
[root@abcdefg test]# cat aa
I like Linux![root@abcdefg test]# ls -al aa
----------    1 root     root           13 Feb 13 15:17 aa
[root@abcdefg test]#


你要不要重启机器再看看,你的系统是什么的?
 楼主| 发表于 2003-2-13 20:43:16 | 显示全部楼层
rh80
 楼主| 发表于 2003-2-16 13:19:51 | 显示全部楼层
hehe,i think there is no mistakes in my codes.
http://www.linuxsir.cn/forum.php?mod=viewthread&tid=24470
发表于 2003-2-17 10:26:34 | 显示全部楼层
因为我没有用RH80,所以想不到怎么会有这样奇怪的原因。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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