File Input Question

This is a discussion on File Input Question within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a text file that looks like this: <file> POV Scene Description File Starting value of t: 0 ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    52

    File Input Question

    Hi, I have a text file that looks like this:
    <file>
    POV Scene Description File
    Starting value of t:
    0
    Ending value of t:
    6.28
    Number of balls:
    200
    </file>
    Anyways, I have to read in certain parts of the file. Specifically, 0, 6.28, 200. The rest is stuff I don't need but it's required that I read in only those values. All of my program is ready to go and to test, I've simply had it set up like this "0 6.28 200" but I have to change it so it looks like the example above and I have no idea. Heh, I tried this
    Code:
    input >> variable >> variable >> variable;
    for example, with input being my fstream variable but that of course did not work, I really have no idea what to do as getline is for strings so I'm stumped. Anyhelp will be appreciated. Also for your information, I am using fstream. Thanks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Typically, programs like yours are for reading many records that are arranged identically, so you just pick out the information at the same location for every record.

    One thing to think about is: getline() is for strings, but if you don't have to do any numerical calculations with the numbers, you can just read them as strings and display them.

    Here is an example:
    Code:
    #include<iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    { 
    	ifstream inFile("C:\\TestData\\input.txt");
    	char junk[100];
    	int start, number;
    	double end;
    
    	inFile.getline(junk, 100);
    	inFile.getline(junk, 100);
    	inFile>>start;
    	inFile.ignore();//To remove the '\n' at the end of the line
    	                //left by the >> operator in the previous line
    
    	inFile.getline(junk, 100);
    	inFile>>end;
    	inFile.ignore();
    
    	inFile.getline(junk, 100);
    	inFile>>number;
    	
    
    	cout<<start<<endl;
    	cout<<end<<endl;
    	cout<<number<<endl;
    
    	
    	
    
    	return 0;
    }
    The cin.ignore() line is necessary or the subsequent getline() attempt will get messed up. getline() stops reading when it encounters a '\n', and the >> operator leaves the '\n' in the input stream. So, after this line is executed:

    inFile>>start;

    there will still be a '\n' sitting in the input stream as the next character to be read, so getline() will only read that character and then end, but you need it to read in the whole next line. The solution is to use cin.ignore(), which skips one character.

    If the numbers could be on any line in the file, then you have to read in each line as a string, test for an alpha numeric character at the first position, if there isn't one--discard the string, if there is one and you need to use it in a numerical calculation, you need to convert it to a number using a function like atoi() (-->alpha to int).
    Last edited by 7stud; 08-05-2003 at 01:01 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    I would be using getline() but I have to do numerical calculations with the numbers...sorry if I didn't say that.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Look at my example again--it doesn't use strings to read in the numbers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I would use getline for everything and then parse the integers out - removes the necessity of the ignore call.
    boost::lexical_cast is out there!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Read from the file using a string variable and the getline method. Then use atoi :
    http://www.cppreference.com/stdstring_details.html#atoi to convert the number to int.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm all for boost::lexical_cast for the conversion. It's more C++-like.

    http://www.boost.org/libs/conversion/lexical_cast.htm
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Beginner Text File Input Question
    By Ruggles in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 01:18 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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