2020年2月6日,我在为LENIX开发标准C库,同一段代码在LENIX和windows都能运行了,关键不是测试代码高不高明,而是gu在于这说明LENIX准备可以使用部分标准C的函数来开发程序了。
1. 测试的代码
这个主要是测试文件功能,最基本的几个功能OK了
/* 2020-01-31 */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
#include <errno.h>
//
char msg[] = "test lenix standard c library!\n";
char filebuf[8192];
//
int main(int argc, char ** argv)
{
int i;
size_t size = 0;
FILE * file;
char * testfile;
unsigned chksum = 0;
//
printf(msg);
for( i = 0; i < argc; i++ )
printf(" No.%2d argument '%s'\n", i, argv[i]);
//
file = fopen("1.t", "rb");
if( file ){
fseek(file, 0, SEEK_END);
printf("file size: %d\n", ftell(file));
fclose(file);
}else
printf("1.t not exist\n");
testfile = argv[1] ? argv[1] : "testfile.txt";
file = fopen(testfile, "w+");
if( !file ){
printf("open '%s' failed\n", testfile);
return EXIT_FAILURE;
}
fprintf(file, "%p %s\n", file, msg);
i = 0xa5a5a5a5;
fprintf(file, "%hd %hhi %d %i %u %lu %ld %lld %llX %C\n",
i, i, i, i, i, i, i, (long long)-1, (long long)-1, 'a');
i = 0;
fseek(file, 0, SEEK_SET);
while( !feof(file) && i < 4096 ){
filebuf[i++] = (char)fgetc(file);
}
filebuf[i] = 0;
printf("file content: \n%s\n",filebuf);
fclose(file);
printf("test fseek extend file space\n");
file = fopen(testfile, "wb");
if( NULL == file ){
printf("failed to open '%s'\n",testfile);
return EXIT_FAILURE;
}
printf("start test fseek extend file space\n");
memset(filebuf, 'a', 1024);
//fseek(file, 1024, SEEK_SET);
printf("write %d count\n", fwrite(filebuf, 512, 1, file));
printf("write %d count\n", fwrite(filebuf, 1, 512, file));
fclose(file);
file = fopen(testfile, "rb");
if( NULL == file ){
printf("failed to open '%s'\n",testfile);
return EXIT_FAILURE;
}
printf(" read count: %d\n", fread(filebuf, 130, 10, file));
fseek(file, 0, SEEK_SET);
printf(" read count: %d\n", fread(filebuf, 10, 130, file));
fclose(file);
printf("test finish\n");
return EXIT_SUCCESS;
}
2. 在windows下的运行结果
这个是在VS2019下直接运行的结果
3. 在Lenix下运行的结果
这是用VS2019编译后,在lenix下运行的结果,
成功的开始