View Single Post
Join Date: Dec 2008
Posts: 1
Reputation: dan_armstrong is an unknown quantity at this point 
Solved Threads: 0
dan_armstrong dan_armstrong is offline Offline
Newbie Poster

Re: Best way to skip first line?

 
0
  #7
Dec 10th, 2008
Here is what I did as the "fastest" way to skip the first line. Fastest=best for my situation.

Shell Scripting Syntax (Toggle Plain Text)
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4.  
  5. #define BUFFER_SIZE 4096
  6.  
  7. int main() {
  8. // Use block I/O
  9. char buff[BUFFER_SIZE];
  10. int firstLine = 1;
  11. size_t numRead;
  12. while((numRead = read(STDIN_FILENO, buff, BUFFER_SIZE))!=0) {
  13. if(numRead==-1) {
  14. perror("read");
  15. return 1;
  16. }
  17. size_t writePos = 0;
  18. // Skip first line
  19. if(firstLine) {
  20. while(writePos<numRead) {
  21. if(buff[writePos++]=='\n') {
  22. firstLine = 0;
  23. break;
  24. }
  25. }
  26. }
  27. while(writePos<numRead) {
  28. size_t numWritten = write(STDOUT_FILENO, buff+writePos, numRead-writePos);
  29. if(numWritten==-1) {
  30. perror("write");
  31. return 1;
  32. }
  33. writePos += numWritten;
  34. }
  35. }
  36. return 0;
  37. }
Reply With Quote