| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 774 人关注过本帖
标题:[求助]C语言文件写入问题...急...急.......
只看楼主 加入收藏
sxbo
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-1-7
收藏
 问题点数:0 回复次数:0 
[求助]C语言文件写入问题...急...急.......
我的程序要实现的功能是:
  把一结构体(含10个空格和一个5位数的两种字符型数据)写入到一个磁盘文件file.txt中.然后在磁盘上直接看file.txt数据.
我的完整程序是下面:(程序有点长,看我注释的1,2,3处就可以)
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <pthread.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>

#define BAUDRATE B9600
#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;
int GET_GPS_OK=FALSE;
char GPS_BUF[1024];
static int baud=BAUDRATE;

//#define  K  0.003818181



struct student {
            char time[14];
         char space[10];
           char age[5];
          };
static void delay(double);
//char *gcvt(double value,int ndigit,char *buf);



typedef struct{
    int year;  
    int month;
    int day;
    int hour;
    int minute;
    int second;
}date_time;

typedef struct{
     date_time D;//鏃堕棿
     char status;          //鎺ユ敹鐘舵??     double    latitude;   //绾?害
     double longitude;  //缁忓害
     char NS;           //鍗楀寳鏋?     char EW;           //涓滆タ
     double speed;      //閫熷害
     double high;       //楂樺害
}GPS_INFO;

#define USE_BEIJING_TIMEZONE

GPS_INFO gps_info;
static int GetComma(int num,char* str);
static void UTC2BTC(date_time *GPS);
static double get_double_number(char *s);
void gps_parse(char *line,GPS_INFO *GPS);
//void show_gps(GPS_INFO *GPS);

//void show_gps(GPS_INFO *GPS)
//{
 
 
   
  
   //sleep(1);

//}

////////////////////////////////////////////////////////////////////////////////
//瑙i噴gps鍙戝嚭鐨勬暟鎹?//0      7  0   4 6   0     6 8 0        90         0  3      0        9      
//$GPRMC,091400,A,3958.9870,N,11620.3278,E,000.0,000.0,120302,005.6,W*62    
//$GPGGA,091400,3958.9870,N,11620.3278,E,1,03,1.9,114.2,M,-8.3,M,,*5E    
void gps_parse(char *line,GPS_INFO *GPS)
////////////////////////////////////////////////////////////////////////////////
{
    int i,tmp,start,end;
    char c;
    char* buf=line;
    c=buf[5];

    if(c=='C'){//"GPRMC"
        GPS->D.hour   =(buf[ 7]-'0')*10+(buf[ 8]-'0');
        GPS->D.minute =(buf[ 9]-'0')*10+(buf[10]-'0');
        GPS->D.second =(buf[11]-'0')*10+(buf[12]-'0');
        tmp = GetComma(9,buf);
        GPS->D.day    =(buf[tmp+0]-'0')*10+(buf[tmp+1]-'0');
        GPS->D.month  =(buf[tmp+2]-'0')*10+(buf[tmp+3]-'0');
        GPS->D.year   =(buf[tmp+4]-'0')*10+(buf[tmp+5]-'0')+2000;
        //------------------------------
        GPS->status      =buf[GetComma(2,buf)];
        GPS->latitude =get_double_number(&buf[GetComma(3,buf)]);
        GPS->NS       =buf[GetComma(4,buf)];
        GPS->longitude=get_double_number(&buf[GetComma(5,buf)]);
        GPS->EW       =buf[GetComma(6,buf)];
#ifdef USE_BEIJING_TIMEZONE
        UTC2BTC(&GPS->D);
#endif
    }
    if(c=='A'){ //"$GPGGA"
        GPS->high     = get_double_number(&buf[GetComma(9,buf)]);
        
    }
}

