从文件到数组阅读 - Ç [英] Reading from a File to an Array - C

查看:117
本文介绍了从文件到数组阅读 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT的main()
{    FILE * infile1;    INT股票code [15];
    炭stockName [100];
    INT stockQuantity [15];
    INT stockReorder [15];
    INT单价[15];
    INT I;    infile1 = FOPEN(NUSTOCK.TXT,R);    而(的fscanf(infile1,%d个%S%D%F
         &安培;股票code,stockName,&安培; stockQuantity,&安培; stockReorder,&安培;!单价)= EOF)
    {
        的printf(%3D%-18s%3D%3D%6.2f \\ n
          股票code,stockName,stockQuantity,stockReorder,单价);
    }    FCLOSE(infile1);}

我想要做的就是从文件中获取信息并存储到5个单独的阵列。但是,打印出的时候,只打印出了正确的名称。


  

1394854864西梅篮1394854688 1394854624 0.00

  1394854864梨篮1394854688 1394854624 0.00

  1394854864桃篮1394854688 1394854624 0.00

  1394854864豪华塔式1394854688 1394854624 0.00


原始文件看起来是这样的。因此,所有的数字不被扫描的,我想不通为什么...


  

101修剪篮065 060 25.00

  105梨篮048 060 30.00

  107桃篮071 060 32.00

  202豪华塔式054 040 45.00



解决方案

我想你想要做的是设计为储存许多个人记录的结构是什么。
每条记录包括:


  • code

  • 名称


  • 重新排序

  • 单价

您应该知道在C语言中每种类型的含义。

我建议你重写你的codeS这样的:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
结构OneRecord {
    INT code;
    字符名称[100];
    INT数量;
    INT记录;
    浮单价;
};诠释主(){
    结构OneRecord *记录=(结构OneRecord *)释放calloc(15,sizeof的(结构OneRecord));
    INT I = 0;    FILE * infile1 =的fopen(NUSTOCK.TXT,R);
    INT最大= 0;    //%99秒用于最大字符串长度,因为我们可以保护了字符串的存储长度的
    而((MAX< 15)及及(的fscanf(infile1,%D%99S%D%F
                &安培;记录[I] code,记录[I] .name和&安培;记录[I] .quantity,&安培;记录[I] .recorder,&安培;记录[I] .unitPrice)== 5) )
    {
        我++;
        最大++;
    }
    对于(i = 0; I<最大;我++){
        的printf(%3D%-18s%3D%3D%6.2f \\ n
                。记录[I] code,记录[I] .name和记录[I] .quantity,记录[I] .recorder,记录[I] .unitPrice);
    }    FCLOSE(infile1);
    免费(记录);
}

和如何使用功能或其他许多地方的C 结构
在C编程语言,有不同的类型,如int,焦炭,结构等。您可以使用结构像许多其他类型的。

 无效printRecords(常量结构OneRecord *记录,诠释最大值)
{
    INT I;
    对于(i = 0; I<最大;我++){
        的printf(%3D%-18s%3D%3D%6.2f \\ n
                。记录[I] code,记录[I] .name和记录[I] .quantity,记录[I] .recorder,记录[I] .unitPrice);
    }
}

int main()
{

    FILE* infile1;

    int stockCode[15];
    char stockName[100];
    int stockQuantity[15];
    int stockReorder[15];
    int unitPrice[15];
    int i;

    infile1  = fopen("NUSTOCK.TXT", "r");

    while(fscanf(infile1, "%d %s %d %d %f",
         &stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF)
    {
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
          stockCode, stockName, stockQuantity, stockReorder, unitPrice);
    }

    fclose(infile1);

}

What I'm trying to do is to take information from a file and store it into 5 separate arrays. However, when printing out, it only prints out the name correctly.

1394854864 Prune-Basket 1394854688 1394854624 0.00
1394854864 Pear-Basket 1394854688 1394854624 0.00
1394854864 Peach-Basket 1394854688 1394854624 0.00
1394854864 Deluxe-Tower 1394854688 1394854624 0.00

The original file looks like this. So all the numbers aren't being scanned in and I can't figure out why...

101 Prune-Basket 065 060 25.00
105 Pear-Basket 048 060 30.00
107 Peach-Basket 071 060 32.00
202 Deluxe-Tower 054 040 45.00

解决方案

I think what you want to do is designing a structure for saving many personal records. And each record contain:

  • code
  • name
  • quantity
  • reorder
  • unitPrice

You should know the meaning of each type in C language.

I suggest you rewrite your codes like these:

#include <stdio.h>
#include <stdlib.h>
struct OneRecord{
    int code;
    char name[100];
    int quantity;
    int recorder;
    float unitPrice;
};

int main(){


    struct OneRecord* records = (struct OneRecord*)calloc(15, sizeof(struct OneRecord));
    int i = 0;

    FILE* infile1  = fopen("NUSTOCK.TXT", "r");
    int max=0;

    //%99s is used for max string length, because of we can protect the out of string's memory length
    while((max<15)&&(fscanf(infile1, "%d %99s %d %d %f",
                &records[i].code, records[i].name, &records[i].quantity, &records[i].recorder, &records[i].unitPrice) == 5))
    {
        i++;
        max++;
    }
    for(i=0;i<max;i++){
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
    }

    fclose(infile1);
    free(records);
}

And how to use the C Structure in functions or many other places? In C programming language, there are different types such as int, char, struct and so on. You can use struct like many other types.

void printRecords(const struct OneRecord* records, int max)
{
    int i;
    for(i=0;i<max;i++){
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
    }
}

这篇关于从文件到数组阅读 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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