警告:函数'getline&#39的隐式声明; [英] warning: implicit declaration of function 'getline'

查看:18
本文介绍了警告:函数'getline&#39的隐式声明;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨,我的课上要做的项目快做完了。我需要根据几个因素对航空公司乘客的优先顺序进行排序。这是我的项目描述:

航空公司使用下面所示的公式来确定乘客在 超额预定航班的等候名单。 优先级数=(A/1000)+B-C 哪里 A是客户过去一年的总里程数 B是他或她的飞行常客计划的年数 C是表示客户预订时到达位置的序列号 飞行。 给出一个超额预订客户的文件,如下表所示,编写一个程序来读取 文件,并确定每个客户的优先级号。然后,该程序建立一个优先级队列 使用优先级编号,并按优先级顺序打印等待客户的列表。"

我觉得我的代码包含了主要思想,但我在getline方面遇到了问题。当我编译代码时(如下所示),它给我一个错误: "|24|警告:函数‘getline’的隐式声明[-WIMPLICIT-Function-DECLANATION]|"

请帮我解决这个问题,这样它才能编译。我试了很多方法,但都不管用。

代码如下:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE * fp; //create file pointer to be read later
    char * line = NULL;
    int temp,count=0,len=0,mileage[100],read,years[100],sequence[100],priority[100];
    char name[100][100],tempst[100];
    fp = fopen("customers.txt", "r"); //opening the file

    if (fp == NULL)
        exit(EXIT_FAILURE);
    int i;



    while ((read = getline(&line, &len, fp)) != -1) { //start reading file and recording data
        i=0;

        while(line[i]!=' ' || (line[i+1]>='A' && line[i+1]<='z'))
        {
            name[count][i]=line[i];
            i++;
        }
        name[count][i++]='';
        mileage[count]=0;

        while(line[i]!=' ')
        {
            mileage[count]=mileage[count]*10+(line[i]-'0');
            i++;
        }
        i++;
        years[count]=0;

        while(line[i]!=' ')
        {
            years[count]=years[count]*10+(line[i]-'0');
            i++;
        }
        i++;
        sequence[count]=0;

        while(line[i]!='')
        {
            sequence[count]=sequence[count]*10+(line[i]-'0');
            i++;
        }
        priority[count]=(mileage[count]/1000)+years[count]-sequence[count];
        count++;

    }

    for( i=0;i<count-1;i++) // calculate priority
    {
        for(int j=i+1;j<count;j++)
        {
            if(priority[i]<priority[j])
            {
                temp=priority[i];
                priority[i]=priority[j];
                priority[j]=temp;
                strcpy(tempst,name[i]);
                strcpy(name[i],name[j]);
                strcpy(name[j],tempst);
            }
        }
    }

    for( i=0;i<count;i++) //print priority
    {
        printf("%s %d
",name[i],priority[i]);
    }
    return 0;
}

推荐答案

#define  _GNU_SOURCE
#include <stdio.h>

#define  _POSIX_C_SOURCE 200809L
#include <stdio.h>

应该可以在GNU系统上启用getline,但可能还有其他方法在不同的POSIX系统上启用它。

像这样启用getline后,len变量的类型应该是size_t,而不是int,这样&len才能正确地类型为size_t *

您的项目测试软件可能不支持getlinePOSIX函数,在这种情况下,您可能需要改用标准C函数重新考虑您的设计。

这篇关于警告:函数&#39;getline&#39的隐式声明;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