在C中将FN LN格式的字符串转换为LN,FN(John Doe到Doe,John)

标签 c file-io c-strings

我对编程相当陌生,这是我的第一个问题,所以希望我不会违反任何(太多)规则!我通常喜欢自己解决问题,但我很困惑,所以这里是:

我的计算机科学 II 类(class)有一项作业,要求我创建/将我的名字写入一个文件(假设是 John Doe),然后输出该文件的内容。这部分我已经能够完成。

现在作业的第二部分告诉我编写并使用一个函数,将我的名字转换为姓氏、名字格式并将其写入一个单独的文件。它没有具体告诉我输出该文件的内容,但我还是想输出。

我不太确定如何转换名称。我应该为我的名字和姓氏创建 2 个不同的字符串(我考虑过这样做,但似乎不是特别有效)?这似乎也不像是需要使用结构的情况,但我可能(而且可能是)完全不这样做。这是我到目前为止所拥有的:

#include <stdio.h>
#define SIZE 20


int main(void)
{
    FILE *readPtr;//readfile.txt file pointer
    FILE *writePtr;//writefile.txt file pointer
    char nameFull[SIZE] = "John Doe";


    readPtr = fopen("readfile.txt", "w");//creates readfile.txt file for writing
    fprintf(readPtr, "%s", nameFull);//writes contents of string "nameFull" to file
    fclose(readPtr);//close file

    if((readPtr = fopen("readfile.txt", "r")) == NULL){//returns error message if file can't be opened
        printf("Error opening file!\n");
    }
    else{
        while(!feof(readPtr)){//end of file check for readPtr file

            fscanf(readPtr, "%[^\n]s", nameFull);
            printf("\n%s\n\n", nameFull);

        }
        fclose(readPtr);
    }
}

非常感谢任何答案或建议。即使只是朝着正确的方向轻推一下也会很棒。另外,也欢迎对我的帖子提出任何建议/批评,以便我可以改进它们。感谢您的宝贵时间!

最佳答案

首先,您应该在单独的函数中拥有将“FN LN”转换为“LN,FN”的代码。该函数必须分配内存,因为 LN、FN 形式还包含两个字符(逗号和空格)。

char *revname(const char *fnln)
{
    char *buf;

    /* +2 for space and comma, +1 for null terminator */
    if ((buf = malloc(strlen(fnln) + 3)) != NULL) {
        char *p, *q;
        size_t len;

        /* find the first space in the string */
        if ((p = strchr(fnln, ' ')) != NULL) {
            /* copy the text after the space */
            strcpy(buf, p + 1);
            strcat(buf, ", ");

            len = strlen(buf);

            /* append the first name */
            for (q = (char *) fnln; q < p; ++q)
                buf[len++] = *q;
        }
        /* null terminate the string */
        buf[len] = '\0';
    }
    return buf;
}

现在,主要代码。 while (!feof()) 是错误的,因为 EOF 指示符是在读取失败之后设置的,而不是之前。因此,循环将比预期多运行一次。完成的代码是

void remlast(char *str) { str[strlen(str) - 1] = '\0'; }
int main()
{
    FILE *fp;
    char name[20], *newname;

    if ((fp = fopen("filename", "w")) != NULL) {
        /* write the name onto the file */
        fputs("John Doe", fp);

        /* reuse the same file pointer for reading */
        if ((fp = freopen("filename", "r", fp)) != NULL) {
            fgets(name, 20, fp);

            /* remove the trailing newline from fgets */
            remlast(name);

            newname = revname(name);
            printf("%s\n", newname);

            /* free memory allocated by revname */
            free(newname);
            fclose(fp);
        } else {
            perror("freopen");
            return 1;
        }
    } else {
        perror("fopen");
        return 1;
    }
    return 0;
}

注意:我还没有测试main,所以如果有错误请告诉我。

关于在C中将FN LN格式的字符串转换为LN,FN(John Doe到Doe,John),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36189412/

相关文章:

c - 使用 bin/time 时 Execv 不工作

c - 如何在c中通过引用传递typedef结构

file-io - ansible include_if_exists

file-io - Fortran 可以直接从二进制文件读取字节吗?

c - 给定两个字符串,找到两个字符串共有的单词

c - 如何将 char ** 转换为 c 中的 char *[]?

c++ - Mac 上的 Eclipse C/C++ 项目

c++ - 左移带有可变误差

Java加载外部CSV文件编译成Jar后不起作用

c++ - 为什么 argv 不起作用?