我是C编程的新手,我想将* .data文件读入动态扩展的结构数组中。一旦完成,我想按值排序并按高/低顺序打印。我已经能够完成Sort&打印功能,但不是文件的READING。 我已经完成了几个例子,但无法理解。任何帮助,将不胜感激。感谢
文件中的示例数据
struct dataBase
{
char modelName[31];
float capacity;
int mileage;
char color[15];
};
int main (void)
{
readData();
// set up conditions for the loop
int choice;
choice = 0;
// I used a do while loop to insure that it would run once
do{
// print menu
printf ( "Make your choice.\n");
printf ( "1. Sort data by the float value & print high to low.\n" );
printf ( "2. Sort data by the float value & print low to high.\n" );
printf ( "3. Sort data by the int value & print high to low.\n" );
printf ( "4. Sort data by the int value & print low to high.\n");
printf ( "5. Exit.\n\n");
// get user input and clear out stdin
choice = getchar();
// this part clears the stdin of leftovers
while ( getchar() != '\n' ) ;
// Call the desired function
if (choice == '1' ) {
compareFloat();
printHigh(Input_Size);
}
else if (choice == '2' ) {
compareFloat();
printLow(Input_Size);
}
else if (choice == '3' ) {
compareInt();
printHigh(Input_Size);
}
else if (choice == '4') {
compareInt(Input_Size);
}
}
// Check for escape code
while ( choice != '5');
// just to verify I finished
printf ( "Done\n");
return 0;
}
int compareFloat(const void *s1, const void *s2)
{
struct dataBase *e1 = (struct dataBase *)s1;
struct dataBase *e2 = (struct dataBase *)s2;
if (e1->capacity < e2->capacity)
return -1;
else if (e1->capacity > e2->capacity)
return +1;
else
return 0;
}
void printLow(int size)
{
for(int i=0; i<size; i++)
{
printf(dataBase[i].modelName + " " + dataBase[i].capacity + " " +
dataBase[i].mileage + " " + dataBase[i].color);
}
}
void readData(void)
{
FILE *inputFile
inputFile = fopen("file.data", 'rb');
}
答案 0 :(得分:1)
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct dataBase
{
char modelName[31];
float capacity;
int mileage;
char color[15];
};
struct dataBase *Inputs = NULL;
size_t Inputs_Size = 0;
size_t Num_Inputs = 0;
void printLow(int size)
{
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n", Inputs[i].modelName, Inputs[i].capacity, Inputs[i].mileage, Inputs[i].color);
}
}
void readData(void)
{
FILE *inputFile;
struct dataBase input;
if (NULL == (inputFile = fopen("file.data", "r")))
{
perror("Couldn't open file.data!");
return;
}
while (4 == fscanf(inputFile, "%30s %f %d %14s", input.modelName, &input.capacity, &input.mileage, input.color))
{
if (++Num_Inputs > Inputs_Size)
{
size_t new_size = 2 * Num_Inputs;
struct dataBase *new_inputs = realloc(Inputs, new_size * sizeof(struct dataBase));
if (NULL == new_inputs)
{
perror("realloc failed!");
goto FAIL_INPUTFILE;
}
Inputs = new_inputs;
Inputs_Size = new_size;
}
Inputs[Num_Inputs - 1] = input;
printf("'%s' %f %d '%s'\n", input.modelName, input.capacity, input.mileage, input.color);
}
if (!feof(inputFile))
{
fprintf(stderr, "Malformed line didn't match expected input format!\n");
}
FAIL_INPUTFILE:
fclose(inputFile);
}
int main()
{
readData();
return 0;
}