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

  • Thread starter rambo3131
  • Start date
In summary, the problem is that you will read arbitrary number of character lines from standard input. The lines are separated with new line character. After the last line, you should mark the end of the lines with a null pointer.
  • #1
rambo3131
18
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
  • #2


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).
 
  • #3


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.
 
  • #4


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:
  • #5


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.
 
  • #6


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.
 

1. What is C and how is it related to Linux?

C is a programming language that is widely used for building operating systems, including Linux. It is a low-level language that provides direct access to system resources, making it ideal for creating efficient and powerful software for operating systems.

2. Can I use C on Linux without any special setup?

Yes, most Linux distributions come with a C compiler pre-installed. However, if you want to use the latest version or specific tools, you may need to install additional packages.

3. What is the problem you are facing with C on Linux?

I am having trouble compiling my C code on Linux. When I try to run the program, I get an error message or it does not produce the desired output.

4. How do I compile and run my C code on Linux?

To compile C code on Linux, you will need to use a compiler such as gcc. You can use the command "gcc .c" to compile your code. To run the program, use the command "./a.out" or specify a different output file name using the "-o" flag.

5. Are there any resources or tutorials for learning C on Linux?

Yes, there are many online resources and tutorials available for learning C on Linux. Some popular options include the "C Programming Language" book by Kernighan and Ritchie, online courses on platforms like Udemy and Coursera, and official documentation from the Linux community.

Similar threads

  • Programming and Computer Science
Replies
5
Views
848
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
843
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
925
Back
Top