I tried using the following code to read a text from the keyboard and write it into the file text.dat. The file was created but it was empty.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <string.h>
int main()
{
char s[201];
int n,f = open("text.dat", O_RDWR | O_CREAT);
while (fgets(s,200,stdin) != NULL)
write(f, s,sizeof(s));
close(f);
return 0;
}
write(f, s, strlen(s))Though I’d use
read()instead offgets()and used its result instead ofstrlen()