modifying existing file using C

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring modifying existing file using C
# 1  
Old 08-30-2010
modifying existing file using C

Hi all,

I have a snmpd.conf file as below. in "SECTION: Trap Destinations" line I want to add "trap2dest <IP>:162 <com_str>" on a new line. For this I wrote following code

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        FILE *fp;
        ssize_t read_char_count = 0;
        char *line = NULL;
        size_t len = 0;
        char trapdest[] = "trap2sink 192.168.1.10:162 public";

        fp = fopen("/etc/snmpd.conf", "r+");
        if(fp == NULL)
        {
                perror("unable to open file");
                exit(2);
        }
        while((read_char_count = getline(&line, &len, fp)) != -1)
        {
                if(strcmp(line, "# SECTION: Trap Destinations\n") == 0)
                {
                        printf("%s\n", line);
                        fwrite(trapdest, sizeof(trapdest[0]), sizeof(trapdest)/sizeof(trapdest[0]), fp);
                        fflush(fp);
                }
        }
        if(line)
                free(line);
        fclose(fp);
        return 0;

}

The code runs but the newly added line eats the line which is below in conf file


snmpd.conf before executing above code
Quote:
############################################################################
# SECTION: Trap Destinations
#
# Here we define who the agent will send traps to.
#
# trap2sink: A SNMPv2c trap receiver
# arguments: host [community] [portnum]
#
###########################################################################
The snmpd conf after executing ablve code
Quote:
############################################################################
# SECTION: Trap Destinations
trap2sink 192.168.1.10:162 public^@ will send traps to.
#

# trap2sink: A SNMPv2c trap receiver
# arguments: host [community] [portnum]
#
###########################################################################
Please suggest me something so that I can fix this issue. Why the newly added line eats the line char that are next to it?

Appreciates your kindly help...

Thanks...
# 2  
Old 08-30-2010
There is no "insert" operation for files, and never has been. Text editors and the like accomplish it by reading the entire file into memory then writing back the data they want. Anything after the change will have to be rewritten if the length changes. (The file might even need to be truncated, if it ended up shorter than before.)
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-31-2010
Thanks Corona688, I have changed the approach now. Will use stad. net-snmp based solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying listener file

Hi Pros, I'm writing a script to modify listener.ora file on multiple hosts. When I ssh to any server from a central server in our environment we are presented with menu to select the instance. I need to set my environment to listener which could be different number on every instance. How can I... (5 Replies)
Discussion started by: humble_learner
5 Replies

2. Shell Programming and Scripting

Modifying file to 75 characters

I have a text file containing some notes and I want to limit the lines to 75 characters. Tried using fold, however fold will cut words. Need something in bash, sed or awk to do this. Find the blank space less than 75 ant cut from there (10 Replies)
Discussion started by: kristinu
10 Replies

3. Shell Programming and Scripting

Help in modifying existing Perl Script to produce report of dupes

Hello, I have a large amount of data with the following structure: Word=Transliterated word I have written a Perl Script (reproduced below) which goes through the full file and identifies all dupes on the right hand side. It creates successfully a new file with two headers: Singletons and Dupes.... (5 Replies)
Discussion started by: gimley
5 Replies

4. Shell Programming and Scripting

Modifying a file?

Hi, I want to convert a file that looks like this >joe XXXXXXXXXXXXXXXXXXX >man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS to something that looks like this (where the spacing is tab seperated) joe XXXXXXXXXXXXXXXXXXX man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS I am able to do the reverse but the other... (3 Replies)
Discussion started by: kylle345
3 Replies

5. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

6. UNIX for Dummies Questions & Answers

Modifying a .ksh file

Hi, i have created a simple .ksh file in the following manner cat <<EOF >mfile #!/bin/ksh echo "hello world" EOF I have 2 questions 1. now i would like to add a second line after the first echo command e.g. echo "this is line 2" how can i do that ? 2. I would then like... (1 Reply)
Discussion started by: corbusier
1 Replies

7. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

8. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

9. Shell Programming and Scripting

Modifying file from outside

hi , I have a javascript file like this: function fnValidateId(){ var array_id = new Array("444","7888","6633","555","146562","3333","33332") var tmp_id = document.form1.id.value; var num=0; while(1) { if(tmp_id ==... (9 Replies)
Discussion started by: Sreejith_VK
9 Replies

10. Shell Programming and Scripting

can someone help me with modifying this file

Hi, I have a file which has data kind of like this. Apr 13 08:20:38 uslis10a sendmail: m3DDKSx3006432: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKSoK006433: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKcSo006442: usliss26.ca.com Apr 13 08:20:38 uslis10c... (2 Replies)
Discussion started by: eamani_sun
2 Replies
Login or Register to Ask a Question