将文件内容从下部情况更改为大写的问题

发布于 2025-01-28 19:01:08 字数 604 浏览 2 评论 0原文

我正在尝试使用下面的代码将文件中的内容从下层更改为上情况。它被困在一个无限的循环中。这里有什么错误?

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

int main()
{
    FILE *fp = fopen("file.txt", "r+");
    if (fp == NULL)
    {
        printf("error occured");
        exit(1);
    }
    char c;

    while ((c = fgetc(fp)) != EOF)
    {
        if (islower(c))
        {
            fseek(fp, -1, SEEK_CUR);
            fputc(toupper(c), fp);
        }
        //fseek(fp, 0, SEEK_CUR);
    }
    fclose(fp);

    return 0;
}

如果未注释的行未注册,该程序就可以正常工作。

如果可能的话,请提及一些详细了解FSEEK功能的良好资源。

I am trying to change the contents in a file from lower to upper case by using the code below. It got stuck in an infinite loop. What is the mistake here?

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

int main()
{
    FILE *fp = fopen("file.txt", "r+");
    if (fp == NULL)
    {
        printf("error occured");
        exit(1);
    }
    char c;

    while ((c = fgetc(fp)) != EOF)
    {
        if (islower(c))
        {
            fseek(fp, -1, SEEK_CUR);
            fputc(toupper(c), fp);
        }
        //fseek(fp, 0, SEEK_CUR);
    }
    fclose(fp);

    return 0;
}

The programme is working fine if the commented line is uncommented.

If possible, please mention some good resource for knowing about fseek function in detail.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文