鱼C论坛

 找回密码
 立即注册
查看: 910|回复: 2

[已解决]为什么打不出来数据呢

[复制链接]
发表于 2020-11-28 22:06:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <string.h>
#define MAXSTR 100
int income_lev, year; // to record which year and income level does user choice//
int country_num = 0, data_num = 0;

struct Country
{
        char country_name[52];
        char income_level[8];
        char region[27];
        struct Data
        {
                char year[6];
                char percent[5];
        } data[34];
} country[200];


void user_choice()// let user to choose the year and income level they want//
{
        printf("Please enter a year which you want to search:\n");
        scanf("%d", &year);
        printf("Please enter an income level which you want to search\n\
        1 corresponds to 'low income'\n\
        2 corresponds to 'lower middle income'\n\
        3 corresponds to 'upper middle income'\n\
        4 corresponds to 'high income'\n");
        scanf("%d", &income_lev);
}

void record_data(char line[100])
{
        int i;
       
        for(i=0; i<51; i++)
        {
                country[country_num].country_name[i] = line[i];
        }
        for(i=51; i<58; i++)
        {
                country[country_num].income_level[i - 51] = line[i];
        }
        for(i=59; i<62; i++)
        {
                country[country_num].data[data_num].percent[i - 59] = line[i];
        }
        for(i=62; i<87; i++)
        {
                country[country_num].region[i - 62] = line[i];
        }
        for(i=88; i<92; i++)
        {
                country[country_num].data[data_num].year[i-88] = line[i];
        }

        if (strcmp(country[country_num].country_name, country[country_num - 1].country_name) != 0 || country_num == 0)
                {
                        country_num += 1;
                        data_num = 0;
                        //printf("C:%d\n",country_num);
                }
        else
                {
                        data_num += 1;
                        //printf("D:%d\n",data_num);
                }               
}

int main ()
{
   FILE *fp;
   char str[MAXSTR];
   int i=1;
   

   fp = fopen("measles.txt" , "r");
   if(fp == NULL)
   {
                perror("Error opening file");
                return(-1);
   }
   
   while( fgets (str, MAXSTR, fp)!=NULL )
   {
                   //printf("%s", str);
                   record_data(str);
   }

   fclose(fp);
   
   //user_choice();
   /*for(i=0; i<200; i++)
    {
            puts(country[i].country_name);
            puts(country[i].region);
            puts(country[i].income_level);
                   puts(country[i].data[1].year);
                   puts(country[i].data[1].percent);
        }*/
        for(i=0; i<33; i++)
        {
                printf("%s %s\n", country[0].data[2].year, country[0].data[2].percent);
        }
        return(0);
}
最佳答案
2020-11-28 23:02:24

第 77 行:fp = fopen(measles.txt,"r");  // 文件名不能作为参数, 并且 measles.txt 的文件路径要正确
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-28 22:09:01 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXSTR 100
  4. int income_lev, year; // to record which year and income level does user choice//
  5. int country_num = 0, data_num = 0;

  6. struct Country
  7. {
  8.         char country_name[52];
  9.         char income_level[8];
  10.         char region[27];
  11.         struct Data
  12.         {
  13.                 char year[6];
  14.                 char percent[5];
  15.         } data[34];
  16. } country[200];


  17. void user_choice()// let user to choose the year and income level they want//
  18. {
  19.         printf("Please enter a year which you want to search:\n");
  20.         scanf("%d", &year);
  21.         printf("Please enter an income level which you want to search\n\
  22.         1 corresponds to 'low income'\n\
  23.         2 corresponds to 'lower middle income'\n\
  24.         3 corresponds to 'upper middle income'\n\
  25.         4 corresponds to 'high income'\n");
  26.         scanf("%d", &income_lev);
  27. }

  28. void record_data(char line[100])
  29. {
  30.         int i;
  31.        
  32.         for(i=0; i<51; i++)
  33.         {
  34.                 country[country_num].country_name[i] = line[i];
  35.         }
  36.         for(i=51; i<58; i++)
  37.         {
  38.                 country[country_num].income_level[i - 51] = line[i];
  39.         }
  40.         for(i=59; i<62; i++)
  41.         {
  42.                 country[country_num].data[data_num].percent[i - 59] = line[i];
  43.         }
  44.         for(i=62; i<87; i++)
  45.         {
  46.                 country[country_num].region[i - 62] = line[i];
  47.         }
  48.         for(i=88; i<92; i++)
  49.         {
  50.                 country[country_num].data[data_num].year[i-88] = line[i];
  51.         }

  52.         if (strcmp(country[country_num].country_name, country[country_num - 1].country_name) != 0 || country_num == 0)
  53.                 {
  54.                         country_num += 1;
  55.                         data_num = 0;
  56.                         //printf("C:%d\n",country_num);
  57.                 }
  58.         else
  59.                 {
  60.                         data_num += 1;
  61.                         //printf("D:%d\n",data_num);
  62.                 }               
  63. }

  64. int main ()
  65. {
  66.    FILE *fp;
  67.    char str[MAXSTR];
  68.    int i=1;
  69.    

  70.    fp = fopen("measles.txt" , "r");
  71.    if(fp == NULL)
  72.    {
  73.                 perror("Error opening file");
  74.                 return(-1);
  75.    }
  76.    
  77.    while( fgets (str, MAXSTR, fp)!=NULL )
  78.    {
  79.                    //printf("%s", str);
  80.                    record_data(str);
  81.    }

  82.    fclose(fp);
  83.    
  84.    //user_choice();
  85.    /*for(i=0; i<200; i++)
  86.     {
  87.             puts(country[i].country_name);
  88.             puts(country[i].region);
  89.             puts(country[i].income_level);
  90.                    puts(country[i].data[1].year);
  91.                    puts(country[i].data[1].percent);
  92.         }*/
  93.         for(i=0; i<33; i++)
  94.         {
  95.                 printf("%s %s\n", country[0].data[2].year, country[0].data[2].percent);
  96.         }
  97.         return(0);
  98. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-28 23:02:24 | 显示全部楼层    本楼为最佳答案   

第 77 行:fp = fopen(measles.txt,"r");  // 文件名不能作为参数, 并且 measles.txt 的文件路径要正确
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 04:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表