在C和分割字符串读取文件 [英] read file in C and split string
本文介绍了在C和分割字符串读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个程序,在C读取文件现在我想把字符串除以空间分成数组。我该怎么做呢?
的#include<&stdio.h中GT;
诠释的main()
{
焦线[30];
炭名称[100] [20];
INT尺寸[100];
INT I = 0;
FILE * FP;FP = FOPEN(in.txt,RT);如果(FP == NULL)
{
的printf(无法打开文件\\ n);
返回0;
}
而(与fgets(线,的sizeof(线),FP)!= NULL)
{
的printf(线); 我++;
}
FCLOSE(FP);返回0;
}
解决方案
有一个在功能 strtok的
或 strtok_r
http://www.cplusplus.com/reference/cstring/strtok /?千瓦= strtok的
I have a program that read File in C. Now I want to put the strings divide by space into an array. How do I do it?
#include <stdio.h>
int main()
{
char line[30];
char names[100][20];
int sizes[100];
int i = 0;
FILE *fp;
fp = fopen("in.txt", "rt");
if(fp == NULL)
{
printf("cannot open file\n");
return 0;
}
while(fgets(line, sizeof(line), fp) != NULL)
{
printf(line);
i++;
}
fclose(fp);
return 0;
}
解决方案
Have a look at the function strtok
or strtok_r
http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok
这篇关于在C和分割字符串读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文