【问题标题】:Using fscanf to read text then printing that text into a file using fprintf使用 fscanf 读取文本,然后使用 fprintf 将该文本打印到文件中
【发布时间】:2023-03-05 05:53:01
【问题描述】:

所以我的程序的目标是从文件中读取一些文本,将其编码到另一个文本文件中,然后将单词的内部字母打乱,所以我需要单独编码每个单词,而不是在字符串,这样我以后就可以做到这一点。到目前为止,我有:

#define INPUT_FILENAME    ("A6P1_2016_TestingSherlock.txt")
#define OUTPUT_FILENAME     ("EncodeMe.txt")

void process_file(FILE* ifp, FILE* ofp) {
printf("Begin file processing\n");
char word[100];
while(fscanf(ifp, "%s", word) != EOF){
    fprintf(ofp, "%s ", word);
}

printf("End file processing\n");
}

我从输入文件中获取的文本如下

Project Gutenberg The Adventures of Sherlock Holmes, by Arthur Conan Doyle
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.  You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.net

在 EncodeMe 文本文件中,我只是将所有内容打印在一行上。请帮忙?

【问题讨论】:

  • 使用fgets 一次读取整行,使用strtok 从该行中获取单个单词。
  • @user3121023 哎呀,我忘了提到我需要单独编码每个单词,而不是在一个字符串中。我之前使用了那个确切的代码,但我无法轻松地编辑这些单词

标签: c


【解决方案1】:
fprintf(ofp, "%s\n", word);

如果你想让每个单词都换行

【讨论】:

  • 但我也不希望所有内容都单独一行,我希望它准确地打印出输入文件中文本的外观。
  • 然后一口气读完整行,打印出来,然后再切成文字。继承人如何阅读行stackoverflow.com/questions/2372813/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
相关资源
最近更新 更多