Hi chromatonus 712,
I suggest a solution a little different. Instead of using C++ streams, it
resorts to OS redirection, in fact there are 2 programs: one collecting
data (named "collect") and the other one displaying it ("disaplay"). Here
is how to use them.
- the command: collect end > saved_data
will collect data until the string "end" is found, it will save records
in the
file "saved_data" and will print the amount collected to the screen.
- the command: display < saved_data
will display the data.
- the pipe: collect end | display
collects and displays data without storing it.
collect.cpp
-----------
#include <iostream>
#include <string>
/*********************************************************************
collect - collect data
usage: collect SENTINEL
SENTINEL is the string marking the end of the input
Input comes from the console and output goes to the console.
Each record found on the console is made of :
string string integer
and should be printed as:
# data string string # data string string # ...
*********************************************************************/
int
main(int argc, char **argv)
{
if (argc != 2) // wrong amount of arguments: print help line
{
std::cout << "Usage: " << *argv << " SENTINEL\n";
return 1;
}
std::string sentinel = *++argv;
int amount = 0;
std::string first_word;
while (std::cin >> first_word && first_word != sentinel)
{
amount++;
std::string second_word;
int data;
std::cin >> second_word >> data;
std::cout << "# " << data << " " << first_word << " "
<< second_word << " ";
}
std::cerr << "\nYou have entered " << amount << " records\n\n";
}
display.cpp
-----------
#include <iostream>
#include <string>
/*********************************************************************
display - print data saved by program "collect"
usage: display
It is expected that input, coming from the console, is made of records
separated by a '#'.
Example:
# data string string # data string string # ...
Output goes to the console.
*********************************************************************/
int
main()
{
char c; // the '#'
int data;
std::string first_word;
std::string second_word;
while (std::cin >> c >> data >> first_word >> second_word)
std::cout << data << '\t'
<< first_word << ' ' << second_word << '\n';
}
Hope this helps.
At 01:16 13/05/02 +0000, chromatonus 712 wrote:
>hi, here is my program that is supposed to do the following:
>
>Enter a series of records from the keyboard, each record consisting of a
>two-word name and an integer,
>example MEG RAM 23, as one entry
>The program will keep a count of the number of entries. After the last one
>is entered, enter a "sentinel" code, some string that will indicate that
>the data has been completely entered.
>Then the data will be saved to a file in the format of
>#data name # name #.......
>Then the data will be read from the file and displayed on the screen, one
>entry per line.
>
>#include "apstring.h"
>#include <iostream.h>
>#include <fstream.h>
>#include <conio.h>
>
>int main()
>{
> apstring name1, name2, x, check;
> ifstream infile;
> infile.open("stuff.dat");
> int n;
> cin.ignore(80,'\n');
> getline(cin,x);
> check = "end";
> while(x!=check)
> {
> infile >> name1 >> name2 >> n >> "\n";
> cin.ignore(80,'\n');
> getline(cin, x);
> }
> infile.close();
> getch();
>
> ofstream xfile;
> xfile.open("stuff.dat");
> getch();
> xfile.close();
>}
>
>I don't know how to do the ofstream part, and I don't know whether I
>actually saved the data in a file, and how to access the file and display
>it? Would you please tell me how to do that, thank you!
--
Luigi
gi...@li...
|