Thread: Read only one line using seekg

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    Read only one line using seekg

    Hello,

    I am just learning how to use the seekg operator. In the code I have written, my intent was to move to the third line of data in an external text file, work with that line of data, and then stop. I used an if statement (if newValue == '\n') to try and tell the program that my wish is to read one, and only one line of code.
    I have had success in getting it to read the line I want, but it also reads every line after that line, rather than breaking once the newline character is encountered. Can someone help me tell this program what I really want it to do?
    Here is my program:
    Code:
    /* This program will make a first pass to find the average
     * of the numbers in an external file,
     * and a second pass to find the difference between 
     * each number and the average. 
     * It is being modified to read and operate on
     * only one line from the external data file.
     */
    
    
    #include <iostream>
    #include <fstream>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    	ifstream inStream("data.txt");
    	assert(inStream.is_open());
    
    	double newValue,
    		   sum = 0.0,
    		   average = 0.0;
    	int count = 0;
    
    	for (;;)
    	{
    		inStream >> newValue;
    		if (inStream.eof()) break;
    		sum += newValue;
    		count++;
    	}
    
    	if (count > 0)
    		average = sum / count;
    	inStream.clear();
    	inStream.seekg(24, ios::beg);
        // 12 bytes per line in data.txt file
    	for (;;)
    	{
    		inStream >> newValue;		
    		if (inStream.eof()) break;		 		
    		cout << "New Value = " << newValue << ": " << newValue - average << endl;		
    		if (newValue == '\n') break;		
    	}
    			
    	inStream.close();
    }
    Semper Fi!

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    inStream >> newValue;	
    ...
    if (newValue == '\n') break;
    'newValue' would never store '\n' as '\n' is not of type double ('newValue' stores only double value). Therefore, the if-statement above is never true.
    Will this one work for your data?
    Code:
    while (inStream >> newValue)
    {		 		
        cout << "New Value = " << newValue << ": "
               << newValue - average << endl;		
        if (inStream.peek()  == '\n')
            break;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    'newValue' would never store '\n' as '\n' is not of type double ('newValue' stores only double value). Therefore, the if-statement above is never true.
    Will this one work for your data?
    Yes, that worked beautifully. Thanks for the explanation of why the way I was doing it would not work. This stuff is starting to make sense.

    Now I have just seen something else in this code that troubles me...what if some of the lines of data in the data.txt file were not exactly 12 bytes long? Is there a way I can pre-read a line to determine it's length so that I know where to move the file pointer to?
    Semper Fi!

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by RedZippo
    Now I have just seen something else in this code that troubles me...what if some of the lines of data in the data.txt file were not exactly 12 bytes long? Is there a way I can pre-read a line to determine it's length so that I know where to move the file pointer to?
    Look up gcount(). But usually the way reading file works is, you know exactly the format of data in the file, because you're the one who wrote to the file of the info about the format of data is in the header of the file, or sthg similar, or there're delimiters that tell when to stop/start reading.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  3. Debugging link error
    By bubux in forum C Programming
    Replies: 5
    Last Post: 07-06-2002, 02:19 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM