[英]warning: implicit declaration of function 'getline'
Hi I'm almost done with a project I have for class.嗨,我几乎完成了课堂上的一个项目。 I need to sort the priority of people in an airline based on a few factors.
我需要根据几个因素对航空公司中人员的优先级进行排序。 This is my project description:
这是我的项目描述:
"An airline company uses the formula shown below to determine the priority of passengers on the waiting list for overbooked flights. Priority Number = (A / 1000) + B - C where A is the customer's total mileage in the past year B is the number of years in his or her frequent flier program C is a sequence number representing the customer's arrival position when he or she booked the flight. Given a file of overbooked customers as shown in the table below, write a program that reads the file and determines each customer's priority number. The program then builds a priority queue using the priority number and prints a list of waiting customers in priority sequence." “一家航空公司使用以下公式来确定超额预订航班候补名单上乘客的优先级。优先级编号 = (A / 1000) + B - C 其中 A 是客户过去一年的总里程 B 是编号在他或她的常旅客计划中的年数 C 是一个序列号,代表客户在他或她预订航班时的到达位置。给定一个如下表所示的超额预订客户的文件,编写一个程序来读取文件并确定每个客户的优先级编号。然后程序使用优先级编号构建一个优先级队列,并按优先级顺序打印等待客户的列表。”
I feel my code encompasses the main idea, but I am having issues with getline.我觉得我的代码包含了主要思想,但我在使用 getline 时遇到了问题。 When I compile the code (shown below) it gives me an error: "|24|warning: implicit declaration of function 'getline' [-Wimplicit-function-declaration]|"
当我编译代码(如下所示)时,它给了我一个错误:“|24|警告:函数‘getline’的隐式声明[-Wimplicit-function-declaration]|”
Please help me fix this so it will compile.请帮我解决这个问题,以便它可以编译。 I've tried a lot of things and nothing works.
我尝试了很多东西,但没有任何效果。
Here is the code:这是代码:
#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++]='\0';
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]!='\0')
{
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\n",name[i],priority[i]);
}
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
or或者
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
should get you getline
on GNU systems, but there might be another way to enable it on a different POSIX system.应该让您在 GNU 系统上获得
getline
,但可能还有另一种方法可以在不同的 POSIX 系统上启用它。
After enabling getline
like that, the len
variable should be typed size_t
, not int
so that &len
is correctly typed as size_t *
.像这样启用
getline
后,应该将len
变量输入size_t
,而不是int
以便&len
正确输入为size_t *
。
It is possible that your project testing software doesn't support the getline
POSIX function in which case you might need to rethink your design in terms of standard C functions instead.您的项目测试软件可能不支持
getline
POSIX 函数,在这种情况下,您可能需要根据标准 C 函数重新考虑您的设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.