User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 13 Jan 2003, 11:52   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
C: replacing a line in a file....

How can I replace a line in a file? Is there actually a C function for that as none is listed in my book.

I want to search for a line containing a certain word, then replace the word and value behind it.
Structural Integrity is offline   Reply With Quote
Unread 13 Jan 2003, 13:01   #2
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
If you're using C you could probably do something with fgetc (scan for 10/13 and count them, so you know which line you're on).

As for replacing, the only way I know is to rewrite the file, although I've never done much with file operations.

If you allready know the line to replace, the pseudocode might go something like:

open file
open newfile
copy contents of file to newfile until you reach the line to replace
scan line for word, store file pointer value at the START of the word
move file pointer backwards to start of line again
copy the line to newfile UP TO the word position
output the new word
scan to the next value 32 (the space between the word you want to skip output of and the next one)
continue copying til EOF
erase file, rename newfile

Obviously, easier said than done.
[edit]
Pseudocode corrected - i missed the bit about you wanting to look for a word
[/edit]
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.

Last edited by meglamaniac; 13 Jan 2003 at 13:12.
meglamaniac is offline   Reply With Quote
Unread 13 Jan 2003, 13:07   #3
Pitchfork
Tourist
 
Join Date: Jun 2001
Location: moon
Posts: 90
Pitchfork is an unknown quantity at this point
I don't know a function for replacing a line in C, what I'd do is:
- open the input file for read
- open a temporary (empty) file for write
while(!eof(inputFile)) {
- read line from input file
- search for your word
- if it contains the word replace the line with the new one else leave it as it is
- write the line to the temp file
}

now you should have what u wanted in the new file, pretty easy, eh
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
Pitchfork is offline   Reply With Quote
Unread 13 Jan 2003, 13:28   #4
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Easy? I was actually hoping that there was a way to replace a line...

=/

Oh how I miss mIRC script.... life was so much easier back then...
Structural Integrity is offline   Reply With Quote
Unread 13 Jan 2003, 18:50   #5
Slidey
Registered User
 
Join Date: Mar 2001
Posts: 205
Slidey is an unknown quantity at this point
Code:
int grep(char *data, char *grepfor1, char *grepfor2)
{
    unsigned char *walker, *buf2;
    char *line;
    char buf[255];
    int hit = 0;

    buf2 = mmalloc(strlen(data) + 1);
    memcpy(buf2, data, strlen(data) + 1);

    for (walker = buf2; *walker; walker++)
        if ((*walker < 32 || *walker > 127) && *walker != '\n' && *walker != '\r' && *walker != 9)
            *walker = '.';

    walker = buf2;
    while ((line = strsep(&walker, "\n"))) {
        //if (opt->debug > 1) printf("grep %s\n", line);
        if (strstr(line, grepfor1) || strstr(line, grepfor2)) {
            //if (opt->debug > 1) printf("^^^ HIT\n");
            snprintf(buf, sizeof(buf), "%s\n", line);
            hit++;
        }
    }
    free(buf2);

    return(! hit);
}
or thereabouts
__________________
#linux - home of idiots

#impulsed - home of genius..?
Slidey is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 06:25.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018