C程序从字符串数组中提取不同子 [英] C program to extract different substrings from array of strings

查看:168
本文介绍了C程序从字符串数组中提取不同子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串数组文件中提取不同的子串。我的文件类似于此。

  abcdxxx
efghijkyyy
lmzzz
ncdslanclsppp
kdfmsqqq
cbskdnsrrr

从上面的文件,我想提取XXX,YYY,ZZZ,PPP,QQQ,存款准备金率(去年基本3个字符),并存储到一个数组。我所指向该链接从用C 字符串中提取子但因为在我的文件中的内容是动态的,下一次执行可能会改变没有感觉到可行的。能有人给这个短暂的?
这里是我的方法

  FILE * FP1 =的fopen(test.txt的,R);
     如果(FP1 == NULL)
     {
        的printf(无法打开文件\\ n);
        返回1;
     }
     字符数组[100];
     而(的fscanf(FP1,%[^ \\ n],数组)!= NULL);     对于(i = 1; I< = 6;我++)
     {
        数组[I] + = 4;
     }


解决方案

  

在我的文件中的内容是动态的,下一次执行可能会改变。


然后,你需要的realloc 或链接列表:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    FILE * F;
    焦炭** ARR = NULL;
    个char [100];
    为size_t I,N = 0;    F = FOPEN(的text.txt,R);
    如果(F == NULL){
        PERROR(FOPEN);
        出口(EXIT_FAILURE);
    }
    而(与fgets(S,sizeof的S,F)!= NULL){
        ARR =的realloc(ARR,sizeof的(* ARR)*(N + 1));
        改编[η] =释放calloc(4,1);
        的memcpy(ARR [N ++],S + strlen的(S) - 4,3);
    }
    FCLOSE(F);
    对于(i = 0; I< N;我++){
        的printf(%S \\ n,编曲[I]);
        免费(ARR [I]);
    }
    免费(ARR);
    返回0;
}

输出:

  XXX
YYY
ZZZ
PPP
QQQ
存款准备金率

如果你总是希望在最后3个字符可以简化:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    FILE * F;
    炭(*改编)[4] = NULL;
    个char [100];
    为size_t I,N = 0;    F = FOPEN(的text.txt,R);
    如果(F == NULL){
        PERROR(FOPEN);
        出口(EXIT_FAILURE);
    }
    而(与fgets(S,sizeof的S,F)!= NULL){
        ARR =的realloc(ARR,sizeof的(* ARR)*(N + 1));
        的memcpy(ARR [N],S + strlen的(S) - 4,3);
        ARR [N ++] [3] ='\\ 0';
    }
    FCLOSE(F);
    对于(i = 0; I< N;我++){
        的printf(%S \\ n,编曲[I]);
    }
    免费(ARR);
    返回0;
}

I want to extract different substrings from a file with array of strings. My file resembles this.

abcdxxx
efghijkyyy
lmzzz
ncdslanclsppp
kdfmsqqq
cbskdnsrrr 

From the above file I want to extract xxx, yyy, zzz, ppp, qqq, rrr (basically last 3 characters) and store into an array. I refered this link extract a substring from a string in C but not felt feasible because the content in my file is dynamic and might change for next execution. Can someone give a brief on this? Here is my approach

     FILE* fp1 = fopen("test.txt","r");
     if(fp1 == NULL)
     {
        printf("Failed to open file\n");
        return 1;
     }
     char array[100];
     while(fscanf(fp1,"%[^\n]",array)!=NULL);

     for(i=1;i<=6;i++)
     {
        array[i] += 4;
     }

解决方案

the content in my file is dynamic and might change for next execution

Then you need realloc or a linked list:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    FILE *f;
    char **arr = NULL;
    char s[100];
    size_t i, n = 0;

    f = fopen("text.txt", "r");
    if (f == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    while (fgets(s, sizeof s, f) != NULL) {
        arr = realloc(arr, sizeof(*arr) * (n + 1));
        arr[n] = calloc(4, 1);
        memcpy(arr[n++], s + strlen(s) - 4, 3);
    }
    fclose(f);
    for (i = 0; i < n; i++) {
        printf("%s\n", arr[i]);
        free(arr[i]);
    }
    free(arr);
    return 0;
}

Output:

xxx
yyy
zzz
ppp
qqq
rrr

If you always want the last 3 characters you can simplify:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    FILE *f;
    char (*arr)[4] = NULL;
    char s[100];
    size_t i, n = 0;

    f = fopen("text.txt", "r");
    if (f == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    while (fgets(s, sizeof s, f) != NULL) {
        arr = realloc(arr, sizeof(*arr) * (n + 1));
        memcpy(arr[n], s + strlen(s) - 4, 3);
        arr[n++][3] = '\0';
    }
    fclose(f);
    for (i = 0; i < n; i++) {
        printf("%s\n", arr[i]);
    }
    free(arr);
    return 0;
}

这篇关于C程序从字符串数组中提取不同子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