Solved

read and store a text file !

Posted on 2004-08-31
5
259 Views
Last Modified: 2010-04-15
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
Comment
Question by:mmccy
  • 3
5 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
found a bug:
      fo = fopen(argv[2], "r");
must be:
      fo = fopen(argv[2], "w");

0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Forgot to tell you. If you compile it as, let's say, updatelog, you must invoke it as

updatelog <infilename> <outfilename>
0
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
> 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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now