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

  • Thread starter Thread starter rambo3131
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around reading an arbitrary number of character lines from standard input in C programming on a Linux system. Participants explore various methods for handling dynamic memory allocation for input lines, as well as alternatives in C++ that may simplify the task.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using fgets to read lines into a fixed-length buffer, recommending against using gets due to safety concerns.
  • Another participant raises the question of whether there is an upper limit on line length or the number of lines, emphasizing the need for dynamic memory allocation if memory capacity is the only constraint.
  • A different approach is proposed, where sufficient memory is allocated upfront based on expected input size, with the possibility of using memchr to find end-of-line characters.
  • Some participants discuss the advantages of using C++ instead of C, highlighting features like standard strings and vectors that handle memory management automatically.
  • It is noted that getline() in C++ can automatically expand to accommodate the entire line, and vectors can dynamically resize when items are added.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to handle the problem, with some favoring C and others suggesting C++ as a more efficient alternative. No consensus is reached on a single method or solution.

Contextual Notes

Participants mention various functions and techniques related to memory allocation and input handling, but there are no settled assumptions about the maximum input sizes or specific implementation details.

Who May Find This Useful

This discussion may be useful for programmers seeking to understand dynamic memory management in C or those considering the advantages of using C++ for similar tasks.

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
 
Technology 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:


Have you considered programming in C++ instead of in C?
Everything that works in C works in C++ as well, but C++ has features that makes what you want to do easier.
 


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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
Replies
3
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
5K
Replies
3
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K