By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

add a newline to an array of unknown size

P: n/a
I'm using getline in a while loop so that I can get all the lines one at a time; I am going to piece through each one and look at each character. However I don't know the line size and I don't know the number of lines so I have to depend on newline characters and EOF markers.

However getline doesn't get the newline character, so I have an array[1000] and I don't know where the text in the array ends. How can I add the newline to the end of the text in the array? or how do I tell when theline array has run out of text without the newline character?

while(inf.getline(theline))
//code

while(theline[i]!= '\n'
//testing each character


thanks,
Jane
Oct 3 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
What you do is perform your getline as usual. Then after getline completescall gcount. That will tell you the number of characters fetched by getline. If the number fetched is equal to your buffer size, there's more data. So you put the getline in a loop and fetch until gcount is less than your buffer size.

From this you can figure out the number of buffer characters to process and you won't need a delimiter.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   const int bufferSize = 10;
  4.   char buffer[bufferSize];
  5.   unsigned int fetched;
  6.   cin.getline(buffer,bufferSize, '\n');
  7.   fetched = cin.gcount();
  8.   while (fetched == bufferSize)
  9.   {
  10.      // 1. process the characters in buffer
  11.      // 2. do another getline and gcount
  12.  
  13.   }
  14.   //3. process the remaining characters (fetched) in buffer
  15.  
  16. }
Oct 3 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.