钓鱼台8号院:C语言:求写一段小程序!

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/06 11:07:33
有一个文件在E:\test.txt 内容如下:

刘雪华
陈凯歌
杨澜
颜行书
张智成
...(省略部分)
黄维德
-------------------
怎样使它以这种格式输出:

INSERT INTO `vote` VALUES (, 8, '刘雪华', 0);
INSERT INTO `vote` VALUES (, 8, '陈凯歌', 0);
INSERT INTO `vote` VALUES (, 8, '杨澜', 0);
INSERT INTO `vote` VALUES (, 8, '颜行书', 0);
INSERT INTO `vote` VALUES (, 8, '张智成', 0);
...(省略部分)
INSERT INTO `vote` VALUES (, 8, '黄维德',0);
要使用文件函数的,可我对文件操做不熟悉。格式就是下面的呀:
“INSERT INTO `vote` VALUES (, 8, '刘雪华', 0); ”
‘刘雪华’这个名字是在test.txt文件提取的,其它的在printf()加入即可!

基本思想:首先在test.txt文件取出一行,以上面的输出,再把取出第二行输出...

在VC6下写的代码,VC6中汉字都是采用gb2132格式的,也即每一个汉字由两个字节表示,各个字节的最高位是1

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
/*
* 从文件E:\test.txt读入,输出到E:\result.txt
*/
int main()
{
char *str1 = "INSERT INTO `vote` VALUES (, 8, '";
char *str2 = "', 0);";
FILE *file1, *file2;
file1 = fopen("E:\\test.txt","r");
if(file1 == NULL)
{
printf("open file error!\n");
return 1;
}
file2 = fopen("E:\\result.txt","w");
if(file2 == NULL)
{
printf("open file error!\n");
return 1;
}
while(!feof(file1))
{
char str[128]={0};
fgets(str,128,file1); //读入该行字符
int len = strlen(str);
//本循环功能在于去掉该行字符串结尾的非中文字符(回车和空格)
while((unsigned char)str[len] < 0x80)
str[len--] = 0x00;
//将需要的数据输出到file2中
if(len > 0)
fprintf(file2,"%s%s%s\n",str1,str,str2);
}

fclose(file1);
fclose(file2);
return 0;
}

用个循环啊,
定义一个字符数组a,a[0]=“刘德华”……
for(i=0;i<某;i++)
{
printf("INSERT INTO `vote` VALUES (, 8, 'a[i]', 0)"
}

你说的是什么格式阿!题目要说清楚啊!