Well I rewrote my code to use your much neater nicer version Prelude, I also did what you suggested and counted the spaces inorder to put the brackets where i wanted them.
So far so good
Now, inorder to put the fullstop after the number at the beggining of each line i thought i would place a '.' after each digit and add a counter so that if the char got from the orginal file was the second digit on a given line it would place it over the character previously written (which would be a '.')
IE. it would get a '1' print it to file, then print a '.' to file, the if it got another number, say a '0', it would place it over the '.' just written and then put another '.'
So that i wouldn't get "1.0.", and instead have "10."
Thing is I thought i could just subtract 1 from the file pointer....... didn't work.
My code is:
Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *infile, *outfile;
int x, f, c=0;
if ( ( infile = fopen ( "results.txt", "r" ) ) != NULL &&
( outfile = fopen ( "modresults.txt", "w" ) ) != NULL )
{
while ( ( f = fgetc (infile) ) != EOF )
switch(f)
{
case '\n':
{
x=0;
c=0;
fputc ( ')' , outfile );
fputc ( ',' , outfile );
fputc ( ' ' , outfile );
}
break;
case ' ':
{
fputc ( f , outfile );
if ( ++x == 3 ) fputc( '(', outfile );
}
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
{
if (c++ == 1)
fputc ( f, outfile-1); /* problem line*/
else fputc( f, outfile);
fputc ( '.', outfile);
}
break;
default:
fputc ( f, outfile );
}
fclose ( infile );
fclose ( outfile );
}
else
fprintf ( stderr, "Error: File open failure\n" );
return 0;
}
Instead of getting "10." i get "1..", which remains the same if i change the pointer to outfile-2. IE. the second digit which should be printing over the first '.' isn't printing at all.
EDIT: Ah, solved it, found the fseek() function. All working as planned now. Thanks for your help Prelude.