I am writing a simple C program using a while loop to enter numbers to a file. I'm very new to this so please don't say it's obvious.
Code:
#include <conio.h>
#include <stdio.h>
int main()
{
int neilsdata[5];
int val, pos;
FILE *neilfile;
int cont = 1;
char contch;
// open file
if( ( neilfile = fopen( "vals", "wb" ) ) == NULL )
{
printf("Couldn't create file\n");
exit( -1 );
}
// write data to file
while ( cont==1 )
{
printf("Enter a value\n");
scanf("%d", &val);
fwrite( &val, sizeof val, 10, neilfile );
printf("Do you want another value\n");
scanf("%d", &contch);
if( contch == "y" )
cont = 1;
else
cont = 0;
}
fclose( neilfile );
// read in same file
// open file
if( ( neilfile = fopen( "vals", "rb" ) ) == NULL )
{
printf("Couldn't open file\n");
exit( -1 );
}
fread( &val, sizeof val, 1, neilfile );
while( !feof( neilfile ) )
{
printf (&val, "\n");
fread( &val, sizeof val, 1, neilfile );
}
fclose( neilfile );
getch();
getch();
}
After taking one value and entering y/n it just throws a whole line of unhelpful characters and ends there. Something with the loop, or the scan command?