LinuxSir.cn,穿越时空的Linuxsir!

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

一个很菜很菜的问题

[复制链接]
发表于 2004-7-31 02:10:36 | 显示全部楼层 |阅读模式
我刚开始学编程,在读写文件的时候遇到了困难,我的文件可以写,但不可以读,不知道是怎么回事,请高手帮忙
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
         int fd;
        char buf[10];
        char data[]="this is a test\n";
       
        if((fd = open("/root/tel.txt",O_RDWR)) == -1)
        {
                printf("open file error!\n");
                exit(-1);
        }
        else
        {                       
                if(write(fd,data,strlen(data)) != strlen(data))
                {
                        printf("write error!\n");
                        exit(-1);
                }
               
                if(read(fd,buf,9) != 9)
                {
                        printf("read error!\n");                       
                        exit(-1);
                }               
                buf[9] = 0;
                printf("the number of read is %d\n",strlen(buf));               
                printf("the string of read is %s \n",buf);
        }
       
}
我不知道为什么就是读不出来,我用od看了,写进去了,请高手帮忙看看问题出在哪里?我知道学程序,首先要学调试,但我不知道linux下用什么调试程序,请告诉我一些调试工具,能介绍些用法更好

发表于 2004-7-31 03:13:44 | 显示全部楼层
但兄弟你是不是應該先seek 回去或者把檔案close再打開?碜x呢?
 楼主| 发表于 2004-7-31 09:33:54 | 显示全部楼层

谢谢版主

就是这个错误,我怎么没有想到呢,实在是太笨了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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