来自文本文件的C字符串 [英] C strings from text file

查看:184
本文介绍了来自文本文件的C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程语言的新手。
如何从文本文件中搜索特定字符串并将其转换为数组,然后从这些数组生成单个字符串?

I am new to C programming language. How do you search a particular string from a text file and make them into array, then produce a single string from those array?

文本文件:

name1,name2,name3,type1,g1,g2,g3
name1,last1,last2,type2,g4,g6,g7
foo1,foo2,foo3,type3,gx,g3,g5
foo1,doo1,doo2,type4,g1,gt,gl

输出应该是1个字符串,而不是
,所以如果让它说是

The output should be in 1 string, not separated so if let's say it is

printf("%s", strings);

它给出的输出如下:

2 records found

Name: name1,name2,name3
type: type1
g1 type: g1
g2 type: g2
g3 type: g3

Name: name1,last1,last2
type: type2
g1 type: g4
g2 type: g6
g3 type: g7

到目前为止,我的尝试是获取文本文件并搜索字符串:

My attempt so far is getting the text file and search the string:

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

int main(){


    char tmp[1000];
    int count=0;

    FILE *fp=fopen("test.csv","r");

    while(fgets(tmp, sizeof(tmp),fp) != NULL){
            if (strstr(tmp, "name1")){
                    count = count + 1;
                    printf("%s", tmp);
            }
    }

}

仅限给出输出:

name1,name2,name3,type1,g1,g2,g3
name1,last1,last2,type2,g4,g6,g7

尝试进行中:

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

int main(){


    char tmp[1000];
    int count=0;
    char *string;
    char *name1, *name2, *name3, *type,*g1,*g2,*g3;

    FILE *fp=fopen("test.csv","r");

            while(fgets(tmp, sizeof(tmp),fp) != NULL){        

                    name1 = strtok(tmp,",");
                    name2 = strtok(NULL,",");
                    name3 = strtok(NULL,",");
                    type= strtok(NULL,",");
                    g1= strtok(NULL,",");
                    g2= strtok(NULL,",");
                    g3= strtok(NULL,",");

                    if (strstr(tmp, "name1")){
                            count = count + 1;
                            string = malloc(sizeof(*string));
                            sprintf(string, "\n%d record(s) found.\n\nName: %s, %s, %s \nType: %s\ng1 type: %s\ng2 type: %s\ng3 type: %s", count, name1, name2,name3,type,g1,g2,g3);
                    }
        }
        printf("%s", string);

}

带输出:

2 record(s) found.

Name: name1, last1, last2 
Type: type2
g1 type: g4
g2 type: g6
g3 type: g7


推荐答案

首先,您需要将行拆分为 strtok的。阅读此处文档。

First, you need to split the line with strtok. Read the doc here.

获得所需的所有字段后,使用 snprintf

Once you get all the fields needed, output them to one single string by using snprintf

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

#define BUFFER_SIZE 1000

int main(){

    char tmp[BUFFER_SIZE];
    int count=0;
    char string[BUFFER_SIZE];
    char all_records[BUFFER_SIZE];
    char *name1, *name2, *name3, *type,*g1,*g2,*g3;

    FILE *fp=fopen("test.csv","r");
    while(fgets(tmp, BUFFER_SIZE,fp) != NULL){        
        name1 = strtok(tmp,",");
        name2 = strtok(NULL,",");
        name3 = strtok(NULL,",");
        type= strtok(NULL,",");
        g1= strtok(NULL,",");
        g2= strtok(NULL,",");
        g3= strtok(NULL,",");

        if (strstr(tmp, "name1")){
            count++;
    // snprintf is very similar to printf, see the reference [here][2]
            snprintf(string, BUFFER_SIZE,"\nName: %s, %s, %s \nType: %s\ng1 type: %s\ng2 type: %s\ng3 type: %s", name1, name2,name3,type,g1,g2,g3);
            strcat(all_records, string);              // combine new correct record
        }
    }
    snprintf(string, BUFFER_SIZE, "%d record(s) found.\n", count);
    strcat(string, all_records);   // add the number of records to the start
    // You can use the return value of snprintf to evaluate the size of all things you will print.
    printf("%s", string);
    return 0;
}

这篇关于来自文本文件的C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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