fbczr 发表于 2019-12-13 13:08:08

c语言问题

我都去一个txt格式的文件里面有
1.chinese 2.Czech Republic 3.Cuba 4.Democratic People's Republic of Korea
一些国家的名字,我要怎么样才能读取这个txt时把这些国家放到一个数组里
就像chinese就是country,Czech Republic就是country这样,因为他中间有空格我不知道要怎么弄了

Croper 发表于 2019-12-13 14:22:32

参考上下文,当然,如果你txt文件时这样的chinese Czech Republic Cuba Democratic People's Republic of Korea那么没法判断

Croper 发表于 2019-12-13 15:46:17

如果txt是你写的这样的1.chinese 2.Czech Republic 3.Cuba 4.Democratic People's Republic of Korea

给个参考#include <stdio.h>
#include <string.h>

#define __Out__
#define N 255

int GetCountryNameFromFile(const char* path,char __Out__ (*names)){
        FILE* fp=fopen(path,"r");
       
        int i=0;
        char *p=names;

        while (fscanf(fp,"%s",p)!=EOF){
                int c_digit=-1;
                while (isdigit(p[++c_digit]));
                int length=strlen(p);
               
                if (c_digit==0){
                        p[-1]=' ';
                }
                else{
                        strcpy(names,p+c_digit+1);
                        length-=c_digit+1;
                        p=names;
                }
                p+=length+1;
        }
       
        fclose(fp);
        return i;
}
页: [1]
查看完整版本: c语言问题