fscanf,fgets,fread,fgetc
哪个可以用来读取names.txt文件中的名字并存储在charstr[100]10]中,本人想不到简单或者可行的办法?谢谢!names.txt"MARY","PATRIC...
哪个可以用来读取 names.txt 文件中的名字并存储在char str[100]10]中,本人想不到简单或者可行的办法?谢谢!
names.txt
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE","CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE","JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE","JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY","THERESA","BEVERLY" 展开
names.txt
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE","CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE","JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE","JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY","THERESA","BEVERLY" 展开
1个回答
展开全部
注意修改下names.txt的路径,我是在d盘下
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char buf[20],c;
char str[100][10];
int count=0;
if((fp=fopen("d:\\names.txt","r"))==NULL)
{
printf("open names.txt failed");
getchar();
return -1;
}
while(1)
{
if(fscanf(fp,"%[^,]",buf)<=0) break;
buf[strlen(buf)-1]='\0';
strcpy(str[count],buf+1);
puts(str[count]);
count++;
if(fscanf(fp,"%c",&c)<=0) break;
}
fclose(fp);
getchar();
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char buf[20],c;
char str[100][10];
int count=0;
if((fp=fopen("d:\\names.txt","r"))==NULL)
{
printf("open names.txt failed");
getchar();
return -1;
}
while(1)
{
if(fscanf(fp,"%[^,]",buf)<=0) break;
buf[strlen(buf)-1]='\0';
strcpy(str[count],buf+1);
puts(str[count]);
count++;
if(fscanf(fp,"%c",&c)<=0) break;
}
fclose(fp);
getchar();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询