Linux论坛's Archiver

《开源》旗舰电子杂志2008年11期发布,火热下载中!

小鱼儿 发表于 2008-3-17 10:50

read函数不能读

最近在写一个自己的“who”命令,在使用read函数读文件的时候,遇到问题了,read始终读不到数据!具体的源码如下:

基本的原理是这样,who命令实际上是读文件/var/run/utmp(路径为UTMP_FILE),其中的数据是以struct utmp结构体保存的,程序只要从/var/run/utmp中以该结构体形式读出数据,再显示出来即可,可是,我在读的时候,怎么都读不出来!

#include<stdio.h>
#include<utmp.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
   struct utmp current_record;
   int  utmpfd;
   int  reclen = sizeof(current_record);
  
   if(utmpfd = open(UTMP_FILE,O_RDONLY)==-1){
      peeor(UTMP_FILE);
      return 0;
   }
   while(  read(utmpfd,&current_record,reclen)==reclen)  //就是这里一直读不到数据?????
         show_info(&current_record);
   close(utmpfd);
   return 0;
}

void show_info(struct utmp * utmpfd)
{
   printf(" % -8.8s",utmpfd->ut_name);
   printf(" \n");
}

rose 发表于 2008-3-17 15:09

if(utmpfd = open(UTMP_FILE,O_RDONLY)==-1){
这句话缺个括号,应该是
if( (utmpfd = open(UTMP_FILE,O_RDONLY) ) ==-1){
你那样写的话,那个utmpfd就总是0,一般正常的fd都是大于3的

小鱼儿 发表于 2008-3-17 16:25

谢谢斑竹啊,我试试

wangqiang6100 发表于 2008-3-18 20:38

READ()读串口的问题

在RECIEVE()函数里始终读不到READ里的数据啊,快疯了,顺便说一下,我采集的串口数据是GPS模块采集回来的信息.各位大哥,拜托了!!!
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include "gps.h"

#define BAUDRATE B4800
#define COM1 "/dev/ttyS0"
#define COM2 "/dev/ttyS1"
#define ENDMINITERM 27 /* ESC to quit miniterm */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
volatile int fd;
GPS_INFO gps_info;
int GET_GPS_OK=FALSE;
char GPS_BUF[1024];

/*void child_handler(int s)
{
  printf("stop!!!\n");
   STOP=TRUE;
}*/


/*--------------------------------------------------------*/
void* show_gps_info(void * data)
{
        while(1){
                if(GET_GPS_OK){
                        GET_GPS_OK=FALSE;
                        printf("%s",GPS_BUF);
                        gps_parse(GPS_BUF,&gps_info);
                        show_gps(&gps_info);
                }
                usleep(100);
                if(STOP)break;
        }

}
/*--------------------------------------------------------*/

void* receive(void * data)
{
        int i=0;
        char c;
        int ss=0;
        char buf[1024];
        GPS_INFO GPS;
        
          printf("read modem\n");
      
          while (STOP==FALSE)
          {printf("read modem\n");
            ss=read(fd,&c,1024); /* com port */
        printf("%d/n",ss);
        printf("read modem\n");
            buf[i++] = c;
               
                if(c == '\n'){
                        strncpy(GPS_BUF,buf,i);
                        i=0;
                       
                        GET_GPS_OK=TRUE;
                }
                //if(STOP)break;
                //write(1,&c,1); /* stdout */
          }
          printf("exit from reading modem\n");
          return NULL;
}
/*--------------------------------------------------------*/
int main(int argc,char** argv)
{      int fd;   
      fd_set rd;
      int i;
      if((fd=open_port(fd,1))<0)
          {
            perror("open_port error");
            return;
          }
      if((i=set_opt(fd,4800,8,'N',1))<0)
          {
            perror("set_opt error");
            return;
          }
        
        /*struct termios oldtio,newtio,oldstdtio,newstdtio;
        struct sigaction sa;
        int ok;
        void * retval;
           fd = open(COM1, O_RDWR );
        if (fd <0) {
            perror(COM1);        
            exit(-1);
          }      
          tcgetattr(0,&oldstdtio);
          tcgetattr(fd,&oldtio); // save current modem settings
          tcgetattr(fd,&newstdtio); // get working stdtio
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;//ctrol flag
        newtio.c_iflag = IGNPAR; //input flag
        newtio.c_oflag = 0;                //output flag
        newtio.c_lflag = 0;
        newtio.c_cc[VMIN]=1;
        newtio.c_cc[VTIME]=0;
// now clean the modem line and activate the settings for modem
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd,TCSANOW,&newtio);//set attrib
          */
        while(1)
     {
        receive(0);
        //show_gps_info(0);
        //while(!STOP){
                //usleep(100000);
        //}
     }
          /*tcsetattr(fd,TCSANOW,&oldtio); // restore old modem setings
          tcsetattr(0,TCSANOW,&oldstdtio); // restore old tty setings
          close(fd);
          exit(0);*/
}
/* 绔彛璁剧疆*/
int set_opt(int fd,int nspeed,int nbits,char nevent,int nstop)
{
         struct termios newtio,oldtio;
         if(tcgetattr(fd,&oldtio)!=0)
          {
           perror("setupserial 1");
          return (-1);
          }
          bzero(&newtio,sizeof(newtio));
    /*璁剧疆瀛楃澶у皬*/
          newtio.c_cflag|=CLOCAL|CREAD;
          newtio.c_cflag&=~CSIZE;
    /*璁剧疆鏁版嵁浣?/
          switch(nbits)
          {
          case7:
             newtio.c_cflag|=CS7;
             break;
          case8:
             newtio.c_cflag|=CS8;
             break;
           }
    /*璁剧疆濂囧伓鏍¢獙浣?/
           switch(nevent)
           {
           case 'O':   //濂囨暟
                 newtio.c_cflag|=PARENB;
                 newtio.c_cflag|=PARODD;
                 newtio.c_iflag|=(INPCK|ISTRIP);
                 break;
           case 'E':   //鍋舵暟
                 newtio.c_iflag|=(INPCK|ISTRIP);
                 newtio.c_cflag|=PARENB;
                 newtio.c_cflag&=~PARODD;
                 break;
           case 'N':  //鏃犲鍋舵牎楠?
                 newtio.c_cflag&=~PARENB;
                 break;
           }
    /*璁剧疆娉㈢壒鐜?/
          switch(nspeed)
          {
           case 2400:
                cfsetispeed(&newtio,B2400);
                cfsetospeed(&newtio,B2400);
                break;
           case 4800:
                cfsetispeed(&newtio,B4800);
                cfsetospeed(&newtio,B4800);
                break;
           case 9600:
                cfsetispeed(&newtio,B9600);
                cfsetospeed(&newtio,B9600);
                break;
           case 19200:
                cfsetispeed(&newtio,B19200);
                cfsetospeed(&newtio,B19200);
                break;
           case 115200:
                cfsetispeed(&newtio,B115200);
                cfsetospeed(&newtio,B115200);
                break;
           case 57600:
                cfsetispeed(&newtio,B57600);
                cfsetospeed(&newtio,B57600);
                break;
           default:
                cfsetispeed(&newtio,B19200);
                cfsetospeed(&newtio,B19200);
                break;
          }
    /*璁剧疆鍋滄浣?/
           if(nstop==1)
                 newtio.c_cflag&=~CSTOPB;
           else if (nstop==2)
                 newtio.c_cflag|=CSTOPB;
    /*璁剧疆绛夊緟鏃堕棿鍜屾渶灏忔帴鏀跺瓧绗?/
            newtio.c_cc[VTIME]=0;
            newtio.c_cc[VMIN]=0;
    /*澶勭悊鏈帴鏀跺瓧绗?/
           tcflush(fd,TCIFLUSH);
    /*婵

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.