Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program in C (without using any standard tools or library functions) to print the top N or/and bottom N lines of a given set of files. Print the first/last N lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
The way to invoke the program is:
a.out –q –h N –t N File1 File2 ....Filen (-h N – Print the top N lines from each file
-t N – The bottom N lines from each file)
-q – Do not print the header giving the file names

What I have tried:

tried to compile the code using different files.
Posted
Updated 11-May-20 21:22pm
Comments
Richard MacCutchan 12-May-20 3:39am    
What code have you tried to compile? If you have a problem then you need to explain the details. We cannot help unless we can see what the code is trying to do.

Roughly speaking you have two task:
  1. Parse the command line (for options and arguments: the file names).
  2. Read the files line by line, extracting the relevant info and reporting it.

The very fact that each option must be preceded by an hyphen simplyfy task (1).
For task (2) I would suggest you reading a good tutorial on C file I/O. See, for instance Basics of File Handling in C - GeeksforGeeks[^].
 
Share this answer
 
Comments
Maciej Los 12-May-20 3:39am    
Short And To The Point!
Member 14829241 14-May-20 10:05am    
thank you so much but it would be really helpful if you could tell me what the code would be because im having errors. and this needs to be submitted as a project and i just cannot seem to get it right
See example here to get an idea how to handle files: C Program to Find the Number of Lines in a Text File[^]

For handling command line arguments, see: C Command Line Arguments | W3Schools | Tutorialspoint | W3Adda[^]
 
Share this answer
 
v2
Comments
Member 14829241 14-May-20 10:06am    
thank you so much but it would be really helpful if you could tell me what the code would be because im having errors. and this needs to be submitted as a project and i just cannot seem to get it right
Maciej Los 14-May-20 13:56pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900