static double get_double_number(char *s)
{
    char buf[128];
    int i;
    double rev;
    i=GetComma(1,s);
    strncpy(buf,s,i);
    buf[i]=0;
    rev=atof(buf);
//    printf("s=%s ,buf= %s,val= %f\n",s,buf,rev);
    return rev;
    
}
////////////////////////////////////////////////////////////////////////////////
//寰楀埌鎸囧畾搴忓彿鐨勯?楀彿浣嶇疆
static int GetComma(int num,char *str)
{
    int i,j=0;
    int len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]==',')j++;
        if(j==num)return i+1;    
    }
    return 0;    
}

//#ifdef USE_BEIJING_TIMEZONE
////////////////////////////////////////////////////////////////////////////////
//灏嗕笘鐣屾椂杞?崲涓哄寳浜?椂
static void UTC2BTC(date_time *GPS)
{

//***************************************************
//濡傛灉绉掑彿鍏堝嚭,鍐嶅嚭鏃堕棿鏁版嵁,鍒欏皢鏃堕棿鏁版嵁+1绉?        GPS->second++; //鍔犱竴绉?        if(GPS->second>59){
            GPS->second=0;
            GPS->minute++;
            if(GPS->minute>59){
                GPS->minute=0;
                GPS->hour++;
            }
        }    

//***************************************************
        GPS->hour+=8;
        if(GPS->hour>23)
        {
            GPS->hour-=24;
            GPS->day+=1;
            if(GPS->month==2 ||
                       GPS->month==4 ||
                       GPS->month==6 ||
                       GPS->month==9 ||
                       GPS->month==11 ){
                if(GPS->day>30){
                       GPS->day=1;
                    GPS->month++;
                }
            }
            else{
                if(GPS->day>31){
                       GPS->day=1;
                    GPS->month++;
                }
            }
            if(GPS->year % 4 == 0 ){//
                   if(GPS->day > 29 && GPS->month ==2){
                       GPS->day=1;
                    GPS->month++;
                }
            }
            else{
                   if(GPS->day>28 &&GPS->month ==2){
                       GPS->day=1;
                    GPS->month++;
                }
            }
            if(GPS->month>12){
                GPS->month-=12;
                GPS->year++;
            }        
        }
}







/*--------------------------------------------------------*/
int main()
{       struct student k[10];
    int ijw,q1,q2,q3;
    FILE *fp;
    char buff[30];
    struct termios oldtio,newtio,oldstdtio,newstdtio;
    struct sigaction sa;
    int ok;
        int i=0;
    char c;
    char buf[1024];
    GPS_INFO GPS;
    int e1;
     pthread_t th_a, th_b, th_show;
     void * retval;
    int q,w,e,r,t,y;
   struct tm *p;
   char zz[14],adn[5];
    int sig=6;//转锟斤拷锟斤拷锟斤拷


    double s;
       int j,n;
       float hh;
        unsigned  long  m[100],temp;
        int fd,count;        //driver is open
        unsigned long result,f=0;
    while(1)
{
         for(e1=0;e1<10;e1++)
  {
    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 = baud | 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      */

//      pthread_create(&th_a, NULL, keyboard, 0);
 
      while (1)
      {
        read(fd,&c,1); /* com port */
        buf[i++] = c;
        if(c == '\n'){
            strncpy(GPS_BUF,buf,i);
            i=0;
            GET_GPS_OK=TRUE;
            break;
        }
      }

     if(GET_GPS_OK){
            GET_GPS_OK=FALSE;
            gps_parse(GPS_BUF,&gps_info);
            
        }
 s=1.888888;
  gcvt(s,sig,adn);

q=gps_info.D.year;
w=gps_info.D.month;
e=gps_info.D.day;
r=gps_info.D.hour;
t=gps_info.D.minute;
y=gps_info.D.second;
printf("%s\n",adn);                                                       ----------------------- > 1
printf("%d_%d_%d\n",q,w,e);                                     ----------------------->2
printf("%d_%d_%d\n", r,t,y);                                         --------------------->3
sprintf(zz, "%04d%02d%02d%02d%02d%02d", q, w, e, r,t,y);   
 strcpy( k[i].time,zz);
  strcpy(k[i].space,"          ");
 strcpy( k[i].age,adn);
}
q=gps_info.D.year;
w=gps_info.D.month;
e=gps_info.D.day;
r=gps_info.D.hour;
t=gps_info.D.minute;
y=gps_info.D.second;
sprintf(buff, "/root/%04d.%02d.%02d-%02d.%02d.txt", gps_info.D.year, gps_info.D.month, gps_info.D.day, gps_info.D.hour,gps_info.D.minute);

if((fp = fopen(buff, "a+"))== NULL)
{printf("no open");}

for(ijw = 0; ijw <10; ijw++)
{
     fwrite(k+ijw, sizeof(struct student),1, fp);
     fprintf(fp,"\r\n");
}
    rewind(fp);
    fclose(fp);
      tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */
      tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
      close(fd);
    }
}

