• Advertisement
Sign in to follow this  

C - File I/O problem

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems with my C programming for file I/O. Once, it reads it can't write anymore or visa versa. I read some examples that people either read or write, I'm not sure if its the problem. I'm using VC++ 6.0 Ok, the problem is everytime after the fscanf ( fp, "%c", &temp ) ; the fprintf ( fp, "\n" ) ; never works, I have to commond out the fscanf statment ! Don't know why

#include <stdio.h>
#include <stdlib.h>

int main ( void )
{

	FILE *fp ;
	int i = 0, * j = 0 ;
	char temp = 0 ;

	fp = fopen ( "test.txt", "rw+t" ) ;

	fprintf ( stderr, "%d %d %d\n", fp, *fp, &fp ) ;

	if ( fp != NULL )
	{
		fprintf ( fp, "testing ^" ) ;
		fseek ( fp, -2, SEEK_CUR ) ;

		fprintf ( fp, "testing ^" ) ;
		fseek ( fp, -2, SEEK_CUR ) ;

		fscanf ( fp, "%c", &temp ) ;
		fprintf ( stderr, "%c", temp ) ;

		fprintf ( fp, "\n" ) ;
		
	}

	fclose ( fp ) ;

	return 0 ;

}


Share this post


Link to post
Share on other sites
Advertisement
I have not used C file io in a while but i think you want to open the file with r+, and for binary r+b or rb+

Share this post


Link to post
Share on other sites
Just use fopen("test.txt", "r+");

And, you must fflush if you want to write directly after doing a read.

See here:
http://www.opengroup.org/onlinepubs/007908799/xsh/fopen.html

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement