怎么用VC获取如下txt文档里的字符串

像这种不规则的,没办法进行字符串截取怎么获取.图片说明

2个回答

币~

#include<stdio.h>
#include<string.h>
int main() {
    FILE *fp=NULL; int i=0;
    int buf_size = 20;
    char *buf = (char *)malloc(buf_size);
    char result[20] = { '\0' };
    errno_t err;
    if ((err=fopen_s(&fp,"E://Create//input.txt", "r"))==0) {
        while (!feof(fp)) {
            i = 0;
            fgets(buf, buf_size,fp);
            if (('a' <= buf[0] && buf[0] <= 'z') || ('A' <= buf[0] && buf[0] <= 'Z')) {
                int j = 0;
                for (; i < buf_size; i++) {
                    if (!(('a' <= buf[i]&& buf[i] <= 'z' )|| ('A' <= buf[i] && buf[i] <= 'Z'))) {
                        continue;
                    }
                    if (('a' <= buf[i] && buf[i] <= 'z') || ('A' <= buf[i] && buf[i] <= 'Z')) {
                        result[j] = buf[i];
                        j++;
                    }
                    if (i == buf_size||!(('a' <= buf[i+1] && buf[i+1] <= 'z') || ('A' <= buf[i+1] && buf[i+1] <= 'Z'))) {
                        puts(result);
                        j = 0;
                        memset(result, '\0', 20);
                    }

                }
            }
        }
    fclose(fp);
    }
    else{
        printf("%s","File open error!");
    }

    getchar();

    return 0;
}
qq_25874585
qq_25874585 回复 Young man: 不客气
3 个月之前 回复
qq_44975008
Young man 找到问题了,我文件放的路径有问题.再次感谢!!!
3 个月之前 回复
qq_44975008
Young man 谢谢大佬了.我按照你给的代码执行,出现异常,异常点在 fclose(fp); 望解惑.
3 个月之前 回复
这个没办法看出中间是tab还是空格,你得自己测试下,如果是\t跳过4行后用fscanf("%s\t%s\t%s", a, b, c)试试看,abc为3个字符数组
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问