对包含从C中的文件输入的数字的字符串进行排序? [英] Sorting string containing numbers entered from a file in C?

查看:129
本文介绍了对包含从C中的文件输入的数字的字符串进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个程序,该程序从包含每个行/项目的项目编号,单价和购买日期的文件中读取行。我已经达到了可以扫描文件并以所需的图表格式组织它的程度,但我无法弄清楚如何按项目编号对数据进行排序。

So I'm working on a program that reads lines from a file containing an "item number", "unit price" and "purchase date" for each line / item. I've made it to the point where I can scan the file and organize it in the chart format required, but I can't figure out how to sort the data by "Item number".

这是我的代码:

#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;
}

样本输入

示例输出

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

See, I need the output to be sorted by Item number (the far left column). My idea was to add each line to a string array (char array in c), then from there I don't know how to identify the item number, in order to sort it for the output. I'm slightly familiar with fscanf, but do not know how to apply it here. Any help is greatly appreciated, thank you.

推荐答案

好的,我相信打破这个会很好进行一些步骤。

Okay, I believe it would be nice to break this down into some steps.


  1. 定义特定于您的数据的结构

  2. 读取文件行按行

  3. 对于每一行,请使用 strtok()拆分字符串

  4. 将这些值添加到struct数组中

  5. 使用 qsort()对结构数组进行排序

  1. Define a struct that is specific for your data
  2. Read a file line by line
  3. For each line, use strtok() to split the string
  4. Add these values into your array of struct
  5. Use qsort() to sort your array of struct

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

I highly recommend you read more about strtok() and qsort() functions if you are not familiar with them.

以下是代码:

#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;
}

请注意,此代码仅针对您的测试用例编写。如果您的数据结构不同(如果您有三列以上等),则需要修改代码。另外,你需要做更多的错误处理。

Please note that this code is written for your test case only. If your data structure is different (if you have more than three columns etc.) you need to modify the code. Also, you are gonna need to do some more error-handling.

这是输出:

##### 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

希望这会有所帮助。

Baris

这篇关于对包含从C中的文件输入的数字的字符串进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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