Solved

read and store a text file !

Posted on 2004-08-31
5
262 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 177
Why adding test code changes the build ? 11 132
outlook office 365 8 119
If a cell in a range equals "YES" return specific Test. 4 27
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…
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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

813 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

10 Experts available now in Live!

Get 1:1 Help Now