Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 26
hello, i'm new to c and i'd like to parse some infos in linux logs (such as syslog). What functions should I use? i've not understood how fscanf works, and ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    29

    parsing logs in c

    hello,
    i'm new to c and i'd like to parse some infos in linux logs (such as syslog).
    What functions should I use?
    i've not understood how fscanf works, and i'm not even sure that's the one i should use
    can you help me please?
    thanks.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    homework? if not, why reinvent the wheel. you can use already recognized tools such as awk, grep etc to parse your logs.

  3. #3
    Just Joined!
    Join Date
    Nov 2009
    Posts
    29
    I want to use C, not bash functions such as grep.
    It's not a homework, but it was suggested from a teacher of mine. I want to do this to practice writting in C.
    Can you give me some hints as how to parse it?

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I would recommend gawk/awk, it was developed for this sort of thing...
    Make mine Arch Linux

  5. #5
    Just Joined!
    Join Date
    Nov 2009
    Posts
    29
    I just want someone to tell me how to program it in C, i want to do it myself, not use an existing software

  6. #6
    Just Joined!
    Join Date
    Mar 2007
    Posts
    7
    Nobody seems to want to answer your question. I recently used my linux laptop to prototype some code for work, which had to run under a dos OS and a lattice compiler from the 80's.

    I found that the NetBeans IDE was a good package for working with c programming, so I would recommend you download that application to your linux computer.

    For my prototyping, I processed a text file, picking parts of the data string apart.

    I used fopen to get into the file, fgets to read in a line of data, and strncpy to pick substrings out of the line of data.

    Hope this helps.

  7. #7
    Just Joined!
    Join Date
    Nov 2009
    Posts
    29
    What I don't understand is how fgets really works (even though i've read the man) : how do I access the second line?
    I mean, if I use fget(s,1000,fp) twice, won't I always get the first line?
    Another question: In a very long file, how would I access the 500th line quickly? (without reading every char that leads there).

  8. #8
    Just Joined!
    Join Date
    Mar 2007
    Posts
    7
    The first thing you do is open the file (fopen). This establishes a pointer to the first line in the file. As long as you don't close the file, each call to fgets reads in a line from the file and advances the pointer to the next line. You can not back up and you can not have random access (reading record 500 without reading the first 499). If you want the 500th record, you could write a function that you pass in the record (line) number that you want and the function returns that line as a string. It won't take a noticeable amount of time, and a few hundred lines would not be a big deal.

  9. #9
    Just Joined!
    Join Date
    Nov 2009
    Posts
    29
    What about fseek and other functions like that?
    How do they work?
    Do they actually read all the chars that lead to the 500th too?

  10. #10
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You need to get a good C language reference book which will explain all of this stuff. There is a lot to learn and while this forum is good to help with specific problems, it is not (in my opinion) intended as a general purpose tutorial forum. Two books I have found useful for C programming references are:

    The C Programming Language, by Kernighan and Ritchie
    C - The Complete Reference, by Schildt

    The book by Kernighan and Ritchie is considered the "bible" of the C language. It is small, dense, and very authoritative. The book by Schildt is bigger and in many respects more approachable for newbies. If you are planning on becoming a C programmer, then both books belong on your shelf.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...