Input of a Text File

This is a discussion on Input of a Text File within the C++ Programming forums, part of the General Programming Boards category; I am am taking data from a file and having the program output the numbers. The only thing I can ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Input of a Text File

    I am am taking data from a file and having the program output the numbers. The only thing I can not seem to do is get the program to skip the first line instead of skipping the first two lines. The second line contains data I need to be in the output window. I attached the one file I am extracting the data from.

    My output is:
    Enter the name of the file:J:\EngineData\A8.txt
    The thrust for that engine is:
    Time Thrust
    0.084 2.115 It skips the first pair of digits in the file.
    0.127 4.358 How do I fix this problem?
    0.166 6.794
    0.192 8.588
    0.206 9.294
    0.226 9.730
    0.236 8.845
    0.247 7.179
    0.261 5.063
    0.277 3.717
    0.306 3.205
    0.351 2.884
    0.405 2.499
    0.467 2.371
    0.532 2.307
    0.589 2.371
    0.632 2.371
    0.652 2.243
    0.668 1.794
    0.684 1.153
    0.703 0.448
    0.730 0.000

    A8.txt

    Code:
    // Include Section
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    // Main Program
    int main( )
    {
    	// Variable Declarations
    	ifstream data;
    	string file;
    	
    	double time;
    	double thrust;
    
    	cout << "Enter the name of the file:";
    	cin >> file;
    
    	data.open(file);
    	data.ignore(1000, '\n');
    	
    	data >> time >> thrust;
    
    	cout << "The thrust for that engine is: " << endl;
    	cout << "     Time      Thrust" << endl;
    	while(data >> time >> thrust)
    	{
    	cout << "     " << setiosflags(ios::fixed) << setprecision(3) 
    		 << time << "      " << setprecision(3) << thrust << endl;
    	}
    
    	cout << "\n\nEnd Program.\n";
    
    	return 0;
    }
    Last edited by Chemeng11; 05-01-2011 at 11:03 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    Time Thrust
    0.084 2.115 It skips the first pair of digits in the file.
    0.127 4.358 How do I fix this problem?
    Well you could stop reading the first line and discarding it:
    Code:
    data.ignore(1000, '\n'); // This discards the first line "Time Thrust"
    
            // Remove the following line.
    	data >> time >> thrust;   /// This reads the first 2 data points and discards them.
    
    	cout << "The thrust for that engine is: " << endl;
    	cout << "     Time      Thrust" << endl;
    	while(data >> time >> thrust)
    Jim

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    It worked! Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input text file - use debugger?
    By c_lady in forum C Programming
    Replies: 7
    Last Post: 02-05-2011, 02:35 PM
  2. Taking input from text file..
    By Passa in forum C Programming
    Replies: 4
    Last Post: 09-14-2010, 06:12 PM
  3. input integer in a text file
    By Kaustubh in forum C Programming
    Replies: 3
    Last Post: 08-04-2010, 12:14 AM
  4. User input into a text file
    By Starr in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2006, 07:52 PM
  5. Input from non-text file
    By starkhorn in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2004, 01:18 PM

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