Is there any other way to read and change value from some line in config file than to jump to specified line and parse whole string from it? I also need to do some modifications (change value) on that parsed line, and write it back to file.
Is there any other way to read and change value from some line in config file than to jump to specified line and parse whole string from it? I also need to do some modifications (change value) on that parsed line, and write it back to file.
Do you know the line number or do you have to find out what line number that the text string you want to edit is located on? It is easy to read a text file line by line.
You could try to open the file and read the line that you want, edit it by writing a new line, than overwrite the file at that line location with the edited line of text. It might work.
I tried it finally. This was the result. It overwrote the original line however the new line was not as long as the original, so it looks like it could be a problem.
Code:Here is some text line 1 Here is more text line 2 A new line is here.xt line 3 This is the the last line of text line 4Code:#include <stdio.h> int main(void) { FILE *fptr = fopen("/pathname/news.txt", "r+"); if (fptr == NULL) exit(1); char buffer[256]; char newline[] = "A new line is here."; int line = 2; while( fgets(buffer, 256, fptr) != NULL) { line -=1; if (line == 0) { fflush(fptr); fputs(newline, fptr); } } return 0; }Code:
In my config files, I parse the whole file, store it in memory, and if I need to change something in the file, I truncate the file and write back the whole thing from the in-memory representation. Unless the config file is absolutely huge, this is the best way I've found to do it, but your needs may be different from mine.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?