reading input file

This is a discussion on reading input file within the C++ Programming forums, part of the General Programming Boards category; My program won't read the input file. I have the right names, so I know that is not the problem. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    reading input file

    My program won't read the input file. I have the right names, so I know that is not the problem. I just don't know if I have to put the input file in the C++ or what. When I start the program, the black box just says to press a key and then the box goes away.

  2. #2
    KingoftheWorld
    Guest

    Re: reading input file

    Originally posted by dellebelle751
    My program won't read the input file. I have the right names, so I know that is not the problem. I just don't know if I have to put the input file in the C++ or what. When I start the program, the black box just says to press a key and then the box goes away.
    A code to check your input file to see if it open successfully!

    if ( ! inputFile)
    cout<<"Damn, open input file not OKie Dokie!"<<endl;
    else
    { // process the input file here}

    Hope ya get an idea!
    KingoftheWorld

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    Here is the code that I have right now:


    Code:
    # include <fstream>              // for file streams for input and output
    # include <iomanip>              // for formatting manipulators
    # include <string>               // 
    using namespace std;
    
    int main ( ) {
    	
    	ifstream In("1.StockData.txt");         // Open the input file
    
    	ofstream Out("Summary.txt");          // Open the output file
    
    	string  CName,                          // Name of Customer
    		    AccountNum,                     // Customer's Account Number
    		    Date,                           // Date of Report
    		    SName;                          // Name of the stock
    
    	int    Shares,                          // Number of shares held
    		   OpenDollar,                      // Opening price dollars
    		   OpenCents,                       // Opening price cents
    		   TotalOpen,                       // Total opening price
    		   CloseDollar,                     // Closing price dollars
    		   CloseCents,                      // Closing price cents
    		   TotalClose,                      // Total closing price
    		   UnitChange,                      // Gain or loss of each share
    		   TotalChange;                     // Gain or loss of total value of shares
    
        double  PerChange;                     // Percentage change of value of a single share
    
    	// Write the header information to the output file:
    	Out << "Programmer: Danielle Barry" << endl;
    	Out << "CS 1044 Simple Stock Trader" << endl;
    	Out << endl;                  // Output a blank line for separation
    
    	// Read the customer name:
    	getline(In, CName);
    	
    	// Read account number and date:
    	In >> AccountNum >> Date;
    
    	// Write the data to the output file:
    	Out << "Customer:"
    		<< setw(41) << CName
    		<< setw(17) << AccountNum
    		<< endl;
    	Out << "Date:"
    		<< setw(11) << Date
    		<< endl;
    }
    [edit]Code tags added by Hammer. dellebelle751: read Salem's post.

  4. #4
    KingoftheWorld
    Guest
    Originally posted by dellebelle751
    Here is the code that I have right now:


    # include <fstream> // for file streams for input and output
    # include <iomanip> // for formatting manipulators
    # include <string> //
    using namespace std;

    int main ( ) {

    ifstream In("1.StockData.txt"); // Open the input file

    ofstream Out("Summary.txt"); // Open the output file

    string CName, // Name of Customer
    AccountNum, // Customer's Account Number
    Date, // Date of Report
    SName; // Name of the stock

    int Shares, // Number of shares held
    OpenDollar, // Opening price dollars
    OpenCents, // Opening price cents
    TotalOpen, // Total opening price
    CloseDollar, // Closing price dollars
    CloseCents, // Closing price cents
    TotalClose, // Total closing price
    UnitChange, // Gain or loss of each share
    TotalChange; // Gain or loss of total value of shares

    double PerChange; // Percentage change of value of a single share

    // Write the header information to the output file:
    Out << "Programmer: Danielle Barry" << endl;
    Out << "CS 1044 Simple Stock Trader" << endl;
    Out << endl; // Output a blank line for separation

    // Read the customer name:
    getline(In, CName);

    // Read account number and date:
    In >> AccountNum >> Date;

    // Write the data to the output file:
    Out << "Customer:"
    << setw(41) << CName
    << setw(17) << AccountNum
    << endl;
    Out << "Date:"
    << setw(11) << Date
    << endl;
    }
    Make sure the correct path where the input file and output file locate. if they locate in disk A: then
    ifstream In("A:\\StockData.txt"); // Open the input file
    ofstream Out("A:\\Summary.txt"); // Open the output

    Also check files if open successfully
    KingoftheWorld

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    rename this file

    1.StockData.txt

    by removing the first period. Then recompile. If still not effective determine location of that file. If it's not is same subdirectory as the cpp file for your driver program it will need to be moved there or you will need to use the full path name as per kingoftheworld's example.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Salem
    Post your code, and don't forget to read this before you paste code to the board
    Looks like you was ignored

    Pleas use code tags, I don't even look at code if it isn't tagged
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by endo
    Looks like you was ignored

    Pleas use code tags, I don't even look at code if it isn't tagged
    Now it's tagged
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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