How to ignore newline characters when reading file in list format

I am trying to read a file with a list of titles and authors, and I need to be able to ignore the newline character that separates each line in the file.
For example, my .txt file might have a list like this:

The Selfish Gene
Richard Dawkins
A Brave New World
Aldous Huxley
The Sun Also Rises
Ernest Hemingway


I have to use parallel arrays to store this info, and then be able to format the data like so:

The Selfish Gene (Richard Dawkins)

I was trying to use getline to read the data, but when I go to format the title and author, I get this:

The Selfish Gene
(Richard Dawkins
)


How do I ignore the newline character when I read in the list from the file?
This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        int loadData(string pathname)
        {
        string bookTitle[100];
        string bookAuthor[100];
        ifstream inFile;
        int count = -1; //count number of books
        int i; //for variable
    
        inFile.open(pathname.c_str());
        {
            for (i = 0; i < 100; i++) 
            {
                if(inFile)
                {
                    getline(inFile, bookTitle[i]);
                    getline(inFile, bookAuthor[i]);
                    count++;
                }
            }
            inFile.close();
            return count;
        }


I greatly appreciate any help!
Richard
Last edited on
Something else is going on because getline() does strip the newline from the end of the string.
closed account (DSLq5Di1)
I believe he wishes to skip the blank lines between the title and author lines Duoas.

1
2
3
4
5
6
7
8
9
10
                if(inFile)
                {
                    getline(inFile, bookTitle[i]);
                    inFile.ignore();

                    getline(inFile, bookAuthor[i]);
                    inFile.ignore();

                    count++;
                }
Sorry, I didn't mean to format that list with the blank lines between the authors and titles. I did that because that's how you do it in Stackoverflow.
It was supposed to just be:
The Selfish Gene
Richard Dawkins
A Brave New World
Aldous Huxley
The Sun Also Rises
Ernest Hemingway
I encounter the seam problem here is my question

http://www.cplusplus.com/forum/general/48215/

but still i did not get my answer ...
closed account (DSLq5Di1)
@eyeswideorange Ahh I see..
http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
THANK YOU SO MUCH SLOPPY9!!! I realized my problem. The .txt file my instructor gave us was corrupt, or in some funny format, or having problems from his Windows machine to my Mac, or something. I copy and pasted the exact list of titles and authors into a new .txt file on my machine, and wa lah! It works like a charm!
Topic archived. No new replies allowed.