Thread: Reading a line of data...

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

    Reading a line of data...

    Hello,

    I am trying to coerce my program into reading one line of data from a data file. It isn't working very well. What it does do rather well is asks the user what terminal number they want information on, then takes that number and checks to see if it is greater than 1 (the computer terminals in the data file are numbered 1 thru 5 sequentially), if it is greater than 1, it multiplies it by 30 (there are 30 characters on each line) to move the read position to the correct line. I can verify that seekg() is getting the correct number by displaying the value in the scope where seekg() resides. But after that the program seems to be stuck in a loop of sorts and I am not having success determining A) what is causing this unwanted un-ending loop, and B) why I am not getting any output of data that is being read from the file.
    Here is the program:
    Code:
    #include <iostream>    // cout, cin, <<, >>
    #include <string>      // string
    #include <cassert>     // assert
    #include <fstream>     // ifstream
    using namespace std;
    
    char c;
    string line;
    int termNumb;
    int lineNumber;
    
    
    int main()
    {	
    	ifstream inFile("data.txt");      // open the file
    	assert(inFile.is_open() );        // check for success.
     
    	cout << "Enter the terminal number (such as 001, 002...): ";	
    	cin >> termNumb;             // get the line number of the data field
    
    	// 
    	int lineNumber = 0;
    	if (termNumb > 001) 
    	{
    		  lineNumber = 30 * termNumb;
    		  cout << "The read position is at: " << lineNumber << "\n";
    	}
    	else lineNumber = 0;
    	
    	for (;;)
    	{
    		inFile >> line;
    		if(inFile.eof()) break;
    		inFile.seekg(lineNumber, ios::beg);
    		getline(inFile, line);          
    		
    	 }
    	cout << "Information: " << line << "\n";	
        cout << "The read position is at: " << lineNumber << "\n";
    inFile.close();
    }
    And this is the data file I am using:
    Code:
    001 A53 45-08 1200 A 03-26-04
    002 B15 41-07 2400 B 03-26-04
    003 C71 48-01 4800 C 02-13-03
    004 D22 21-01 4800 D 01-14-01
    005 E35 17-05 9600 E 12-01-03
    I'd appreciate any help I can get. Thanks.
    Semper Fi!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    the reason why you are not getting output is because your cout statement does not have a end line command, so everything that you want to output is still in the output buffer and it never goes to the screen, so change some of your cout statement to read like this

    cout << "The read position is at: " << lineNumber << endl;

    hope this helps you

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >But after that the program seems to be stuck in a loop of sorts
    That's because you've got an infinite for-loop:
    for (;;)
    and since you seek from the beginning each time thru the loop, it never hits eof.
    Code:
    for (;;)
    	{
    		inFile >> line;
    		if(inFile.eof()) break;
    		inFile.seekg(lineNumber, ios::beg);
    		getline(inFile, line);          
    		
    	 }
    	cout << "Information: " << line << "\n";
    If you just want to read one line from the file, you don't need a loop:
    Code:
    	inFile.seekg(lineNumber, ios::beg);
    	getline(inFile, line);          
    	cout << "Information: " << line << "\n";
    You may have to adjust your offset, depending of whether the end-of-line is <CR> or <CR><LF>. For me:
    Code:
    		  lineNumber = 31 * termNumb;
    Last edited by swoopy; 04-03-2004 at 12:10 AM.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    That's because you've got an infinite for-loop:
    for (;
    and since you seek from the beginning each time thru the loop, it never hits eof.
    Thanks, that is something I can work with. But why am I seeking from the beginning each time through the loop? I have no code in my program to tell it to go back to the beginning. How does that work, I mean, where in my code is the program getting information to go back to the beginning of the file? I think if I can understand the mechanics of how the program wants to go back to the beginning, I might can get past this.

    Thanks very much for your help.
    Semper Fi!

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    I have another question about what I am doing...

    Code:
    inFile >> line;
    I am having trouble comprehending what that does. I only put it in my code because the example I am basing my code on used a statement like that. Can you tell me what service that statement provides in a context that I can understand when and why I am using it?
    Semper Fi!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >where in my code is the program getting information to go back to the beginning of the file?

    Right here:
    inFile.seekg(lineNumber, ios::beg);
    The ios::beg says, seek from the beginning of the file.

    >inFile >> line;

    >I am having trouble comprehending what that does.

    It basically reads a string from the file into the string variable line. It stops when it hits whitespace, so it won't read a whole line like getline(), unless of course the line contains no spaces. I don't think you need it in this case.

    You would use it if you only wanted to read text until whitespace is found. Like:
    Code:
    inFile >> firstName >> lastName;
    Something like that.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    It basically reads a string from the file into the string variable line. It stops when it hits whitespace, so it won't read a whole line like getline(), unless of course the line contains no spaces. I don't think you need it in this case.
    Well that was an important piece of information to know. I need to read the entire line, and I will be happy to pay you on Tuesday for a hamburger today.
    Semper Fi!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, if you wanted to seek to a certain place in the file, and then output from there until the eof, you could do this:
    Code:
    	inFile.seekg(start, ios::beg);
    	while(getline(inFile, line))
    	{
    		cout << line << endl;
    	}
    >and I will be happy to pay you on Tuesday for a hamburger today
    Munched on one a few hours ago.

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    By the way, if you wanted to seek to a certain place in the file, and then output from there until the eof, you could do this:
    I definately only want to read one line of data, and I want to stop reading at the newline character.
    Semper Fi!

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try this:

    #include<fstream>
    #include<string>

    //declare ifstream and associate with file to read
    //start at beginning of file by default.
    ifstream fin("myFile.txt");

    //declare variable to hold material read in
    string myString;

    //read in entire file one line at a time and display
    //file contents on screen. Line ends with new
    //line char, by default. Save all spaces and other
    //whitespace char except new line chars
    //file ends at EOF, which will cause getline to fail
    //thereby stopping loop
    while(getline(fin, myString))
    cout << myString << endl;

    //close ifstream
    fin.close();

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Thank you swoopy and elad. I was able to use input from the both of you and finally overcome this challenge. Thank you very much.

    I had another problem in my algorithm, and I am going to mention it here for other newbies who may read this: When you want to read a line of a file...do not forget that the beginning line starts at 0, not 1, like we normally count.
    I blew it in this code by failing to take that into consideration.
    The reason I wasn't getting any output was that I was advancing the read position just shy of the line I wanted, missing it by one position, and it ended up on the newline character just prior to the line I wanted to read and getline() will stop at a newline character. So getline () was doing its job. Once I added:
    Code:
    lineNumber = ((termNumb - 1) * LINE_LENGTH);
    all of my problems went away. LINE_LENGTH is a constant integer that represents the actual length of the line, termNumb is the actual line the user wants to read.

    I physically counted 30 places from the beginning of the line to the end of each line in my data file, but my code would only work when I made LINE_LENGTH = 31.

    Perhaps a more experienced programmer will provide an example to show how to compensate for the fact the line count starts at 0, not 1.


    This is a great site, and it has made my life alot easier. Thanks so much.
    Semper Fi!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  2. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 09:42 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. trouble reading data from a file
    By bob56 in forum C Programming
    Replies: 14
    Last Post: 03-17-2005, 09:03 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM