• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

read and store a text file !

hi experts,

I have a system log file which is in text format ! it has many lines day by day !! I want to  write a C shell script (*.c) to read in the first 150 lines and store it to another text file "????.log".  ???mean the date I type in !! So that I run the script once everyday, I can get the log daily !!

any idea ?
thanks !

Mike
0
mmccy
Asked:
mmccy
  • 3
1 Solution
 
Jaime OlivaresCommented:
Something like this:

#include "stdio.h"
#define LINE_SIZE 250  /* set a proper max line buffer size */

int main(argc, argv)
int argc;
char **argv;
{
      FILE *fi, *fo;
      int i;
      char buffer[LINE_SIZE];

      fi = fopen(argv[1], "r");
      if (!fi) {
           printf("Error openning input file: %s\n", argv[1]);
           return 1;
      }

      fo = fopen(argv[2], "r");
      if (!fo) {
           printf("Error openning output file: %s\n", argv[2]);
           return 1;
      }

      for (i=0; i<150; i++) {
            fgets(buffer, LINE_SIZE, fi);
            fputs(buffer, LINE_SIZE, fo);
            if (feof(fi))
                 break;
      }
      fclose (fi);
      fclose (fo);
      return 0;
}

I don't know if this exactly covers your requierement. Let me know if not.
Jaime.
0
 
Jaime OlivaresCommented:
found a bug:
      fo = fopen(argv[2], "r");
must be:
      fo = fopen(argv[2], "w");

0
 
Jaime OlivaresCommented:
Forgot to tell you. If you compile it as, let's say, updatelog, you must invoke it as

updatelog <infilename> <outfilename>
0
 
brettmjohnsonCommented:
> I want to  write a C shell script (*.c)

Well the c shell and the C programming language are very different.
Satisfying your requirements in the cshell is trivially easy:

head -n 150 logfile > `date "+%Y%m%d"`.log

0
 
sunnycoderCommented:
Or better still, let the system do it for you everyday rather than run the program yourself
http://iain.cx/src/logrotate/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now