473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

add a newline to an array of unknown size

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
1 3254
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

4
by: Jason Heyes | last post by:
Why does Foo.h need to be included in Bar.cpp for this program to compile properly? I define the FooList destructor in a separate module so that a forward declaration to Foo can be used. //...
2
by: cjl | last post by:
Hey all: I have a div of a known size, 672px X 672px. In it I will display images that are of unknown size. I have two simple functions that shrink and center the image: function shrink() {...
25
by: prashna | last post by:
Hi all, I have seen a piece of code(while doing code review) which declared an array of size 0.One of my friend told although it is not standard C,some compilers will support this..I am very...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
1
by: Betsy | last post by:
I'm trying to do some pointer manipulation and can not avoid using void * as the type declaration. I get this error message : error C2036 : 'void *' : unknown size though. Is there a compiler...
3
by: manoj.pattanaik | last post by:
Hi, I am trying to compile following piece of code (bb.cpp) using aCC (HP ANSI C++ B3910B A.03.37) compiler on HP-UX 11.23. It gives error:485 //bb.cpp -- Starts #include <iostream> using...
0
by: Cedric | last post by:
Hello, I 'm a bit confused about the size of an array of byte use by getjob or setjob. For GetJob no problem, API return size needed BUT for SetJob I must pass an array with good size. It's...
9
by: bela | last post by:
hello, may I know the method to create an array with unknown size? in C , one can go for malloc. what is the method in java? regards, bela
3
by: cppman | last post by:
Hello, I am very new to C++ programming. I am trying to read a file with very large strings back to back, no commas or any other seperations between characters in a string and the size of the...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.