As below code representation, it seems strange that the 'read' syscall dose not work correctly with C language in windows.
#include <fcntl.h>
#include <windows.h>
#include <stdio.h>
int main()
{
int fd = open("a.txt",O_RDONLY);
char *buf = (char *)malloc(4);
read(fd,buf,4);
printf("the string is %s\n",buf);
return 0;
}
very succinct c code, and the content of a.txt is 'abcd'. But when I run this code in windows (env is MinGW, compiler is gcc). The output is
abcd?
what is the character "?" in this output string?
Can I use "read" or "write" unix syscall in windows?
thanks advance.
openwas successful.open,read, and "file descriptors" are implemented by the C runtime library; they are not system calls. The Windows API usesCreateFileto create a File object for a device or file-system file/directory and return a handle for it.ReadFilereads from a File object that's referenced by a handle. The actual system calls (i.e. that switch to ring-0 kernel mode viaSYSCALL,SYSENTER, etc) areNtCreateFileandNtReadFile.