I am trying to parse a text files which has blank lines. The getline function is supposed to read blank lines and move on to the next lines. However, my text file is like this :
ABC.....
DEF......


HIJ......

I am able to reach the lines before the blank lines but not the line 'HIJ......' with getline. Here is my code

char *tmp1,*tmp2,*tmp4;
tmp4="http://";

fileread.get(tmp2,8);

while((strcmp(tmp2,tmp4)!=0) )
    {

        fileread.getline(tmp1,100);
       cout<<"tmp1 is"<<tmp1<<"\n";
       
       fileread.get(tmp2,8);    
       cout<<"tmp2 is"<<tmp2<<"\n";      
         
    }

There are about 15 non-empty lines in my text file before the blank lines and I am able to read everything in them properly. Once I reach the blank lines , both tmp1 and tmp2 are printed blank

Can anyone figure out what the problem is?

If I understand you correctly then you want to skip all the blank lines in your file. What you'll see with getline() is that it reads the next line in the input file regardless of whether it is full or blank. Therefore, you will get blank lines in your output. You can use a quick if statement to test whether what getline() read is blank or not.

Here's a quick example:

string data;
    while (getline(inputFile, data))
    {
        if (data == "") continue; //Skip blank line

        cout << data << endl;
    }

In this example, "continue" will restart the loop while the input is blank and therefore, skip all the blank lines.

(Of course you'll have to modify the code to work with your program, but the basic idea is there.)

I'm not 100% sure what the problem is or what the text file is exactly, what the rest of the program looks like, and what the actual and desired output is, but I ran this program:

#include <fstream>
#include <cstring>
#include <iostream>
using namespace std;


int main ()
{
    ifstream fileread;
    fileread.open ("inputfile.txt");

    char *tmp1, *tmp2, *tmp4;
	
	tmp1 = new char[101];
	tmp2 = new char[101];
    tmp4 = "http://";

    fileread.get(tmp2,8);
	
    while((strcmp(tmp2,tmp4)!=0) )
    {
        fileread.getline(tmp1,100);
        cout<<"tmp1 is"<<tmp1<<"\n";       
        fileread.get(tmp2,8);    
        cout<<"tmp2 is"<<tmp2<<"\n";
    }
		
    fileread.close ();
    delete tmp1;
    delete tmp2;
    return 0;	
}

with this input file:

ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOP




HIJKLMNOPQRSTUV
http://www.yahoo.com
KLMNOPQRST

and got this output:

tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 isABCDEFG
tmp1 isHIJKLMNOP
tmp2 is
tmp1 is
tmp2 is
tmp1 is
tmp2 is
tmp1 is
tmp2 is
tmp1 is
tmp2 isHIJKLMN
tmp1 isOPQRSTUV
tmp2 ishttp://

Is this what you want?

There are about 15 non-empty lines in my text file before the blank lines and I am able to read everything in them properly. Once I reach the blank lines , both tmp1 and tmp2 are printed blank

Can anyone figure out what the problem is?

The problem is you read a blank line.

Blank lines are not automatically skipped when you read a file. If you want it skipped, you have to do the skipping with an if statement.

Sorry guys I was away so I could not reply earlier. I figured what my mistake was. I was doing a .get on the blank line and then doing a .getline to move to the next line. But the .get removed the '\n' character so .getline couldn't find it and hence wouldn't move to the next line. I just do a getline now, without the .get and it works fine.

@necrolin & WaltP - Maybe I should I have framed my question better. I do not mind the blank outputs for the blank lines. The problem was I was not able to move to the non-empty lines after the blank ones.

@VernonDozier - I thought the .get was the problem but if you are getting the right output then what I said above might not be right. But I am not using the .get anymore, and things are fine.

This is my first post here and I am grateful for the replies. Thanks.

tmp4 is a null pointer which has the value 0.

When you get the string into tmp2 that has 0 and compares them. The result is 0 so it breaks out of the while loop.

Second Idea:
Condition is being met it, tmp2 attains the "http://"

Regards,

Gen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.