Hi,i have a question about C. (i use linux).The problem is: I will

  • Thread starter Thread starter rambo3131
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
rambo3131
Messages
18
Reaction score
0
Hi,i have a question about C. (i use linux).The problem is: I will read arbitrary number of character lines from standard input.(not from a file). Size of the lines is also arbitrary. Lines are separated with new line character. . I must send EOF with CTRL-D from terminal.

I should allocate space for each line. Each line should be terminated with terminating null character ('\0').
After the last line, I should mark the end of the lines with a null pointer (NULL).


I don't want code,but some hints please please help me
 
Physics news on Phys.org


You can read the lines in with fgets (do NOT use gets) into a fixed length buffer, with the length being the longest line you will see.

You probably don't know how many lines of input you will get. Given what you said, I'm thinking you can malloc an array of char * and realloc it when you need more space for more lines.

Don't forget you have access to man pages. So you can type 'man fgets' in a shell to see documentation on fgets, realloc, fopen, etc.

Does that help?

P.S. To read from standard input, just use 'stdin' as the FILE* to fgets (or other calls to that stream).
 


rambo3131 said:
I will read arbitrary number of character lines from standard input.(not from a file). Size of the lines is also arbitrary.

Is there an upper limit on the length of a line, or on the number of lines? If your only constraint is your computer's memory capacity, then you have to use dynamic memory allocation (malloc()) and be prepared to increase the size of the allocated memory for a line, or for the number of lines, when necessary. I'm not very familiar with C-style memory allocation (I'm a C++ guy myself), but I think realloc() can be used for this. For example, if you fill the memory allocated for a line, you can expand it by e.g. a factor of two.
 


There's an alterative approach. Any user input will be smaller than the amount of available space in RAM, at least on a modern day PC. Taking advantage of this, you can allocate (malloc()) enough memory to hold the largest amount of input you expect to see with this program, and also allocate enough memory required to hold an array of pointers for the maximum number of lines you expect to see. After receiving a line of input data, if the function you use reports the number of character input, you can use that to set a pointer to the next line and input agin. The alternative is to use memchr to search for the EOL (and advance one past that to set a pointer for the next line).
 
Last edited:


Yes, by using the standard string and vector types to store the data, you eliminate any worries about capacity, at least until you run out of memory in your computer.

When you read a string using getline(), it automatically expands to accommodate the entire line.

When you append an item to a vector using the push_back() member function, the vector automatically expands as necessary.