Thread: output file

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    output file

    i need help with my output statement dont know if its correct .. reading data file is ok .. but want to know how to send results to and output file .. please help

    // DATAFILE
    //------------------------------------------------------
    // data file "depostit.txt"

    ifstream infile ("c:/deposit.txt",ios::in);
    if(!infile)
    {cerr << endl << "file not opened" << endl;
    exit(1);
    }

    while(!infile.eof())
    {infile >> cust[i].fname;
    infile >> cust[i].lname;
    infile >> cust[i].acct;
    infile >> cust[i].amt;
    i++;
    }

    // OUTPUT FILE
    //------------------------------------------------------
    //output file call "output.txt"

    ofstream outfile ("c:/output.txt",ios:ut);
    if(!outfile)
    {cerr << endl << "file not opened" << endl;
    exit(1);
    }

    while(!outfile.eof())
    {outfile >> cust[i].fname;
    outfile >> cust[i].lname;
    outfile >> cust[i].acct;
    outfile >> cust[i].amt;
    i++;
    }

  2. #2

    Post Here's what I make of it

    For the most part you have it correct. You're going to have a change a few things around.

    This is the only part that needs fixing:
    Code:
    while(!outfile.eof()) 
    {outfile >> cust[ i ].fname; 
    outfile >> cust[ i ].lname; 
    outfile >> cust[ i ].acct; 
    outfile >> cust[ i ].amt; 
    i++; 
    }
    First off, you should not have a loop to check for the end of the file, because with fstream I don't think the file ever ends when you write to it, I think it just keeps expanding it. So just make it a for loop if you really need to increment the i variable.

    Second off, your ">>" keyword is pointing the wrong way. It should be pointing outfile ("<<").

    So here is what I would write the code to look like:
    Code:
    for(i=0;i<10;i++)
    {outfile << cust[ i ].fname; 
    outfile << cust[ i ].lname; 
    outfile << cust[ i ].acct; 
    outfile << cust[ i ].amt; 
    i++; 
    }
    Hope this helps
    -Mike
    {InFeStEd-ArCh0n}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM