hi..
im trying to get a line using getline() function from an input file, but it is ignoring end line character. is there any other function that would including the end line character? btw, im not allowed to use something like aString+'\n'
thanks in advance~

Recommended Answers

All 7 Replies

fgets()

is there a function for ifstream?
btw,fgets() need to specific # of character...i tried to put INT_MAX, but it doesnt do the trick.

rather than making it as INT_MAX, seems the only way i could do is to find the length of the file in this way?
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

Maybe more information would help. Like why you need the \n when getline() clearly reads the entire line.

is there any other function that would including the end line character?

istream& my_getline(istream& is, string& s)
{
    istream& result = getline(is, s);

    s.push_back('\n');

    return result;
}

Presto!

im trying to write a program copying lines from input to output file r, and i cant specific any other buffer(ex aString+'\n'). if im using getline, then all lines would stack together. in other word, i want both file(input and output) to be identical the same...if i use aString+'\n'.. the end of the file would actually contain one more character

fgets would gives me another problem i just tried, where if i allocated the array of character to big and not knowing the length of the current line...it then copy the unused part of the array(random stuffs)into my output file. still trying to figure out how to fix this problem.

istream& my_getline(istream& is, string& s)
{
    istream& result = getline(is, s);

    if (!result.eof())
    {
        s.push_back('\n');
    }

    return result;
}

Presto magico!

fgets() reads up to n-1 characters or \n, whichever comes first.
Write out what was read.
Read again to get the next batch of characters.
If the first fgets() reads 1/2 a line, the next fgets() reads the rest.

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.