C/How to overwrite a specific line in a file?
Expert: Narendra - 8/20/2006
QuestionHi,
This question is in fact a follow up on my first question that I asked you earlier on about the abnormal abortion of my program.
Based on your advise, my program finally managed to work smoothly until I tried to overwrite a line in a file. I tried to use fputs to overwrite a line in a file as well as fwrite to overwrite the line too, but both ways failed.
I wonder what goes wrong in my codes. With this enquiry, I attach my codes for your expert inspection.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *PATH = "c:\\append\\208159A.LST";
int main(int argc, char *argv[]){
char *line = NULL, *tmp = NULL;
int count = 1;
FILE *inFilePtr = NULL;
argv[1] = PATH;
if (argc != 1){
printf("Usage: Auto append a % before every DFS\n");
printf("Fail to run due to lack of input: PATH\\FILENAME.\n");
}
else{
if ((inFilePtr = fopen(argv[1], "r+")) == NULL){
printf("Path is invalid! Or input file could NOT be opened!\n");
}
else{
fseek(inFilePtr, 0, SEEK_SET); // Set inFilePtr to the beginning of ARGV[1]
tmp = (char *) malloc(100);
line = (char *) malloc(100);
while (!feof(inFilePtr)){
fgets(tmp, 100, inFilePtr); // Read each line in the file
printf("%s", tmp);
// chance = fputs(tmp, outFilePtr);
// printf("%d", chance);
if (strncmp(tmp, "%", 1) == 0){
fgets(tmp, 100, inFilePtr);
printf("%s", tmp);
}
else
if (strncmp(tmp, "(DFS", 4) == 0){ // Detect for '(DFS' in ARGV[1]
strcpy(line, "%");
strcat(line, tmp);
count++;
printf("%d\n", fputs(line, inFilePtr));
if (fwrite(line, sizeof(line), 1, inFilePtr) != 0) // Insert a % before each detected '(DFS'
// if (fputs(strcat(line, tmp), outFilePtr) >= 0) // Insert a % before each detected '(DFS'
printf("%s", line);
else{
printf("Failed to insert '%' before %d (DFS", count);
break;
}
}
}
fclose(inFilePtr);
}
}
return 0;
}
I hope the codes above would be properly indented for your viewing. This program supposes to search for (DFS in the file and will insert '%' before it if there is no '%' found.
Hope to hear from you soon. Thank you! :)
AnswerAre you getting any errors here or your o/p is not correct?
I think there are two mistakes in your code.
1. You are opening the file in read mode and then trying to write. Open it in "a" (append) mode and try the same
2. This mistake is more important.
You are trying to overwrite. So, whatever that was there before gets overwritten.
Suppose, your original line is:
ONE
TWO
The size of this line is 3. And there is a '\n' character which is not visible.
Now, you want to append a '%' character to this.
So, your new lines become
%ONETWO
O was overwritten with %.
N was overwritten with O.
E was overwritten with N.
'\n' was overwritten with E.
As you can see from the above illustration, your file will become garbled because of this. It is ok till the time, you overwrite with exactly same number of characters.
So, the solution to this is, "don't overwrite".
Instead, write into a new file and after closing both the input and output files, rename the o/p file as i/p file.
Hope this helps.