How to input multiple groups of data

This is a discussion on How to input multiple groups of data within the C++ Programming forums, part of the General Programming Boards category; Dear Friends: The following is a tiny program to illustrate "opening a file" and save the results to another file. ...

  1. #1
    Unregistered
    Guest

    How to input multiple groups of data

    Dear Friends:

    The following is a tiny program to illustrate "opening a file" and save the results to another file. I have done this successfully. However, I want to do a further step. I want to input such as 1000 groups data, instead of input them manually, now, I want to input them by a file and calculate them one by one automatically. The following program I wrote only is able to input once. Is any expert know how to do that?

    In order to say more clearly, the following also contains "test" and "test1". My purpose is to also calculate the second and third row of the "test" file and put the results in "test1:

    "test" file content:

    1.1 2.2 3.2
    4.2 5.5 6.5
    7.2 8.1 9.1

    "test1" file content:
    1.1 2.2 3.2 6.6

    Program:

    #include<iostream.h>
    #include<fstream.h>

    main()
    {
    float a,b,c,d;

    ifstream in("test");
    ofstream out("test1");
    if(!in) {
    cout<<"cannot open file.";
    return 1;
    }

    if(!out) {
    cout<<"cannot open file.";
    return 1;
    }

    in >>a>>b>>c;
    d = a + b + c;
    out << a <<" " << b << " " << c << " " << d;
    in.close();
    out.close();
    return 0;
    }

  2. #2
    Unregistered
    Guest
    replace:
    ofstream out("test1");

    with:
    ofstream out("test1.txt", ios::app);

  3. #3
    Unregistered
    Guest
    hrm, ignore the .txt part, sorry

  4. #4
    Unregistered
    Guest
    It seems that it doesn't work this way. I guess it needs a loop while I don't know how to make it.

    Thanks a lot.

  5. #5
    Unregistered
    Guest
    each time your create a new file the OS provides it with two attributes, a filepointer and an end of file flag to indicate when you have reached the end of the file. The OS automatically maneuvers these two attributes for you, although you do have some control over (at least) the file pointer.

    You are correct in the assessment that you can use loop(s) to do what you want. To read all the data from the first file you can set up a loop that reads from the file until the input is the end of file flag, EOF. There are multiple ways to do this:

    while(in)
    while(!in.eof())
    while(in >> data)
    while((in >> data) != EOF)

    within the body of the loop you use the same code you have. However, you need to be sure you declare the ofstream and associate it with a file outside the loop or else you will keep creating the file over and over again, overwriting data each time through. As long as you don't write data to the file all in one session and don't reposition the file pointer yourself you don't need to worry obout ios::app, but it is something you will want to know about as your file handling becomes more sophisticated. Often it is wise to do the first read from file outside of the loop to be sure there is something in the file to be read and to avoid some difficult to find bugs associated with EOF, but experience is probably a better teacher on these sorts of things, so give it a go.

    //streams declared, opened, and associated with file before loop
    ifstream fin("test.txt");
    ofstream fout("dest.txt");

    //variable to hold data inputs
    int data1;
    int data2;

    //primer read to be sure there is data in file
    fin >> data;

    //loop to read file, manipulate data, and output to different file
    while(!fin.eof())
    {
    fin >> data2;
    //manipulate data
    fout << dataResults;
    fin >> data1;
    }

    //close streams
    fin.close();
    fout.close();

  6. #6
    Unregistered
    Guest
    Thanks a lot. It is solved.

    I used a for loop while the annoying part is that I have to input the value of "i" for different data sets. Maybe you have a better idea to do so.

    Look at the following code:

    #include<iostream.h>
    #include<fstream.h>

    main()
    {
    float a,b,c,d;
    int i;

    ifstream in("test");
    ofstream out("test1");
    if(!in) {
    cout<<"cannot open file.";
    return 1;
    }

    if(!out) {
    cout<<"cannot open file.";
    return 1;
    }

    for (i=0; i<3; i++)
    {
    i = i;
    in >>a>>b>>c;
    d = a + b + c;
    out << a <<" " << b << " " << c << " " << d << "\n";
    cout << a <<" " << b << " " << c << " " << d << "\n";

    }
    in.close();
    out.close();
    return 0;
    }

  7. #7
    Unregistered
    Guest
    your loop will only read in one "set" of data per run of the program. The problem with files is there may be more than one set of data in the file, and if you want access to them you can't do it with your approach. For loops are used most often when you know how many times you want to do things. While loops are used more often when you are unsure how many times you need to do something, but you do know what the end point/terminal event will be.

    Try :

    in >> a;
    while(!in.eof())
    {
    in >> b >> c;
    d = a + b + c;
    out << a <<" " << b << " " << c << " " << d << "\n";
    cout << a <<" " << b << " " << c << " " << d << "\n";
    in >> a;
    }

    to read the entire file.

  8. #8
    Unregistered
    Guest
    Good, thanks.

    It works.

    P.S. We can put in >> a; with in >> b>>c; together in the while loop. It seems all right.

  9. #9
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    hmmmm

    does:


    while (!in)


    work the same as:


    while (!in.fail())


    im a bit confused here...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Sorting data retrieved from input file
    By Minds_I in forum C Programming
    Replies: 3
    Last Post: 07-20-2003, 06:25 PM
  5. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21