首先,我想向大家介绍一下自己,因为我是论坛的新手;我的名字是 Max,我是 C 的初学者。我无法创建一个可以执行以下操作的程序:
- 为汽车数据创建一个数组。
- 为鸡肉数据创建三个并行数组,读取时跳过输入文件中的行。
- 为污染数据创建一个字符和整数数组,最后
- 允许用户选择如何显示数据(升序或降序)。
数据可以在下面找到:
https://github.com/untamedspud/Lab-Assignment-8-data-files/blob/master/motorway.txt
https://github.com/untamedspud/Lab-Assignment-8-数据文件/blob/master/pollution.txt
我非常感谢你们能给我的任何帮助,如果您有任何问题,请告诉我。
这是我当前的非功能代码,该代码将从文本文件中读取数据并将数据存储到适当的数组中:
#include <stdio.h>
#include <stdlib.h>
#include "/home/TU/tue44432/include/selectionSort.h"
// Using a C global constant variable.
// The only global variables you can use in your program are const.
const char *filename = "pollution.txt";
int main()
{
FILE *F; // C file structure
int i = 0;
int level[30];
int pl; // holds the first value we read in from the file
char city[30]; // holds the second value we read in from the file
// associate the internal file name, F, with the external file name, pop.txt.
F = fopen( filename, "r" );
fscanf( F, "%d ", &pl );
fgets( city, 30, F );
chomp( city ); // removes the newline at the end of the line read in
level[0] = pl;
while ( ! feof( F ) ) {
// printf("Pollution level is %d in %s.\n", pl, city);
printf("Pollution level is %d in %s.\n", level[i], city);
fscanf( F, "%d", &pl ); // scan in the first value from the file
fgets( city, 30, F ); // reads in UP TO 30 characters, stopping at the
chomp( city );
i += 1;
level[i] = pl;
// printf("loop controll variable i == %d.\n", i);
}
fclose( F );
return EXIT_SUCCESS;
}