只有字符串的第一个字符才会在 C 中打印

发布于 2024-11-05 22:08:04 字数 1893 浏览 0 评论 0原文

我有这个结构

typedef struct grade
{
    char firstName[SIZE];
    char lastName[SIZE];
    int stuNum;
}GRADE;

和这个输出代码:

void printData(GRADE grade[], int used)
{
    int i;
    for(i=0; i<used-1; i++)
    {
        printf("%s %s %d\n", grade[i].firstName, grade[i].lastName, grade[i].stuNum);
    }
}

当我尝试打印字符时,它们只打印字符串的第一个字符,我处于静止状态,无法真正弄清楚如何让它打印字符串的其余部分。

此代码读取数据文件(格式为 "Firstname Lastname StudentNumber",大约 17 行,例如 "Mark Markinson 1234" newline代码> <代码>“等等1234”等)。

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        grade[position].firstName[0] = parseName(buf, &used);
        used++;

        grade[position].lastName[0] = parseName(buf, &used);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

这是解析缓冲区中名称的代码:

char parseName(char *str, int *place)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;

    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    return *buf;
}

在此处采取一些建议后,我更改了一些代码,但它仍然执行相同的操作(仅打印第一个字符)

,使用这种方式调用函数

parseName(buf, &used, grade[position].firstName);

,并且 parseName 现在是

void parseName(char *str, int *place, char *firstName)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;    
    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    *firstName = *buf;
}

I have this struct

typedef struct grade
{
    char firstName[SIZE];
    char lastName[SIZE];
    int stuNum;
}GRADE;

and this output code:

void printData(GRADE grade[], int used)
{
    int i;
    for(i=0; i<used-1; i++)
    {
        printf("%s %s %d\n", grade[i].firstName, grade[i].lastName, grade[i].stuNum);
    }
}

When I attempt to print the char's they only print the first character of the string, I'm at a stand still and cannot really figure out how to make it print the rest of the string.

This code reads the data file (which is in the format of "Firstname Lastname StudentNumber" in about 17 lines, for example "Mark Markinson 1234" newline "blah blah 1234" etc).

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        grade[position].firstName[0] = parseName(buf, &used);
        used++;

        grade[position].lastName[0] = parseName(buf, &used);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

This is the code that parses the buffer for names:

char parseName(char *str, int *place)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;

    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    return *buf;
}

after taking some suggestions here i changed some of the code, but it still does the same thing (only first character is printed)

used this way to call the function

parseName(buf, &used, grade[position].firstName);

and parseName is now

void parseName(char *str, int *place, char *firstName)
{
    char buf[SIZE] = {0}, name;
    int i=*place,j=0;    
    while( str[i] != '\0' && !isspace(str[i]))
    {
        buf[j++] = str[i++];

    }
    buf[j] = '\0';

    *place = i;

    *firstName = *buf;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-11-12 22:08:04

return *buf 返回(仅)buf 中的第一个字符,这是您存储到 firstNamelastName 数组中的所有字符。问题是数组不是 C 中的一流类型,因此您不能将它们作为参数传递给函数或将它们作为返回值返回。

您可能想要做的是将指针传递给缓冲区以填充 parseName 并让它填充它,而不是使用本地缓冲区。然后,您只需直接传入 firstNamelastName 即可,它们将成为指向这些字段开头的指针。

return *buf returns (just) the first character in the buf, which is all that you are storing into the firstName and lastName arrays. The problem is that arrays aren't first-class types in C, so you can't pass them as arguments to functions or return them as return values.

What you probably want to do is pass pointer to the buffer to fill in to parseName and have it fill it in rather than using a local buffer. Then you just pass in firstName or lastName directly, which become pointers to the beginning of those fields.

小嗲 2024-11-12 22:08:04

问题就在这里:

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        grade[position].firstName[0] = parseName(buf, &used);
        used++;

        grade[position].lastName[0] = parseName(buf, &used);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

当你调用parseName时,你只是设置了第一个字符。您应该将数组(您将在其中写入字符串)传递给 parseName:

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        parseName(buf, &used, grade[position].firstName);
        used++;

        parseName(buf, &used, grade[position].lastName);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

不要忘记更改 parseName 添加参数并将结果写入其中。

The problem is here:

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        grade[position].firstName[0] = parseName(buf, &used);
        used++;

        grade[position].lastName[0] = parseName(buf, &used);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

When you call parseName, you just set the first characters. You should pass your array (where you will write the string) to parseName:

void readData(FILE *infile, GRADE grade[], int *count)
{
    char buf[SIZE] = {0};
    int position=*count;
    int used = 0;
    while( fgets(buf, SIZE, infile))
    { 
        removeNL(buf);

        parseName(buf, &used, grade[position].firstName);
        used++;

        parseName(buf, &used, grade[position].lastName);
        used++;

        grade[position].stuNum = parseNumber(buf, &used);
        used = 0;
        position++;
    }
    *count = position;

}

Don't forget to change parseName adding a parameter and writing the result to it.

国际总奸 2024-11-12 22:08:04

parseName 中,您将所有输入放入缓冲区并返回第一个字符,然后将该字符分配给 firstName[0]lastName[0]< /code> 并且永远不会对放入缓冲区的其余数据执行任何操作。难怪只打印一个字符。

您可能应该将您的更改更改

grade[position].firstName[0] = parseName(buf, &used);

parseName(grade[position].firstName, &used);

There's not 事实上任何理由返回 parseName 中的第一个字符。您可以将 parseName 的返回类型更改为 char* 并返回 buf 而不是 *buf 一点冗余(一些 C 函数可以做到这一点),这将是更好的设计。

In parseName you're putting all the input into the buffer and returning the first character, then assigning that character to firstName[0] or lastName[0] and never doing anything with the rest of the data that you put in the buffer. No wonder only one character is being printed.

You should probably change your

grade[position].firstName[0] = parseName(buf, &used);

to

parseName(grade[position].firstName, &used);

There's not really any reason to return the first character in parseName. You could change the return type of parseName to char* and return the buf instead of *buf for a little redundancy (some C functions do it) and that'd be better design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文