Thread: file manipulation help

  1. #1
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7

    Unhappy file manipulation help

    Hi all,
    first of all congrats about this great forum it has been a real help for me since I started to learn C (that is 3 months ago ).
    As you understand I am a complete newbie so I would like you to help me in a program that I am trying to develop. (shouldnt be too hard for you)

    The thing is that I must develop a program which will take 2 (or 3) parameters

    1) MaxKeepAliveRequests or Timeout
    2) a value
    3) -M (which is optional)

    My problem:
    now this program must search for the word MaxKeepAliveRequests or Timeout in a file called httpd.conf (the apache server one), find the word and replace the value after the "=" sign.


    -M will update the new value only if its higher than the value in the file.

    for example: UPDCONF Timeout 32

    should check the file httpd.conf which looks like this
    MaxKeepAliveRequests = 2
    Timeout = 5

    find Timeout and make the value 32.


    So far what I have done is change the value of ALL variables in a file using fscanf and fprintf.
    Oh and the only way that I managed to read the file was to remove the = sign and have only one space instead of it which is wrong.

    This is what I've made so far including a sample httpd.conf file

    RapidShare: 1-CLICK Web hosting - Easy Filehosting

    I hope you didnt get dizzy from my post

    (just to inform you)
    I copy the file httpd.conf in "r" mode then I copy itself to httpd.temp and I read it in "w" mode then I rename .conf -> .bak and .temp -> .conf

    Thank you,
    Fiveco
    Last edited by fiveco; 12-08-2009 at 10:19 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fiveco View Post
    I copy the file httpd.conf in "r" mode then I copy itself to httpd.temp and I read it in "w" mode then I rename .conf -> .bak and .temp -> .conf
    Read it in once, write it out once. So either load the entire thing into memory, which is fine, or else work line by line and write that out to a tmp file, then move the tmp (to overwrite http.conf) with a system command (mv, on *nix).

    Make sure you keep a back up of your httpd.conf

    One easy way to load the whole thing into memory is to get the file length with stat, then create a single char array for the whole file:
    Code:
    char filetext[filelength+1];
    Pretty easy to apply strstr() to find your variable, too.
    Last edited by MK27; 12-08-2009 at 10:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    Thanks for the tips!

    so you think strstr() is needed to find the variable in a file?
    How can i do that with fscanf and fprintf (or i cant?)

    As far as I know fscanf and fprintf is a bad choice is that true?
    If yes what else do you suggest me to use?

    once again thank you

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would use fgets() to read the file in line by line, but strcat it onto the "filetext" string. Since reading all of a text file into a buffer is something you will probably do again in another C program, you might as well write a function like this:
    Code:
    char *linefile(char *filepath);  /* prototype */
    char *filedata = linefile("/etc/apache2/httpd.conf"); /* example call */
    You give it the absolute path of a file, fopen() it, use fstat() to get the length, create the necessary char buffer with malloc(), then use a while loop with fgets to strcat into the buffer, fclose, and return the buffer pointer.

    Use strstr to look for "\nTimeOut" -- because it should be at the beginning of the line, and you don't want anything in comments. Then you should be able to uses fscanf on the pointer returned by strstr(), maybe with a little pointer arithmetic, to get the integer value.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by MK27 View Post
    I would use fgets() to read the file in line by line, but strcat it onto the "filetext" string.
    ...
    You give it the absolute path of a file, fopen() it, use fstat() to get the length, create the necessary char buffer with malloc(), then use a while loop with fgets to strcat into the buffer, fclose, and return the buffer pointer.
    Since you already know the size of the file, why not use a single call to fread()?
    If you understand what you're doing, you're not learning anything.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itsme86 View Post
    Since you already know the size of the file, why not use a single call to fread()?
    Good call. You can replace the whole strcat/while loop that way.

    Just remember to NULL terminate the buffer then, fread() won't do that.
    Last edited by MK27; 12-08-2009 at 11:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    actually the file isnt always the same in size what i left out (since it didnt matter)
    was that if the user types for example updconf hello 55 it will add the word hello and the value 55 next to it in the file.
    Last edited by fiveco; 12-08-2009 at 11:29 AM.

  8. #8
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    wow great tips!!
    I lost ya somewhere guys though : /

    my
    Code:
    while (fscanf(ifp, "%s %s", fstring, &score) == 2) {               
                    fprintf(ofp, "%s %s\n", fstring, argv[2]); 
                    }
    should now changed to (?)
    Dont forget you are talking to a C newbie

  9. #9
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    nevermind all the previous.
    I will do something simple
    given that the "httpd.conf" file is something like that
    Code:
    MaxKeepAliveRequests 0
    Timeout 0
    the fscanf i am using looks like that
    Code:
    while (fscanf(ifp, "%s %s", fstring, &score) == 2) {               
                    fprintf(ofp, "%s %s\n", fstring, argv[2]); 
                    }
    is there anyway using the fscanf and fprintf to edit the first or the second variable only?

    with my while what it does is change both values.

    Thank you again,
    Fiveco

  10. #10
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    any help guys?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fiveco View Post
    nevermind all the previous.
    I will do something simple
    given that the "httpd.conf" file is something like that
    Code:
    MaxKeepAliveRequests 0
    Timeout 0
    the fscanf i am using looks like that
    Code:
    while (fscanf(ifp, "%s %s", fstring, &score) == 2) {               
                    fprintf(ofp, "%s %s\n", fstring, argv[2]); 
                    }
    is there anyway using the fscanf and fprintf to edit the first or the second variable only?

    with my while what it does is change both values.

    Thank you again,
    Fiveco
    So first, put the equal sign back into the file, OK? There's no sense in making a program that runs right, on a file that has been altered, so it isn't right.

    Second, post up a copy of the actual file, to rapidshare, and post the link here.

    Third, how do you differentiate a MaxKeepAlive request, from a Timeout request, in the programs input? Be precise, please.

    Same with M - how do you differentiate M from a MaxKeepAlive or Timeout request?

    Post up 3 or 4 typical input for the program, one per line.

    The program is trivial, *IF* you can show the exact input the program deals with, the exact calculations you want done on the various values, and the output you need.

    Except for this M thing, I think I've got the output you need nailed down.

    I'll code up something as simple as possible.

  12. #12
    Registered User fiveco's Avatar
    Join Date
    Dec 2009
    Posts
    7
    well I think I found a way of writing correctly to a file...
    Thanks a lot guys your help is much appreciated!!!
    If I have more problems I'll post them.
    Once again, thank you very much!

    Regards,
    Fiveco

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM