无法使用c中的fprintf将数据保存在txt文件中

时间:2016-08-03 20:56:50

标签: c printf

我有两个FILE变量可以在两个.txt不同的文件中写入数据。我的问题是数据只存储在一个文件中。代码很长但我只遇到“fprintf(Ublox_TXT,...)”的问题。当我打开文件“Ublox.txt”时,它是空白的,我不知道为什么,因为另一个文件(Xsens.txt)具有必须存储的数据。它编译时没有错误或警告。

main()
{
FILE *Xsens_text;
FILE *Ublox_text;
int Xsens_UART, Ublox_UART;

Xsens_text=fopen("Xsens.txt","w");
Ublox_text=fopen("Ublox.txt","w");

int i,j,k,w,wa,z,cont,TS,TH_pasos;
unsigned char Buffer_Lectura_XS[100];
unsigned int pc_XS;
float q0_XS,q1_XS,q2_XS,q3_XS,acx_XS,acy_XS,acz_XS,grx_XS,gry_XS,grz_XS;
int cont_XS=0,a=0;
int N_XS;
unsigned char Buffer_Lectura_UB[255], Buffer_Auxiliar_UB[255],Buffer_Auxiliar2_UB[255];
int ACK_UB=0, Cont_UB=0, Cont2_UB=0, Bandera2=0;
int Num_Bytes_UB, Bandera1;
float X_ECEF, Y_ECEF;
struct Medidas_UB
{
    char valido;
    char Norte;
    char Este;
    float longitud; 
    float latitud;
};
struct Medidas_UB medidas;

struct XSENS_message Buf_Lect1_XS=GoToConfig(Xsens_UART); 
printf("Configurando Unidad de XSENS... \n");
struct XSENS_message Buf_Lect2_XS=SetOutputConfiguration(Xsens_UART);
printf("Unidad XSENS configurada correctamente \n");

struct UBX_message Buf_Lect1_UB=UBX_NMEA_CFG(Ublox_UART); 

struct XSENS_message Buf_Lect3_XS=GoToMeasurement(Xsens_UART);
k=1;
while(k<3)   
{
    struct XSENS_message Buf_Lect4_XS=ReqData(Xsens_UART);      
    for(i=0;i<256;i++);
    k=k+1;
}
TS=1; 
while(1) 
{
    cont=1; 
    while(cont<51)
    {

        struct XSENS_message Buf_Lect5_XS=ReqData1(Xsens_UART); 
        for(i=0;i<Buf_Lect5_XS.NumBytes;i++) 
        {
            Buffer_Lectura_XS[i]=*(Buf_Lect5_XS.Buffer_Lectura+i);
        }
        if (Buffer_Lectura_XS[3]!=0x45){a=1;}
        if (a==0) 
        {
            q0_XS=ConcXsens(Buffer_Lectura_XS[7],Buffer_Lectura_XS[8],Buffer_Lectura_XS[9],Buffer_Lectura_XS[10]);
            q1_XS=ConcXsens(Buffer_Lectura_XS[11],Buffer_Lectura_XS[12],Buffer_Lectura_XS[13],Buffer_Lectura_XS[14]);
            q2_XS=ConcXsens(Buffer_Lectura_XS[15],Buffer_Lectura_XS[16],Buffer_Lectura_XS[17],Buffer_Lectura_XS[18]);
            q3_XS=ConcXsens(Buffer_Lectura_XS[19],Buffer_Lectura_XS[20],Buffer_Lectura_XS[21],Buffer_Lectura_XS[22]);
            acx_XS=ConcXsens(Buffer_Lectura_XS[26],Buffer_Lectura_XS[27],Buffer_Lectura_XS[28],Buffer_Lectura_XS[29]);
            acy_XS=ConcXsens(Buffer_Lectura_XS[30],Buffer_Lectura_XS[31],Buffer_Lectura_XS[32],Buffer_Lectura_XS[33]);
            acz_XS=ConcXsens(Buffer_Lectura_XS[34],Buffer_Lectura_XS[35],Buffer_Lectura_XS[36],Buffer_Lectura_XS[37]);
            grx_XS=ConcXsens(Buffer_Lectura_XS[56],Buffer_Lectura_XS[57],Buffer_Lectura_XS[58],Buffer_Lectura_XS[59]);
            gry_XS=ConcXsens(Buffer_Lectura_XS[60],Buffer_Lectura_XS[61],Buffer_Lectura_XS[62],Buffer_Lectura_XS[63]);
            grz_XS=ConcXsens(Buffer_Lectura_XS[64],Buffer_Lectura_XS[65],Buffer_Lectura_XS[66],Buffer_Lectura_XS[67]);
            pc_XS=ConcPC(Buffer_Lectura_XS[71],Buffer_Lectura_XS[72]);
            fprintf(Xsens_text,"%lf \t %lf \t %lf \t %lf \t %lf \t %lf \t %lf \t %lf \t %lf \t %lf \t %d \t %d \n",q0_XS,q1_XS,q2_XS,q3_XS,acx_XS,acy_XS,acz_XS,grx_XS,gry_XS,grz_XS,pc_XS,TS); 
            }
        }
        else 
        {
            N_XS=Buffer_Lectura_XS[3];
            N_XS=N_XS+5;
            struct XSENS_message Buf_Lect7_XS=ReqData2(Xsens_UART,N_XS);    
            a=0;
        }
    cont=cont+1;
    }

        Num_Bytes_UB=read(Ublox_UART,Buffer_Lectura_UB,255); //Se lee el GPS

        for(i=0;i<Num_Bytes_UB+1;i++) //Recorrer el Buffer donde está almacenados los datos leidos
        {
            if((Buffer_Lectura_UB[i]=='$')&&(Buffer_Lectura_UB[i+3]=='R')&&(Buffer_Lectura_UB[i+4]=='M')&&(Buffer_Lectura_UB[i+5]=='C')) 
            {
                k=0;
                j=i;
                while(k==0) 
                {
                    if(Buffer_Lectura_UB[j-2]=='*'){k=1;}
                    j=j+1;
                    if(j==255){j=0;}
                }
                for(w=0;w<256;w++){Buffer_Auxiliar_UB[w]=0;} 
                for(w=0;w<(j-i+1);w++){Buffer_Auxiliar_UB[w]=Buffer_Lectura_UB[i+w];} 
                Bandera2=0;                     
                for(w=0;w<256;w++) 
                {
                    if(Buffer_Auxiliar_UB[w]==',') 
                    {
                        Bandera2=Bandera2+1;
                        wa=w+1;
                        k=0;
                        Bandera1=0;
                        for(j=0;j<256;j++){Buffer_Auxiliar2_UB[j]=0;}  
                        while(Bandera1==0) 
                        {
                            if(Buffer_Auxiliar_UB[k+wa]==','){Bandera1=1;}
                            if(Bandera1!=1)
                            {
        Buffer_Auxiliar2_UB[k]=Buffer_Auxiliar_UB[wa+k];
                            k=k+1;
                            }
                        }
                        if(Bandera2==2){medidas.valido=Buffer_Auxiliar2_UB[0];} 
                        if(Bandera2==3){medidas.latitud=atof(Buffer_Auxiliar2_UB);}
                        if(Bandera2==4){medidas.Norte=Buffer_Auxiliar2_UB[0];}
                        if(Bandera2==5){medidas.longitud=atof(Buffer_Auxiliar2_UB);}
                        if(Bandera2==6){medidas.Este=Buffer_Auxiliar2_UB[0];}
                    }
                }
                X_ECEF=X_transform(medidas.latitud,medidas.longitud);
                Y_ECEF=Y_transform(medidas.latitud,medidas.longitud);
                printf("Sensor Ublox: \n");
                printf("X= %lf \t Y= %lf \n",X_ECEF,Y_ECEF);
                fprintf(Ublox_text,"%lf \t %lf \n",X_ECEF,Y_ECEF); //Guarda datos del GPs en un archivo .txt
            }
        }

TS=TS+1;
}

fclose(Xsens_text);
fclose(Ublox_text); 

tcsetattr(Xsens_UART, TCSANOW, &oldtio_XS);
tcsetattr(Ublox_UART, TCSANOW, &oldtio_UB);
}

1 个答案:

答案 0 :(得分:1)

您的阵列太小了。像

这样的数组
unsigned char Buffer_Auxiliar_UB[255];

只能编入[254]

的索引

但这一行

for(w=0;w<256;w++){Buffer_Auxiliar_UB[w]=0;}

打破了数组。它应该是

unsigned char Buffer_Auxiliar_UB[256];

可能在其他地方,其他阵列也是如此。