tags:

views:

118

answers:

4

Did i set my file pointer wrong?

+3  A: 

You're opening ReadMe.txt for writing:

fopen_s( &fptr2, filename, "w" )

but then passing it to CharReadWrite:

CharReadWrite(fptr2);

which tries to read from it:

c=fgetc(fin)

Did you mean to open it for reading, by passing "r" to fopen_s?

You also carry on and call CharReadWrite if opening ReadMe.txt fails.

RichieHindle
+1  A: 

One problem is that you are attempting to read from (and then close) fptr2 even if you failed to open it.

Try:

#include "stdafx.h"
#include "string.h"
#include "stdio.h"
void CharReadWrite(FILE *fin);
FILE *fptr2;

int _tmain(int argc, _TCHAR* argv[])
{   

    char filename[]="ReadMe.txt";
    if (fopen_s( &fptr2, filename, "w" ) != 0 )
    {
      printf( "File stream %s was not opened\n", filename );
    }
    else
    {
      printf( "The file %s was opened\n", filename );
      CharReadWrite(fptr2);
      fclose(fptr2);
    }
    return 0;
}
finnw
A: 

The problem is this line:

if (fopen_s( &fptr2, filename, "w" ) != 0 )

When you pass the 'w' flag, "Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."

Instead, use the "r+" flag.

samoz
A: 

You're opening file for writing and trying to read from it (fgetc) ...

stefanB