92自己动手实现分割文本
Posted 随意就好欧巴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了92自己动手实现分割文本相关的知识,希望对你有一定的参考价值。
#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自己动手实现分割文本的主要内容,如果未能解决你的问题,请参考以下文章