getline i/o

This is a discussion on getline i/o within the C++ Programming forums, part of the General Programming Boards category; I am trying to gain a better understanding of reading a file and getline. I need to be able to ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    getline i/o

    I am trying to gain a better understanding of reading a file and getline. I need to be able to read and output a file one line at a time. The file contains 5 short lines each ending with a period. I need to read a different line with each pass. I wrote this little program to try to help me understand this. Rather than loop through and read each line one at a time, the program reads the first line of the text file and ends. Any help with understanding this would be appreciated.

    Thanks.

    Kyle


    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    using std::ifstream;
    
    newLine(ifstream& fin);
    
    int main()
    {
    char question=0;
    ifstream fin;
    	fin.open("A:\\answerfile.txt");
    
    	for(int i=0;i<5;i++);
    	{		
    		
    	newLine(fin);
    
    	}
    	return 0;
    
    }
    
    newLine(ifstream& fin)
    {
    	char answerfile[50];
    	
    		fin.getline(answerfile,50, '\n');
    		cout<<answerfile<<endl;
    
    	return 0;
    	
    }
    The answerfile.txt file looks like:
    Test answer one.
    Test answer two.
    Test answer three.
    Test answer four.
    Test answer five.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    try

    Code:
    	while (!fin.eof())
    	{		
    		
    	newLine(fin);
    
    	}
    	return 0;
    instead of your for loop.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Ideally, what I am trying to understand and get to work is that I will prompt the user to input a question, which the program will ignore, the program will then read a "answer" from the text file. The program will then ask the user if they would like to ask another question (which will also be ignored), however, the program will then provide the next "answer" from the file.
    While I execute the program with the two lines that are commented out, the program works how I would like in that it will let you read each line in the file by answering yes to the question "would you like to see the next answer". When I execute the program with the lines prompting for the ignored question, it will execute once, display the first line of text from the file and end. Any help in understanding this would be appreciated.

    Thanks.

    Kyle


    [code]
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    using std::ifstream;

    newLine(ifstream& fin);

    int main()
    {
    char doesntmatter=0;
    char question=0;

    ifstream fin;
    fin.open("A:\\answerfile.txt");

    do
    {
    //cout<<"Please enter a question:"<<endl;
    //cin>>doesntmatter;
    newLine(fin);
    cout<<"Would you like to see the next answer, y or no:"<<endl;
    cin>>question;

    }while(question=='y'||question=='Y');

    return 0;
    }

    newLine(ifstream& fin)
    {
    char answerfile[100];

    fin.getline(answerfile,100, '\n');
    cout<<answerfile<<endl;

    return 0;

    }

    The answerfile.txt file looks like:
    Test answer one.
    Test answer two.
    Test answer three.
    Test answer four.
    Test answer five.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    I really cant' understand what your saying.....I ran the program, and it seems to work fine....outputs the answer, then asks if I want to display again..if i yet Y, it goes ot the next line, if i hit N it ends...whats the problem?

    edit: I uncommented the stuff, and it worked fine if i inputed without entering a space, but would exit after 1 pass if i entered a space....try using a getline and character array (or string) for the doesntmatter variable
    Last edited by Jamsan; 04-19-2003 at 05:23 PM.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Thanks for your help with this. The best way that I can describe what this program is supposed to do is similar to the old "magic 8" balls, where you can ask a question that doesn't really matter and receive one of the pre-prepared messages. The only part of this that is still not working correctly is that what prompted for "would you like to ask another question", it does not allow for another question to be input and simply displays the second "answer", and the thrid, and so on.
    The process should be input question, displays "answer", prompts for another question, if yes-displays another answer, if no-terminates program. Thanks again.

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    using std::ifstream;
    
    newLine(ifstream& fin);
    
    int main()
    {
    char doesntmatter[100];
    char question=0;
    
    ifstream fin;
    	fin.open("A:\\answerfile.txt");
    
    	do
    	{		
    	cout<<"Please enter a question:"<<endl;
    	cin.getline(doesntmatter, 100);
    	newLine(fin);
    	cout<<"Would you like to ask another question, y or no:"<<endl;
    	cin>>question;
    
    	}while(question=='y'||question=='Y');
    
    	return 0;
    }
    
    newLine(ifstream& fin)
    {
    	char answerfile[100];
    	
    		fin.getline(answerfile,100, '\n');
    		cout<<answerfile<<endl;
    
    	return 0;
    	
    }
    The answerfile.txt file looks like:
    Test answer one.
    Test answer two.
    Test answer three.
    Test answer four.
    Test answer five.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    do
    { 
    cout<<"Please enter a question:"<<endl;
    cin.getline(doesntmatter, 30, '\n');
    newLine(fin);
    cout<<"Would you like to see the next answer, y or no:"<<endl;
    cin>>question;
    cin.ignore();
    
    }while(question=='y'||question=='Y');

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Jamsan,
    Thanks for the help with this. It works as I would like it to.

    Kyle

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Originally posted by kylesbigdog
    Jamsan,
    Thanks for the help with this. It works as I would like it to.

    Kyle
    No problem.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Rather than loop through and read each line one at a time, the program reads the first line of the text file and ends. Any help with understanding this would be appreciated.

    for(int i=0;i<5;i++);
    {

    newLine(fin);

    }

    Try dropping the ; at the end of the for line. Example:

    for(int i=0;i<5;i++)
    {

    newLine(fin);

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  2. problem with getline() file I/O
    By hyperion in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2003, 08:34 AM
  3. getline I/O
    By Azmeos in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2003, 11:18 AM
  4. getline() and file I/O
    By 7stud in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2003, 01:49 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

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