Hi guys I need help
I'm writing a simple program than asks user to entere string and then write this to a file
I'm using functions for both write and read:
For writing to a file:
Code:
void Write(char string[])
{

if((fp=fopen("Test.bin","a+b"))==NULL) exit(1);
fwrite(string,sizeof(char),strlen(string)+1,fp);
fclose(fp);
}
fp is a global file pointer so it is working. Each time program is executed new string is added to a file.
Problem raises when attempt to read.
function for reading:
Code:
char * Read(void)
{	
	
	if((fp=fopen("Test.bin","rb"))==NULL) exit(1);	
	
	
	char*  novi=(char *)malloc(sizeof(char)*500);
	char *str;
	
	
	fread(novi,sizeof(char),500,fp);
	
	fclose(fp);
	return novi;
}
Every time only first string is returned. I understand what is happening. I allocate 500
elements of char assuming I don't know how much is enetered. Character '\0' is also written
and when I use puts(string) //string=Read(); in main() only first string is displayed.
How can I solve this problem. In geereal case how to read files taht contains strings variable size. Do I have to track length when writing and then to pass it in Read or...?
And what if I write to a file other data types such as integer? or maybe it is better to use structures then? Questions, question oooooooooohhh
Help me please!