码迷,mamicode.com
首页 > 其他好文 > 详细

92自己动手实现分割文本

时间:2018-05-25 01:48:18      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:\n   file   char s   system   ring   open   get   gets   pen   

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * my_strtok(char * p_string, char * p_delimiter) {
static char * p = NULL;
if (p_string != NULL) {
p = p_string;
int l_length = strlen(p_string);
for (size_t i = 0; i < l_length; i++) {
if (p_string[i] == p_delimiter[0]) {
p_string[i] = 0;
}
}
return p;
}
else {
while (*p != 0) {
p++;
}
p++;
return p;
}
}


void main() {
FILE*read = fopen("1.txt", "r");
if (!read) {
printf("文件为空");
system("pause");
return;
}

int kong = NULL;
int xiaomiao = NULL;
while (!feof(read)) {
char shuju[20] = { NULL };

fgets(shuju, sizeof(shuju), read);
int shaomiao = ftell(read);
if (shaomiao != kong) {

char*p = my_strtok(shuju, ",");
char name[20] = { NULL };
strcpy(name, p);
p = strtok(NULL, "NULL");//指针会自动移动
int age = atoi(p);
printf("名字%s 年龄%d\n",name , age);


}
}

fclose(read);

system("pause");

}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
FILE*read = fopen("1.txt", "r");
if (!read) {
printf("文件为空");
system("pause");
return;
}

int kong = NULL;
int xiaomiao = NULL;
while (!feof(read)) {
char shuju[20] = { NULL };

fgets(shuju, sizeof(shuju), read);
int shaomiao = ftell(read);
if (shaomiao != kong) {

char*p = strtok(shuju, ",");
char name[20] = { NULL };
strcpy(name, p);
p = strtok(NULL, "NULL");//指针会自动移动
int age = atoi(p);
printf("名字%s 年龄%d\n",name , age);


}
}

fclose(read);

system("pause");

}

92自己动手实现分割文本

标签:\n   file   char s   system   ring   open   get   gets   pen   

原文地址:https://www.cnblogs.com/xiaodaxiaonao/p/9085884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!