Hi, I'm pretty new to programming and I have a question. I've created a function for a program that is supposed to delete 4 lines from a file. The program is movie database and I want it to take in the year(in this case 1994) and delete the year and the 4 lines below it. I've gotten to the point where it deletes 1 line, 1994. Can anyone help me figure out what I should do to delete 4?
Code:
void deleteLines(struct MovieDB *pMovies, int count){ FILE *pFile;//pointing to buffer
char line[50];
char *buffer;//pointers are 4 bytes, better than creating a buffer array
char *ptr;
printf("Enter a name you want deleted");
buffer = (char *)malloc(1000*sizeof(char)); //passes the type and returns the size of that type
//you want this buffer to initilized to all zeros ^
memset(buffer, 0, 1000*sizeof(char));
/*buffer is that start adress, the value you want to store in each element which is zero
The number of bytes which is the same as above. when you allocate a buffer you have to zero it
*/
ptr = buffer;
//open the file
pFile =fopen(FileName, "r");
if(pFile != NULL)
{
while (!feof(pFile)){
fgets(line, 50, pFile);
if(strcmp(line, "1994\n") != 0)//line is equal to "1994"
{
//put line in buffer
strcpy(ptr, line);//destination string, source string
ptr += strlen(line); //increments ptr by the length of the line. puts it back into ptr
}
}
fclose(pFile);
pFile = fopen(FileName, "w");
fprintf(pFile, "%s", buffer);
fclose(pFile);
}
else{
printf("Could not open the file.\n");
}
getch();
}