下面代码中全是c的东西
本人写了一个字符串子串获取方法
1 char *SubString(char *dest ,const char *source ,int iBegin ,int iLen)
2 {
3 char *result = dest;
4
5 if (source == NULL)
6 return result;
7
8 if (iBegin < 0 || iLen < 1)
9 return result;
10
11 while(iBegin-- != 0)
12 source++;
13
14 while(iLen-- != 0 && *source != '\0')
15 {
16 *dest = *source;
17 dest++;
18 source++;
19 }
20
21 *dest = '\0';
22
23 return result;
24 }
好,贴出完整代码,用tc3.0编译通过
ShowCode
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define Mode 33
typedef struct
{
int data;
int isuse;
}ball;
char *SubString(char *dest ,const char *source ,int iBegin ,int iLen);
void main()
{
//Declare localvar
int numread;
int i;
int len;
ball arrOlder[6];
ball arrBall[Mode];
ball arrContain[Mode];
int iBall;
FILE *stream;
/*//测试能否写出,结论:能写出
if ((stream=fopen("001.txt" ,"w")) == NULL)
{
printf("Error open file\t\n");
exit(1);
}
else
{
printf("Writed num:%d\t\n" ,fwrite("125478963" ,sizeof(char) ,10 ,stream));
}
*/
//加载ball.txt内容到arrOlder[6]
if ((stream=fopen("ball.txt" ,"r")) == NULL)
{
printf("Error open file\t\n");
exit(1);
}
else
{
char *buffer = (char*)malloc(20*sizeof(char));
// printf("buffer address 0x%X\t\n" ,buffer);
numread = fread(buffer ,sizeof(char) ,20 ,stream);
// printf("Readed num:%d\t\n" ,numread);
*(buffer + numread) = '\0';
// printf("buffer data %s\t\n" ,buffer);
if (strlen(buffer) > 0)
{
char *b = buffer;
char *subs = NULL;
i = 0;
while (b)
{
char *s = strchr(b ,'|');
if (s == NULL)
break;
else
{
//复制数据到结构中
len = s-b;
subs = (char*)malloc(10);
SubString(subs ,b ,0 ,len);
// printf("substring s = %s\t\n" ,subs);
if (strlen(subs) > 0)
{
arrOlder[i].data = atoi(subs);
arrOlder[i].isuse = 1;
i++;
}
// printf("b = %s\t\n" ,b);
b = s+1;
// printf("s = %s\t\n" ,s);
if (subs != NULL)
free(subs);
subs = NULL;
}
}
}
if (buffer != NULL)
free(buffer);
buffer = NULL;
}
fclose(stream);
//初始化
for(i=1;i<34;i++)
{
arrBall[i-1].data = i;
arrBall[i-1].isuse = 0;
}
for(i=0;i<6;i++)
{
if (arrOlder[i].isuse == 1)
{
arrContain[i].data = arrOlder[i].data;
arrContain[i].isuse = 0;
arrBall[arrOlder[i].data-1].isuse = 1;
}
}
//随机获取其他数据
i = 6;
while(1)
{
iBall = rand() % Mode;
if (arrBall[iBall].isuse == 0)
{
arrContain[i].data = arrBall[iBall].data;
arrContain[i].isuse = 0;
i++;
arrBall[iBall].isuse = 1;
}
if (i == Mode)
break;
}
for(i=0;i<33;i++)
{
// printf("arrBall\t%d:%d,%d\t\n" ,i+1 ,arrBall[i].data ,arrBall[i].isuse);
printf("arrContain\t%d:%d,%d\t\n" ,i+1 ,arrContain[i].data ,arrContain[i].isuse);
}
printf("\t\nNext datas show by rand\t\n");
//随即产生乱序数据,保存到arrBall
i = 0;
while(1)
{
iBall = rand() % Mode;
if (arrContain[iBall].isuse == 0)
{
arrBall[i].data = arrContain[iBall].data;
arrBall[i].isuse = 0;
i++;
arrContain[iBall].isuse = 1;
}
if (i == Mode)
break;
}
for(i=0;i<33;i++)
{
printf("arrBall\t%d:%d,%d\t\n" ,i+1 ,arrBall[i].data ,arrBall[i].isuse);
// printf("arrContain\t%d:%d,%d\t\n" ,i+1 ,arrContain[i].data ,arrContain[i].isuse);
}
printf("End of file");
}
char *SubString(char *dest ,const char *source ,int iBegin ,int iLen)
{
char *result = dest;
if (source == NULL)
return result;
if (iBegin < 0 || iLen < 1)
return result;
while(iBegin-- != 0)
source++;
while(iLen-- != 0 && *source != '\0')
{
*dest = *source;
dest++;
source++;
}
*dest = '\0';
return result;
}