Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read and store a text file !

Posted on 2004-08-31
5
Medium Priority
?
267 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 2000 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

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!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

636 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