I've been trying to read and write from a file at the same time and I'm trying to substitute all the tabs in the content of text.txt to be turned into spaces. This is my code:
int main()
{
FILE* filePtr = fopen("text.txt", "w+");
char c;
c = fgetc(filePtr);
fpos_t num;
while(c != EOF)
{
if(c == '\t')
{
fgetpos(filePtr, &num);
num--;
fsetpos(filePtr, &num);
fputc(' ', filePtr);
}
c = fgetc(filePtr);
}
}
The content of text.txt is as such:
Hi \t my \t name \t is \t jack!
When I run this code, my output in the text.txt file is just blank space. There are no characters there. What should I do so that the substitutions work out as intended?