Ok...so I'm trying to write a simple program to write the letter "a" into a text file. The problem is, is that when I already have things written in the text file, and run the program, it deletes all the other stuff in the file, and puts "a"...I have no idea how I can put the letter "a" after all the other stuff(whatever that may be) in the file.
I'd also like to learn how to find certain text in a file, then based on where that is, place some data after the text that was found...I hope I explained that well.
So if someone could lead me in the right direction, I'd really appreciate it. I've learned to do the following code from a book, but it didn't teach me how to do either of the things above...And I am trying to think of a way to do it, but can't come up with one. Nor has google helped(might just be searching for the wrong sorta thing).
Code:
Code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *filePtr;
char fileContents;
char c;
c = 'a';
filePtr = fopen("../../test.txt", "r");
if(filePtr == NULL) {
printf("Error opening file.");
}
else
{
while((fileContents = fgetc(filePtr)) != EOF)
{
putchar(fileContents);
}
fclose(filePtr);
}
filePtr = fopen("../../test.txt", "w");
if(filePtr == NULL)
{
printf("Error opening file, to write to it.");
}
else
{
fputc(c, filePtr);
}
return 0;
}