1处是由S转换过来的
2,3处的值是通过串口从GPS处得来的.
这123处在计算机终端都正确显示对应的数据.

然后,把这些数据写入文件,打开被写入的文件后,发现全是乱码.

另外,把23处的时间值从系统的时钟获取,再把123处的数据写入文件,打开被写入的文件后,发现是乱码.计算机终端都正确显示对应的数据.
程序如下:
#include<stdio.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdlib.h>
#include <signal.h>
//#define  K  0.003818181



struct student {
            char time[14];
         char space[10];
           char age[5];
          };
static void delay(double);
//char *gcvt(double value,int ndigit,char *buf);



main()
{  
// while(1)
//{

   FILE *fp;
   int i,q,w,e,r,t,y;
    struct student k[10];
   time_t timep;
   struct tm *p;
   char buff[30],zz[14],adn[5];
 //  strcpy(adn[5],"      ");
   int ijw;
    int sig=6;//转换参数
for(i=0;i<10;i++)
{      double s;
       int j,n;
       float hh;
        unsigned  long  m[100],temp;
        int q,fd,count;        //driver is open
        unsigned long result,f=0;
     s=0.1000000;
  gcvt(s,sig,adn);
  
            
time(&timep);
p=gmtime(&timep);
q=(1900+p->tm_year);
w=(1+p->tm_mon);
e=p->tm_mday;
r=p->tm_hour;
t=p->tm_min;
y=p->tm_sec;
printf("%f\n",hh);
printf("%s\n",adn);
printf("%d_%d_%d\n",q,w,e);
printf("%d_%d_%d\n", r,t,y);
sprintf(zz, "%04d%02d%02d%02d%02d%02d", q, w, e, r,t,y);

 strcpy( k[i].time,zz);
  strcpy(k[i].space,"          ");
 strcpy( k[i].age,adn);
  
   sleep(1);
}
time(&timep);
p=gmtime(&timep);
q=(1900+p->tm_year);
w=(1+p->tm_mon);
e=p->tm_mday;
r=p->tm_hour;
t=p->tm_min;

sprintf(buff, "/root/%04d.%02d.%02d-%02d.%02d.txt", q, w, e, r,t);//在C盘tmp目录下保存文件,一定要先建立
                                                                       //文件夹;
if((fp = fopen(buff, "a+"))== NULL)
{printf("no open");}



for(ijw = 0; ijw <10; ijw++)
{
     fwrite(k+ijw, sizeof(struct student),1, fp);
  fprintf(fp,"\r\n");
     //fputc('\n',fp);
}//把信息写入文件
    rewind(fp); //重定位文件
    fclose(fp);
  //  return 0;
   
//}
}




void delay(double i)
{double j,k;
for(j=0;j<i;j++)
{k=0;
}
}

非常困惑,都是一样的数据,一个是正常显示,另一个却是乱码.
请高手帮我指点一下,不盛感谢!
搜索更多相关主题的帖子: C语言 文件 
2007-12-07 08:55
快速回复:[求助]C语言文件写入问题...急...急.......
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017629 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved