Following is dummyFile.txt
cc:=gcc
file:=good
src:=$(file).c
executable:=$(file).exe
$(executable):$(src)
$(cc) $(src) -o $(executable)
clean:
del $(executable)
It is a simple kind of makefile but it is stored in the text file mentioned above.
Following is test.c which will ultimated gentrate test.exe when compiled. It has been compiled without warnings and errors so I provide concerning part of the code snippet here.
FILE* fp = fopen("dummyFile.txt", "r+");
if (!fp) {
printf("Invalid FileName or it does not exist!");
exit(1);
}
int count;
do {
//scans the whole line
fscanf(fp, "%[^\n]c", buffer);
count = strlen(buffer);
//skips next char while is newline
fscanf(fp, "%*c");
//looking if string is "file:"
sscanf(buffer, "%[^=]c", t);
if (!strcasecmp(t, "file:")) {
char temp[20];
//Copying the content or simply filename to temp variable
strncpy(temp, argv[1], strlen(temp));
//as windows sets \r\n as padding i did increment 2 for count
count += 2;
//i am at the next line from "file:" part so this will move my pointer to where "file:" is
fseek(fp, -count, SEEK_CUR);
//content overwritten
fprintf(fp, "file:=%s\n", temp);
return 0;
}
} while (!feof(stdin) && !ferror(stdin));
I did :
./test newfile
to run the file. And the only motive of this program is to replace that very line (mentioned below) from **dummyFile.txt**
file:=good
to the following :
file:=newfile
In fact, the aim is only to modify or replace the part after file:= by the given argument while running the program. As length of file:=good is less than file:=newfile. It should do the job I guess. I was wrong and the changes I see is the following :
cc:=gcc
file:=newfile
:=$(file).c
executable:=$(file).exe
$(executable):$(src)
$(cc) $(src) -o $(executable)
clean:
del $(executable)
Doing a simple analysis. I found the content is overwritten as file:=good was changed to file:=newfile but from the line3 of original file [src:=$(file).c] src was missing and turns into [:=$(file).c].
I want suggestion what to change in my program or my approach. I will follow it strictly.
EDIT
Creation of temp_file to copy all lines as it is excluding the intended line would have been the option. (adding intended changes). But I guess there is always a way to my approach.
I don't know what your constraints are, but here is one way to do what was suggested in the comments. I assumed you know what pointers are, since you used one in
FILE *fp. I also assumed you know how to usemalloc, but if you don't you can always use a static array that's large enough to fit your file.The idea is that, since working on files directly is cumbersome, you can copy everything into an array of bytes in memory, and work with it there, then place everything back in the file. You are basically doing operations on strings, the fact that the string comes from a file (and will go back into it) is irrelevant.
Note 1: This implementation doesn't do any error checking for clarity's sake Note 2: A few white spaces are added at the end, but you can debug that yourself - don't copy-paste this code, understand it and rewrite it yourself, in a way that YOU are comfortable with.