c - 使用 C 替换文本文件中的每一行

标签 c

我想从每一行开始将 poem.txt 中的每个其他单词大写。我创建了一个函数 capitalise() ,它接收一个字符串(例如:“像根三这样的孤独数字”),将每个其他单词大写并返回它(例如:返回“像根三这样的孤独数字”)。我试图从 poem.txt 中读取每一行,将该行发送到 capitalise(),然后接收更新的字符串并将该行替换为更新的字符串,但我不知道如何操作。我测试了我的程序,它可以读取文件的内容并将它们发送到 capitalise() 并逐行显示,但它不能写入和替换每一行。我尝试使用 fprintf() 但它不起作用。各位大神能指点一下吗?

poem.txt(执行前)
我害怕我会永远
像根号三一样孤独的数字
三是好的和正确的,
为什么我的三个必须远离视线
在恶性平方根符号下方,
我希望我是九岁
因为九可以挫败这个邪恶的把戏,
只需一些快速算术
我知道我永远见不到太阳,因为 1.7321
这就是我的现实,一种可悲的非理性
什么时候听!我看到的是什么
三的另一个平方根
已经悄悄走过华尔兹,
现在我们一起倍增
形成一个我们喜欢的数字,
高兴成一个整体
我们摆脱了凡人的束缚
随着魔法棒的挥动
我们的平方根符号变得脱胶
你对我的爱又更新了

代码:

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int reverse(int otherWord)
{
    if (otherWord)
        return 0;
    else
        return 1;
}
void capitalise(char *s)
{
     int start = 1;
     int otherWord = 1;
     for (; *s; s++)
     {
            if (start && otherWord)
                *s = toupper(*s);
            start = isspace(*s);
            if (start)
                otherWord = reverse(otherWord);

     }
}
int main(int argc, char *argv[])
{
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("poem.txt", "r+");
    if (fp == NULL)
        exit(EXIT_FAILURE);

    while ((read = getline(&line, &len, fp)) != -1) {
        capitalise(line);
        fprintf(fp, "%s", line); // Here's the problem, this line!

    }

    fclose(fp);
    if (line)
        free(line);
    exit(EXIT_SUCCESS);
}

poem.txt(执行后):
我害怕我会永远
我害怕我将永远如此
ee
Ee
三是好的和正确的,
三是一切都很好而且对,
eath 恶性平方根符号,
Eath 恶性平方根符号,
ne 可以挫败这个邪恶的把戏,
Ne 可以挫败这个邪恶的把戏,
知道我永远见不到太阳,因为 1.7321
知道我永远见不到太阳,因为 1.7321
听!我看到的是什么
恩听!我看到的是什么,
电子

悄悄走过华尔兹,
S悄悄地跳华尔兹,
形成一个我们喜欢的数字,
形成一个我们喜欢的数字,
挣脱我们凡人的束缚
E 摆脱我们凡人的束缚
uare 根标志脱胶
Uare root 标志变得脱胶
编编

poem.txt(预期执行):
我害怕我将永远如此
像根号三这样的孤独数字
...

最佳答案

可以就地完成,但这很危险,如果出现故障,您可能会丢失数据。

稍微简化的版本是

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int reverse(int otherWord)
{
  if (otherWord) {
    return 0;
  } else {
    return 1;
  }
}

void capitalise(char *s)
{
  int start = 1;
  int otherWord = 1;
  for (; *s; s++) {
    if (start && otherWord) {
      *s = toupper(*s);
    }
    start = isspace(*s);
    if (start) {
      otherWord = reverse(otherWord);
    }
  }
}

int main(void)
{
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  long offset = 0L;
  int res;
  // you might prefer "w+" instead
  fp = fopen("poem.txt", "r+");
  if (fp == NULL) {
    fprintf(stderr, "Opening file poem.txt failed\n");
    exit(EXIT_FAILURE);
  }

  while ((read = getline(&line, &len, fp)) != -1) {
    capitalise(line);
    // set position relative to start of file
    if ((res = fseek(fp, offset, SEEK_SET)) < 0) {
      fprintf(stderr, "fseek failed\n");
      exit(EXIT_FAILURE);
    }
    // printing line sets file pointer to end of line
    fprintf(fp, "%s", line);
    // get that position
    offset = ftell(fp);
  }
  // fclose() might fail, too, see man-page for details
  fclose(fp);
  if (line) {
    free(line);
  }
  exit(EXIT_SUCCESS);
}

关于c - 使用 C 替换文本文件中的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40795803/

相关文章:

c - var[x] 有什么作用?

c - 弹出 x86 堆栈以访问函数 arg 时出现段错误

iphone - matchTemplate opencv 无法正常工作,如 opencv 文档中所示

c - 如何在 sprintf() 中获取追加模式

c - 二进制文件读取,在c中添加额外的字符?

c - genep是什么?

c - 内存在哪里损坏?

在 shell 中用管道连接 n 个命令?

c - struct数组元素初始化

c - 通过管道函数在 C 中计算行列式矩阵。修订代码