| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3336 人关注过本帖
标题:C++中使用结构体读取文件
只看楼主 加入收藏
SagacityWhit
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-5-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
C++中使用结构体读取文件
文本格式:
B= 73.965  L= -29.457
2010  1  4  0: 4:42.000
Altitude, unit: m;    Geopotential height, unit: gpm;  Refractivity, unit: N
 NO          Alt           GeopH        Refrac
  1     127.7539       128.0322    3.001763e+002
    2     257.6292       258.1852    2.954945e+002
    3     386.8093       387.6362    2.909217e+002
    4     515.3259       516.4172    2.864528e+002
    5     643.2098       644.5590    2.820830e+002
    6     770.4980       772.0988    2.778066e+002
    7     897.2189       899.0650    2.736190e+002
    8    1023.3960      1025.4815    2.695166e+002
    9    1149.0560      1151.3749    2.654953e+002
   10    1274.2202      1276.7666    2.615516e+002
   11    1398.9054      1401.6735    2.576829e+002
   12    1523.1306      1526.1148    2.538864e+002
   13    1646.9144      1650.1091    2.501589e+002

需要读取B,L,以及以下的每行数据,求大神们指教!小弟实在不知到错在那里
程序代码:
#include "stdafx.h"
#include "TEST_READ.h"
#include "function.h"
const int MAXNU = 2048;

struct GNSSData
{
    double Latitude_B;
    double Latitude_L;
    char   GNSS_TIME[256];
    char   GNSS_UNIT[256];
    char   GNSS_NU[256];
    struct data{
        double NO;
        double Alt;
        double GeopH;
        double Refrac;
    }data[MAXNU];
}gnssdata;
int main()
{CString file = _T("C:\\Users\\Administrator\\Desktop\\hj.txt");

            const char* test = CStringToCharArray(file);
            FILE* pfile = NULL;

            if (fopen_s(&pfile, test, "r"))
            {
                fprintf_s(stdout, "This file is not opened!\n");
                return 1;
            }
            char LineofStr[256];
            memset(LineofStr, 0x00, sizeof(LineofStr));
            //fgets(LineofStr, sizeof(LineofStr), pfile);
            int st=0,n=0;
            while(4)
            {
                if(fgets(LineofStr, sizeof(LineofStr), pfile)==NULL)
                    break;
                if(st==0)
                {
                    if(sscanf_s(LineofStr,"B= %f",&gnssdata.Latitude_B," L= &f",&gnssdata.Latitude_L)==2)
                        st=1;
                    else if(st==1)
                        if(sscanf_s(LineofStr, gnssdata.GNSS_TIME)==1)
                            st=2;
                        else if(st==2)
                            if(sscanf_s(LineofStr,"Altitude, unit: m;    Geopotential height, unit: gpm;  Refractivity, unit: N \n",gnssdata.GNSS_UNIT )==0)
                                st=3;
                            else if(st==3)
                                if(sscanf_s(LineofStr," NO          Alt           GeopH        Refrac \n ",gnssdata.GNSS_NU)==0)
                                    st=4;
                                else if(st==4)
                                    if(sscanf_s(LineofStr,"%d%4.0f%4.0f%4.0f",&gnssdata.data[n].NO,&gnssdata.data[n].Alt ,&gnssdata.data[n].GeopH,&gnssdata.data[n].Refrac)==4)
                                    {
                                        n++;
                                        if(n>MAXNU||n>sizeof(LineofStr))
                                            break;
                                    }
                }
            }
            fclose(pfile);
            printf_s("gnssdata.data[3].NO= %f",gnssdata.data[n].NO);
}

function.h
程序代码:
#ifndef CStringToCChar_H_H_H_H_H_
#define CStringToCChar_H_H_H_H_H_
char* CStringToCharArray(CString cStr);
#endif

程序代码:
#include "stdafx.h"
#include "function.h"

char* CStringToCharArray(CString cStr)
{
    char *ptr;

#ifdef _UNICODE  
    LONG len;
    len = WideCharToMultiByte(CP_ACP, 0, cStr, -1, NULL, 0, NULL, NULL);
    ptr = new char[len + 1];
    memset(ptr, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, cStr, -1, ptr, len + 1, NULL, NULL);
#else  
    ptr = new char[cStr.GetAllocLength() + 1];
    sprintf(ptr, _T("%s"), cStr);
#endif  

    return ptr;
}



搜索更多相关主题的帖子: 结构体 
2016-05-11 20:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9026
专家分:54030
注 册:2011-1-18
收藏
得分:20 
竟然有中文全角符号,你这代码是梦中写的吧,包括sscanf的格式、if else的匹配等等,不想多说,态度决定一切

给你个参考
程序代码:
#include <stdio.h>

struct GNSSData
{
    double Latitude_B, Latitude_L;
    char   GNSS_TIME[256];
    char   GNSS_UNIT[256];
    char   GNSS_NU[256];
    struct data
    {
        int    NO;
        double Alt;
        double GeopH;
        double Refrac;
    } datas[2048];
};

int main( void )
{
    const char* filename = "a.txt";
    GNSSData gnssdata;
    gnssdata.datas[0].NO = 0;

    FILE* pfile = fopen( filename, "r" );
    if( !pfile )
    {
        puts( "This file is not opened!\n" );
        return 1;
    }

    char line[256];
    if( !fgets(line,sizeof(line)/sizeof(*line),pfile) )
    {
        fclose(pfile);
        return 1;
    }
    if( sscanf(line,"B=%lf L=%lf",&gnssdata.Latitude_B,&gnssdata.Latitude_L) != 2 )
    {
        fclose(pfile);
        return 1;
    }
    if( !fgets(gnssdata.GNSS_TIME,sizeof(gnssdata.GNSS_TIME)/sizeof(*gnssdata.GNSS_TIME),pfile) )
    {
        fclose(pfile);
        return 1;
    }
    if( !fgets(gnssdata.GNSS_UNIT,sizeof(gnssdata.GNSS_UNIT)/sizeof(*gnssdata.GNSS_UNIT),pfile) )
    {
        fclose(pfile);
        return 1;
    }
    if( !fgets(gnssdata.GNSS_NU,sizeof(gnssdata.GNSS_NU)/sizeof(*gnssdata.GNSS_NU),pfile) )
    {
        fclose(pfile);
        return 1;
    }
    for( size_t i=0; i!=sizeof(gnssdata.datas)/sizeof(*gnssdata.datas); ++i )
    {
        gnssdata.datas[i].NO = 0;
        if( 4 != fscanf(pfile,"%d%lf%lf%lf",&gnssdata.datas[i].NO,&gnssdata.datas[i].Alt,&gnssdata.datas[i].GeopH,&gnssdata.datas[i].Refrac) )
            break;
    }

    fclose( pfile );
    return 0;
}

2016-05-12 09:39
快速回复:C++中使用结构体读取文件
数据加载中...
 
   



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

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