c - 对包含从 C 中的文件输入的数字的字符串进行排序?

标签 c sorting io

所以我正在开发一个程序,该程序从文件中读取包含每行/项目的“商品编号”、“单价”和“购买日期”的行。我已经做到了可以扫描文件并以所需的图表格式组织它,但我不知道如何按“项目编号”对数据进行排序。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main() {

FILE *fp;

char ch;

fp = fopen("f.txt", "r"); //open the file named f.txt
if (fp == NULL) //In case we can't find the file, notify the user
    printf("File not found\n");

printf("Item \t\tUnit Price\tPurchase Date\n"); //set up the header

while ((ch = fgetc(fp)) != EOF) { //set the character equal to the character next in the file using fgetc, and
                                //if its not equal to the end of file
    if (ch == ',') {
        printf("\t\t"); //add two tabs every time a ',' is encountered.
    }
    else {
        printf("%c",ch); //just display the output from the file
    }
}

fclose(fp); //closes the file

return 0;
}

示例输入

enter image description here

示例输出

enter image description here

看,我需要按项目编号(最左边的列)对输出进行排序。 我的想法是将每一行添加到一个字符串数组(c 中的字符数组)中,然后从那里我不知道如何识别项目编号,以便对输出进行排序。我对 fscanf 有点熟悉,但不知道如何在这里应用它。 非常感谢任何帮助,谢谢。

最佳答案

好吧,我相信将其分解为几个步骤会很好。

  1. 定义特定于您的数据的结构
  2. 逐行读取文件
  3. 对于每一行,使用 strtok()分割字符串
  4. 将这些值添加到结构数组中
  5. 使用qsort()对结构数组进行排序

如果您不熟悉 strtok() 和 qsort() 函数,我强烈建议您阅读更多有关它们的内容。

这是代码:

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

// STEP 1
typedef struct
{
    int item;
    float price;
    char date[50];
} Data;

int compare (const void * a, const void * b)
{

  Data *dataA = (Data *)a;
  Data *dataB = (Data *)b;

  return ( dataA->item - dataB->item );  // Ascending order
//return ( dataB->item - dataA->item );  // Descending order
}

int main()
{
  FILE * fp;
  char line[150];
  Data data[3];
  int i = 0;

  fp = fopen("f.txt", "r");
  while (1)
  {
    // STEP 2
    if (fgets(line,150, fp) == NULL) break;

    // STEP 3-4
    char * pch;
    pch = strtok (line,",");
    data[i].item = atoi(pch);  // item part
    pch = strtok (NULL, ",");
    data[i].price = atof(pch); // unit price part
    pch = strtok (NULL, ",");
    strcpy(data[i].date, pch); // purchase date part

    i++;
  }

  printf("##### BEFORE #####\n");
  printf("Item \t\tUnit Price\tPurchase Date\n"); //set up the header
  for (int k = 0; k < 3; k++)
  {
    printf("%d\t\t%f\t%s", data[k].item, data[k].price, data[k].date);
  }

  // STEP 5
  qsort (data, 3, sizeof(Data), compare);

  printf("\n##### AFTER #####\n");
  printf("Item \t\tUnit Price\tPurchase Date\n"); //set up the header
  for (int k = 0; k < 3; k++)
  {
    printf("%d\t\t%f\t%s", data[k].item, data[k].price, data[k].date);
  }

  fclose(fp);
  return 0;
}

请注意,此代码仅为您的测试用例而编写。如果您的数据结构不同(如果您有超过三列等),您需要修改代码。此外,您还需要进行更多的错误处理。

这是输出:

##### BEFORE #####
Item        Unit Price  Purchase Date
583         13.500000   10/24/2005
3912        599.989990  7/27/2008
12          19.990000   3/16/2001

##### AFTER #####
Item        Unit Price  Purchase Date
12          19.990000   3/16/2001
583         13.500000   10/24/2005
3912        599.989990  7/27/2008

希望这有帮助。

巴里斯

关于c - 对包含从 C 中的文件输入的数字的字符串进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47358051/

相关文章:

c - Glib 信号 - 如何检查实例的处理程序是否已被阻止?

c - 动态数组分配和复制到更大的

list - 如何按另一个列表对列表进行排序

go - 如何通过从另一个模块 B 调用模块的 A 函数来读取位于模块 A 中的静态文件?

c++ - 以固定速率重播存储的数据

c - 检查宏是否定义

c - 如何读取消息队列中的动态消息

string - Groovy:如何按字符串长度顺序对 String:s 的 ArrayList 进行排序?

java - 如何按字母顺序对列表列表的第一个元素进行排序?

Haskell 编译 IO-Action 顺序和刷新