如何从c中的两个(纯文本)文件中的两个元素创建一个字符串?

时间:2016-05-28 01:37:46

标签: c

我的程序从两个文件(FirstNames和Lastnames)读取。到目前为止,我已经打印了一个客户ID,旁边是第一个名字和姓氏。现在我还需要打印“登录ID”,这将是第一个名字和所有姓氏的第一个字母。这是我到目前为止的程序。我认为登录ID很容易,但对我的关卡来说有点棘手。 (如果有些人在编译时遇到问题,请尝试在for循环外声明“i”)。

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

int main(int argc, char ** argv)
{
    if ( argc != 2 )
    {
        printf("usage: %s no_of_records\n", argv[0]);
        exit(1);
    }

    int nrecords = atoi(argv[1]);

    typedef struct test
    {
        int num; char *firstName; char *lastName;
    } CU;

    char buf[256];
    char * fname;
    char * lname;
    CU * cup = malloc ( nrecords * sizeof(CU));
    CU * cufirst = cup;

    FILE * fpfirst;
    FILE * fplast;

    if ( (fpfirst = fopen("FirstNames", "r")) == NULL)
    {
        fprintf(stderr, "Error reading file FirstNames");
        abort();
    }
    if ( (fplast = fopen("LastNames", "r")) == NULL)
    {
        fprintf(stderr, "Error reading file LastNames");
        abort();
    }

    for (int i = 0; i < nrecords; )
    {
        cup -> num = ++i;
        fgets(buf, sizeof(buf), fpfirst);   /*  get line    */
        fname = strndup(buf, strlen(buf)-1);    /* omit newline */
        cup -> firstName = (char *)  strdup(fname);

        fgets(buf, sizeof(buf), fplast);
        lname = strndup(buf, strlen(buf)-1);
        cup -> lastName = (char *) strdup(lname);

        cup++;
    }
    cup = cufirst;
    for (int i = 0; i < nrecords; i++)
    {
        printf("%03d\t%s\t\t%s\n", cup -> num, cup -> firstName, cup ->    lastName);
        cup++;
    }
    return 0;
 }

2 个答案:

答案 0 :(得分:0)

要打印名字的第一个字母,后跟整个姓氏,请使用%s转换打印第一个字母,然后使用printf( "%c%s", cup->firstName[0], cup->lastName ); 转换打印姓氏,如下所示:

00100

答案 1 :(得分:0)

您只需添加一个简单的格式即可打印登录ID。

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

int main(int argc, char **argv) {
    if (argc != 2) {
        printf("usage: %s no_of_records\n", argv[0]);
        exit(1);
    }

    int nrecords = atoi(argv[1]);

    typedef struct test {
        int num;
        char *firstName;
        char *lastName;
    } CU;

    char buf[256];
    char *fname;
    char *lname;
    CU *cup = malloc(nrecords * sizeof(CU));
    CU *cufirst = cup;

    FILE *fpfirst;
    FILE *fplast;

    if ((fpfirst = fopen("FirstNames", "r")) == NULL) {
        fprintf(stderr, "Error reading file FirstNames");
        abort();
    }
    if ((fplast = fopen("LastNames", "r")) == NULL) {
        fprintf(stderr, "Error reading file LastNames");
        abort();
    }

    for (int i = 0; i < nrecords;) {
        cup->num = ++i;
        fgets(buf, sizeof(buf), fpfirst);    /*  get line    */
        fname = strndup(buf, strlen(buf) - 1); /* omit newline */
        cup->firstName = (char *) strdup(fname);

        fgets(buf, sizeof(buf), fplast);
        lname = strndup(buf, strlen(buf) - 1);
        cup->lastName = (char *) strdup(lname);

        cup++;
    }
    cup = cufirst;
    for (int i = 0; i < nrecords; i++) {
        printf("%03d\t%s\t\t%s\t\t%c%s\n", cup->num, cup->firstName, cup->lastName, cup->firstName[0], cup->lastName);
        cup++;
    }
}

<强> Firstnames

Bob
Lars
Carl
Tim

<强> Lastnames

Larsson
Smith
Doe
Andersson

<强>输出

$ ./a.out 2

    001 Bob     Larsson     BLarsson
    002 Lars    Smith       LSmith