在低级文件复制程序中使用gotoxy()时生成错误的代码

时间:2012-04-06 21:28:49

标签: c

包含在低级文件复制程序中的函数gotoxy会生成错误。

这是代码

#include<stdio.h>
#include"d:\types.h"
#include"d:\stat.h"
#include"d:\fcntl.h"
#include<windows.h>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

main()
{
int inhandle,outhandle,bytes;
char buffer[512],source[128],target[128];
printf("enter source file location\n");
scanf("%s",source);
printf("enter target file name with location\n");
scanf("%s",target);
inhandle=(source,O_BINARY,O_RDONLY);
if(inhandle==-1)
{
printf("cannot open source file\n");
}
outhandle=(target,O_CREAT,O_BINARY|O_WRONLY,S_IWRITE);
if(outhandle==-1)
{
printf("cannot create target file");
}
 while(1)
{
bytes=read(inhandle,buffer,512);
if(bytes>1)
{
write(outhandle,buffer,bytes);
}
}
close(inhandle);
close(outhandle);
}

这是由c-free ver5.0编译器

生成的错误列表
--------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------

Compiling D:\c\test4.c...
[Error] C:\PROGRA~2\C-FREE~1\mingw\include\stdlib.h:293: error: conflicting types for '_fmode'
[Error] d:\fcntl.h:107: error: previous declaration of '_fmode' was here
[Error] C:\PROGRA~2\C-FREE~1\mingw\include\stdlib.h:293: error: conflicting types for '_fmode'
[Error] d:\fcntl.h:107: error: previous declaration of '_fmode' was here
[Warning] D:\c\test4.c:43:2: warning: no newline at end of file

Complete Compile D:\c\test4.c: 4 error(s), 1 warning(s)

我做错了什么?如何成功编译代码?

2 个答案:

答案 0 :(得分:1)

fcntl.h中的d:\副本(wtf?)与其他系统标题不匹配。不要像这样混合和匹配系统标题。

另外:gotoxy()是一个lib(n)curses函数;它在Windows上通常不可用。如果需要,您可能需要调查PDCurses

答案 1 :(得分:0)

标题应为

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <windows.h>

请勿复制它们以使其正常工作。

遇到错误时请处理,例如

inhandle=open(source,O_BINARY,O_RDONLY);
if(inhandle==-1) { perror("cannot open source file\n");return -1;}
outhandle=open(target,O_CREAT|O_BINARY|O_WRONLY,S_IRWXU);
if(outhandle==-1) { perror("cannot create target file"); return -1;}

并且while循环没有退出点。

while(bytes=read(inhandle,buffer,512),bytes>1) {
 if (write(outhandle,buffer,bytes) == -1) { perror("write");break; }
}

程序之前不可能有效,但并没有包含goto来破坏它。