954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hw help...reading lines from file

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~

wildplace
Junior Poster in Training
63 posts since Dec 2009
Reputation Points: 10
Solved Threads: 4
 

fgets()

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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);

wildplace
Junior Poster in Training
63 posts since Dec 2009
Reputation Points: 10
Solved Threads: 4
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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!

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

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.

wildplace
Junior Poster in Training
63 posts since Dec 2009
Reputation Points: 10
Solved Threads: 4
 
istream& my_getline(istream& is, string& s)
{
    istream& result = getline(is, s);

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

    return result;
}

Presto magico!

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

fgets() reads up ton-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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You