c - 要进一步计算读取和存储数据文​​件 [英] C - read and store data file for further calculations

查看:104
本文介绍了c - 要进一步计算读取和存储数据文​​件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用R,并有大量无法理解的C.我需要的读取和存储一个数据文件,如下图所示,这样我就可以对数据进行计算。这些计算依赖于用户输入的信息来源。下面是我试图读取数据(在我的code称为下载/ exchange.dat),

I normally use R, and have a lot of trouble understanding C. I need to read and store a data file, shown below, so that I can perform calculations on the data. These calculations depend on user-entered infomation. Here's the data that I'm trying to read in (called "Downloads/exchange.dat" in my code),

dollar   1.00
yen      0.0078
franc    0.20
mark     0.68
pound    1.96

下面是我在哪里为止。这仅读取数据的第一行,并返回它,这不是我想要的。我需要阅读这整个文件,存储汇率各自的货币,并能够在以后对其进行计算。

Here's where I'm at so far. This reads the first line of data only and also returns it, which is not what I want. I need to read this entire file, store the exchange rates with their respective currencies, and be able to perform calculations on them later.

我想我需要一个的typedef ,也许?后来在节目中,我要求像信息的用户的转换?和转换为?在这种情况下,他们将进入标记,日元,美元,等我希望,以配合其使用各自的汇率响应(文件string.h 库)。

I think I need a typedef, perhaps? Later in the program, I ask the user for information like "Convert from?" and "convert to?". In which case, they'll enter "mark", "yen", "dollar", etc. and I hope to match their response to the respective exchange rate (using the string.h library).

我的code,到目前为止,在数据读取:

My code so far, to read in the data:

#include <stdio.h>

main(int argc, char *argv[])
{
    FILE *fpt;  // define a pointer to pre-defined structure type FILE
    char c;

    // open the data file for reading only
    if ((fpt = fopen("Downloads/exchange.dat", "r")) == NULL)
        printf("\nERROR - Cannot open the designated file\n");

    else   // read and display each character from the data file
        do
            putchar(c = getc(fpt));
        while (c != '\n');

    // close the data file
    fclose(fpt);
}

我觉得我需要类似的东西,而是完全不同的读取和存储整个数据文件。你的帮助是AP preciated。

I feel like I need something similar, but completely different to read and store entire data file. Your help is appreciated.

推荐答案

您需要创建一个数据结构来保存它们,然后这个结构将举行各货币阅读的载体。你应该利用的fscanf 功能,不仅将通过拆分值拯救你手,但也将转换成它们。这里是我想出了:

You'll need to create a data structure to hold them and then a vector of this struct which will hold each currency read. You should make use of the fscanf function that will not only save you from splitting values by hand but will also convert them for you. Here is something I came up with:

/* to store each currency you read */
struct currency {
  char name[256];
  double value;
};

int main(int argc, char ** argv) {
  FILE * fp = fopen("x.dat", "r");
  int count = 0, i;
  struct currency currencies[30];

  /* this will read at most 30 currencies, and stop in case a
   * end of file is reached */
  while (count < 30 && !feof(fp)) {
    /* fscanf reads from fp and returns the amount of conversions it made */
    i = fscanf(fp, "%s %lf\n", currencies[count].name, &currencies[count].value);

    /* we expect 2 conversions to happen, if anything differs
     * this possibly means end of file. */
    if (i == 2) {
      /* for the fun, print the values */
      printf("got %s %lf\n", currencies[count].name, currencies[count].value);
      count++;
    }
  }
  return 0;
}

要再次阅读它们,你需要在货币迭代阵列,直到你达到计数迭代。

To read them again, you'll need to iterate on the currencies array until you reach count iterations.

既然你已经想和 STRCMP 功能匹配值,读取货币名称,遍历数组,直到找到匹配,然后执行这些计算。

Since you already wants to match those values with the strcmp function, read the currency name, iterate on the array until you find a match and then perform calculations on those.

这是基本的C知识并不亚于我理解你不习惯用它,我强烈建议你的看书在其中找到这些问题的答案。

This is basic C knowledge and as much as I understand you're not used to using it, I strongly suggest you read a book to find these answers in them.

这篇关于c - 要进一步计算读取和存储数据文​​件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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