如何将文本文件中的下面内容读到 2维字符串数组中,能不能按行和列读
天气 温度 湿度 风强度 打球
sunny hot low weak yes
sunny warm low strong yes
cloud hot low weak yes
cloud warm high strong yes
rain cold high weak yes
rain warm high strong yes大家给写个程序,先谢了

解决方案 »

  1.   

    为什么不定义一个结构呢?
    struct A
    {
       int index;
       CString str_weather;
       CString str_temperature;
       CString str_humidity;
       CString str_wind;
       BOOL b_playbool;
    }*p,a[20];
      

  2.   

    CStdioFile   file;  
       
      CString   strLine;  
       
      if(!file.Open(szFileName,CFile::modeRead,NULL))  
      {  
            char   buf[100];  
            sprintf(buf,"无法打开文件!",szFileName);  
            AfxMessageBox(buf,MB_OK+MB_ICONSTOP,0);  
            return   false;  
      }   
       
      fTxt.SeekToBegin();       //文件指针到头,每次读之前都要这么做  
       
      for(int   i=0;   i<N;   i++)  
       
            fTxt.ReadString(strLine);
      

  3.   

    可以使用fgets从文件中提取出每一行,再根据字符间的空格提取出每个子串,放到二维数组里
      

  4.   

    有点错误,我拷贝来的
    把fTxt全该城file
      

  5.   

    void main(void)
    {
    FILE *fin = 0;                  //文件指针
    char *FileName = "F:\\abc.txt"; //文件路径名
    char *Buff[MAX_LEN];            //指针数组
    char *token;                    //提取出的子串
    char *sep = " ,\t\n";           //根据空格,逗号,TAB等区分子串
    char linebuf[MAX_LEN] = "";     //从文件中读到的每一行
    int  Cnt = 0;                   //计数器 for(Cnt = 0; Cnt < MAX_LEN; Cnt++)
    {
    Buff[Cnt] = NULL;    //初始化指针数组 }

    fin = fopen(FileName, "r");      //打开文件
    if(NULL == fin)
    {
    printf("Can't open file");
    return;
    } Cnt = 0;
    while(NULL != fgets(linebuf, MAXLINE, fin))
    {
    token = strtok(linebuf, sep);
    while(NULL != token)
    {
    Buff[Cnt] = (char*)malloc(strlen(token) + 1);//动态分配
    memset(Buff[Cnt], 0, strlen(token) + 1);
    strcpy(Buff[Cnt], token);//将每一行中的每个子串存到指针数组里

    printf("%s\n", Buff[Cnt]);
    if(NULL != Buff[Cnt])
    {
    free(Buff[Cnt]);//释放
                       Buff[Cnt] = NULL;
    }
    token = strtok(NULL, sep);
    Cnt++;
    }
    }
    return;
    }