使用 C 中的库编辑 txt 文件
Edit txt file using library from C
我在扫描现有文件时遇到问题。
挑战在于我有一个包含一些字符串的源文本文件。我必须在这个文件中扫描 "o'clock"
这个词,在我找到它的地方,我必须把 "o'clock"
之前的词放在方括号中。
我找到了一个查找触发器的函数,但我不明白我接下来需要做什么。如果您能解释一下如何替换读取字符串中的字符,我将不胜感激。
例如:我们有一个字符串:
"Let's go to the graveyard at night - at twelve o'clock!"
我需要将单词 twelve
替换为 [twelve]
。
代码如下:
#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
while (!feof(fp)) { // scanning all string in the source
if(fgets(c,1000, fp) != NULL) { // reading charter from file
fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}
}
}
fclose(fp); // closing file
fclose(fn);
return 0;
}
在您的代码中,您将该行复制到输出文件,然后查找触发词。但目标是找到触发器并编辑字符串,然后才将行写入输出文件。此外,为了编辑该行,您需要知道在哪里找到了触发器,因此您需要保存搜索的输出。
所以,而不是:
fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}
我们需要:
char *p = strstr(c, trigger);
if (p) {
// I tried compare string which has a trigger-word
}
fputs(c, fn); // writing charter from buffer "c" to empty file
所以现在我们找到了触发器,我们需要向后搜索以找到结尾,然后找到它之前的单词的开头。当我们找到它们时,我们需要创建一个点并插入编辑内容:
char *p = strstr(c, trigger);
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
}
fputs(c, fn); // writing charter from buffer "c" to empty file
在这里试试:https://www.onlinegdb.com/H18Tgy9xu
但是,如果字符串中有多个“o-clock”怎么办?像这样:
Let's go to the graveyard at twelve o'clock and have dinner at six o'clock beforehand!
在这种情况下,您需要循环直到找到所有这些:
这是最终代码:
#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
if (fn == NULL) { // testing on create
printf ( "Error");
getchar ();
return 0;
}
while (fgets(c, 1000, fp)) { // scanning all string in the source
char *p = c; // we need to start searching at the beginning
do {
p = strstr(p+1, trigger); // find the next oclock after this one
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
p = strstr(p, trigger); // find that same oclock again
// (we couldn't save the location because memmove has changed where it is)
}
} while (p); // as long as we found one, search again
fputs(c, fn); // writing charter from buffer "c" to empty file
}
fclose(fp); // closing file
fclose(fn);
return 0;
}
我在扫描现有文件时遇到问题。
挑战在于我有一个包含一些字符串的源文本文件。我必须在这个文件中扫描 "o'clock"
这个词,在我找到它的地方,我必须把 "o'clock"
之前的词放在方括号中。
我找到了一个查找触发器的函数,但我不明白我接下来需要做什么。如果您能解释一下如何替换读取字符串中的字符,我将不胜感激。
例如:我们有一个字符串:
"Let's go to the graveyard at night - at twelve o'clock!"
我需要将单词 twelve
替换为 [twelve]
。
代码如下:
#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
while (!feof(fp)) { // scanning all string in the source
if(fgets(c,1000, fp) != NULL) { // reading charter from file
fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}
}
}
fclose(fp); // closing file
fclose(fn);
return 0;
}
在您的代码中,您将该行复制到输出文件,然后查找触发词。但目标是找到触发器并编辑字符串,然后才将行写入输出文件。此外,为了编辑该行,您需要知道在哪里找到了触发器,因此您需要保存搜索的输出。
所以,而不是:
fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}
我们需要:
char *p = strstr(c, trigger);
if (p) {
// I tried compare string which has a trigger-word
}
fputs(c, fn); // writing charter from buffer "c" to empty file
所以现在我们找到了触发器,我们需要向后搜索以找到结尾,然后找到它之前的单词的开头。当我们找到它们时,我们需要创建一个点并插入编辑内容:
char *p = strstr(c, trigger);
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
}
fputs(c, fn); // writing charter from buffer "c" to empty file
在这里试试:https://www.onlinegdb.com/H18Tgy9xu
但是,如果字符串中有多个“o-clock”怎么办?像这样:
Let's go to the graveyard at twelve o'clock and have dinner at six o'clock beforehand!
在这种情况下,您需要循环直到找到所有这些:
这是最终代码:
#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
if (fn == NULL) { // testing on create
printf ( "Error");
getchar ();
return 0;
}
while (fgets(c, 1000, fp)) { // scanning all string in the source
char *p = c; // we need to start searching at the beginning
do {
p = strstr(p+1, trigger); // find the next oclock after this one
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
p = strstr(p, trigger); // find that same oclock again
// (we couldn't save the location because memmove has changed where it is)
}
} while (p); // as long as we found one, search again
fputs(c, fn); // writing charter from buffer "c" to empty file
}
fclose(fp); // closing file
fclose(fn);
return 0;
}