elements.c
来自「c语言开发方面的经典问题,包括源代码.c语言开发所要注意的问题,以及在嵌入式等各」· C语言 代码 · 共 56 行
C
56 行
/* * File: elements.c * ---------------- * This program copies the information from the elements.dat * file into a table formatted into fixed-width columns. The * data values in the file are read using fscanf. */#include <stdio.h>#include "genlib.h"#include "simpio.h"/* * Constants * --------- * ElementsFile -- Name of the elements data file * MaxElementName -- Maximum length of element name * MaxSymbolName -- Maximum length of element symbol */#define ElementsFile "elements.dat"#define MaxElementName 15#define MaxSymbolName 2/* Main program */main(){ FILE *infile; char elementName[MaxElementName+1]; char elementSymbol[MaxSymbolName+1]; char namebuf[MaxElementName+MaxSymbolName+4]; int atomicNumber; double atomicWeight; char termch; int nscan; infile = fopen(ElementsFile, "r"); if (infile == NULL) Error("Can't open %s", ElementsFile); printf(" Element (symbol) Atomic Weight\n"); printf("--------------------------------------\n"); while (TRUE) { nscan = fscanf(infile, "%15[^,], %2[^,], %d, %lf%c", elementName, elementSymbol, &atomicNumber, &atomicWeight, &termch); if (nscan == EOF) break; if (nscan != 5 || termch != '\n') { Error("Improper file format"); } sprintf(namebuf, "%s (%s)", elementName, elementSymbol); printf("%3d. %-20s %8.3f\n", atomicNumber, namebuf, atomicWeight); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?