well I did do a fflush of the file between read and write but the end result was the same. I keep getting the stuff written at the end only
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *file;
void commentremover(FILE *tmp)
{
char o;
int i = 0;
char input[] = ".";
while((o=fgetc(tmp)) != EOF)
{
if(o=='\n')
{
printf(" %d \t:*LF*:\n", i , o);
}
else
{
if(o=='\x0D')
{
printf(" %d \t:*CR*:\n");
}
else
{
if(o==' ')
{
fflush(tmp);
fopen("out.txt", "r+");
fwrite(input,1,1,tmp);
}
else
{
printf(" %d \t:%c:\n", i , o);
}
}
}
i++;
fflush(stdout);
}
}
int main()
{
file = fopen("out.txt", "r+");
commentremover(file);
return 0;
}
I havent reached the part of recognizing a comment yet since my first task would be to succesfully identify a character and substitute it for any other.
Code:
if(o==' ')
{
fflush(tmp);
fopen("out.txt", "r+");
fwrite(input,1,1,tmp);
}
EDIT: reseting the pointer tmp seems like a bad ideia since we do need to know where in the file we need to write. So I dropped that ideia. Any other thoughts? I also used fputc which produced the same result. I read the help files (man) on both functions and since I'm not using append mode the only other reason that comes to mind would be that something goes wrong with the writing (fputc writes in append mode when that happens). The ideia of this test part would be to substitute the spaces with dots in a .txt file. I also made some printf to monitor the advance of the search across the file (it stops at the first space)
out.txt:
Code:
Beggining of the file
Second line
/* comment one */
// line comment
END OF FILE
I get at stout (after I disregard the fflush and the fopen):
0 :B:
1 :e:
2 :g:
3 :g:
4 :i:
5 :n:
6 :i:
7 :n:
8 :g:
and ends there