Solved

read and store a text file !

Posted on 2004-08-31
5
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11948522
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
ID: 11948528
found a bug:
      fo = fopen(argv[2], "r");
must be:
      fo = fopen(argv[2], "w");

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11948544
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
ID: 11949065
> 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
ID: 11949160
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

726 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