Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,633 people online right now. Registration is fast and FREE... Join Now!




getline!

 
Reply to this topicStart new topic

getline!, problem with ignore()

wisheshewasleet
1 Oct, 2007 - 11:22 PM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 15


My Contributions
I am trying to write a program that reads text from two different input files. the text from the first input file, made up of words, and the text from the second input file, made up of lines of text, are both being written to an output file that the user names.

I have all of the code and the program compiles succesfully, but when I run it it only reads from the first file and not the second one. but if i take out the portion of code that opens, reads, outputs, and closes the first input file ("words.txt"), then the second part DOES read from the second input file ("phrases.txt"). So why in the world won't it do both? what is the error in my code and how do I fix it? My teacher said something about the ignore() thing, but even when I try that it still only reads and writes the first section.

help please!!

CODE

#include<iostream> // Lab 4_B by Tim Nelson
#include<fstream>

using namespace std;

int main()
{
    ifstream infile;  // keep on declarin' those I/O variables!!
    ofstream outfile;

    char word1[20], word2[20], word3[20], word4[20], fileName[20];
    char text1[40], text2[40], text3[40];

    cout << "Please enter the name of the output file you would like the data written to: "
         << endl;
    cin >> fileName;
    cout << "Thank you, the data will be written to " << fileName << endl;

    outfile.open(fileName);
    infile.open("words.txt");
    infile >> word1 >> word2 >> word3 >> word4;
    outfile << word1 << endl
            << word2 << endl
            << word3 << endl
            << word4 << endl;
    infile.close(); //make sure to close the first input file before opening the second
    
    
    
    infile.open("phrases.txt");
    infile.getline(text1, 40);
    infile.getline(text2, 40);
    infile.getline(text3, 40);
    

    outfile << text1 << endl;
    outfile << text2 << endl;
    outfile << text3 << endl;

    infile.close();
    outfile.close();

    return 0;

}





User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Getline!
1 Oct, 2007 - 11:35 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
when using multiple files with a single fstream, it is generally good policy to clear the status flags of the stream prior to opening a new file. if you include the line infile.clear() between closing the first file and opening the second, the problem will likely go away.

unfortunately, i can't give you a good explanation for this right now - at one time i knew why, but that was early on in my c++ learning curve, and so now it's just become a rule of thumb for me. and now, i generally use multiple fstreams when multiple files are to be opened, so it doesn't come up a lot for me.

hope that helps, and i'll post the explanation if i can recall it.

-jjh

This post has been edited by jjhaag: 1 Oct, 2007 - 11:36 PM
User is offlineProfile CardPM
+Quote Post

wisheshewasleet
RE: Getline!
1 Oct, 2007 - 11:42 PM
Post #3

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 15


My Contributions
That worked! thank you very much.
My teacher explained that sometimes the program gets hung up on a newline character, so when that happens just use infile.ignore(), but that didn't work. So I'll ask her about the infile.clear(), and if she knows it well enough to explain it, I'll post the explanation up on this thread sp anyone who reads it later will understand.

Thanks again!!!
User is offlineProfile CardPM
+Quote Post

wisheshewasleet
RE: Getline!
1 Oct, 2007 - 11:48 PM
Post #4

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 15


My Contributions
And also, if I just declare another input stream variable, like ifstream infile2, then use that for the second file, it works without infile.clear().

I didn't realize you could declare two input stream variables.
Now I really get it, thanks!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month