prg2_2.cpp

来自「经典数据结构书籍 数据结构C++语言描述 的源代码 很难找的哦」· C++ 代码 · 共 51 行

CPP
51
字号
#include <iostream.h>
#include <fstream.h>
#include <strstream.h>
#include <stdlib.h>
#include <string.h>

void main(void)
{
    // input text file containing names and values
    ifstream fin;
    
    // read identifiers into name and write results to outputstr
    char name[30], outputstr[256];
    
    // declare an array-based output stream that uses outputstr
    ostrstream outs(outputstr, sizeof(outputstr));
    
    double value;
    
    // open 'names.dat' for input. make sure it exists.
    fin.open("names.dat", ios::in | ios::nocreate);
    if (!fin)
    {
        cerr << "Could not open 'names.dat'" << endl;
        exit(1);
    }
   
   // read names and values. write to outs as 'name = value   ' 
    while(fin >> name)
    {
        fin >> value;
        outs << name << " = " << value << "   ";
    }
    // null-terminate the output string
    outs << ends;
    
    cout << outputstr << endl;
}

/*
<File "names.dat">

start   55
breakloop   225.39
stop    23

<Run of Program 2.2>

start = 55   breakloop = 225.39   stop = 23
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?