- 论坛徽章:
- 0
|
用C把二进制文件(
CODE:
#include
#include
#include
#include
#include
#include
#define host "localhost" //mysql server
#define username "root"
#define password "cipher"
#define database "www"
int get_file_size(char *path, off_t *size)
{
struct stat file_stats;
if(stat(path, &file_stats))
return -1;
*size = file_stats.st_size;
return 0;
}
int main(int argc, char *argv[])
{
char *filename;
off_t *size;
MYSQL *conn;
MYSQL_RES *res_set;
MYSQL_ROW row;
MYSQL_FIELD *field;
int i, flag;
char *sql;
FILE *fp;
char *buf;
int n=0;
char *end;
unsigned long *length;
if (argc != 2) {
printf("Usage: %s srcfile\n", argv[0]);
exit(1);
}
filename = argv[1];
if ((get_file_size(filename, size)) == -1) {
perror("get file size" );
exit(1);
}
if ((buf = (char *)malloc(sizeof(char)*(*size+1))) == NULL) {
perror("malloc buf" );
exit(1);
}
if ((fp = fopen(filename, "rb" )) == NULL) {
perror("fopen file" );
exit(1);
}
if ((n = fread(buf, 1, *size, fp))
Makefile
CODE:
CXX = gcc
LIBS = -lmysqlclient
PRODUCT = mysql_binary
.LIBPATTERNS: lib%.so lib%.a
vpath % .
vpath %.c src
vpath %.h include
OBJS = mysql_binary.o
$(PRODUCT): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LIBS)
.c.o:
.PHONY:clean
clean:
-rm -f $(OBJS) $(PRODUCT)
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/9380/showart_100060.html |
|