gpt4 book ai didi

C - 从文本文件打印特定行

转载 作者:行者123 更新时间:2023-11-30 17:14:11 25 4
gpt4 key购买 nike

我是编程新手,所以请保持友善。

目前我正在尝试编写一个程序,打开一个文本文件,读取两个单词,搜索文本文件,计算这两个单词出现的次数,然后最后打印第一个单词出现的第一行。

到目前为止,这就是我所做的:

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

FILE *infile;
char inputWord1[100], inputWord2[100], filename[100], wordInText[100], line[500];
int i, count, strComp, word1Count, word2Count, wordLen, lineCount;
char c;

int main() {
printf("Enter the first word: ");
gets(inputWord1);
printf("Enter the second word: ");
gets(inputWord2);
printf("Enter the file name: ");
gets(filename);

infile = fopen(filename, "r");
if(infile == NULL) {
printf("Error");
exit(1);
}

word1Count = 0; word2Count = 0; lineCount = 1;
while(fscanf(infile, "%s", wordInText) != EOF) {
wordLen = strlen(wordInText);
for(i = 0; i < wordLen; i++) {
if(wordInText[i] >= 65 && wordInText[i] <= 90) {
wordInText[i] = wordInText[i] + 32;
}
}

for(c = getc(infile); c != EOF; c = getc(infile)) {
if(c == '\n') {
lineCount = lineCount + 1;
}
}

strComp = strcmp(wordInText, inputWord1);
if(strComp == 0) {
word1Count++;
if(word1Count == 1) {
for(int x = lineCount; x <= lineCount; x++) {
fgets(line, 500, infile);
printf("%s\n", line);
}
}
}
strComp = strcmp(wordInText, inputWord2);
if(strComp == 0) {
word2Count++;
}
}
printf("Word 1 appears %d times\n", word1Count);
printf("Word 2 appears %d times\n", word2Count);
}

所以所有这些都有效,除了:

strComp = strcmp(wordInText, inputWord1); 
if(strComp == 0) {
word1Count++;
if(word1Count == 1) {
for(int x = lineCount; x <= lineCount; x++) {
fgets(line, 500, infile);
printf("%s\n", line);
}
}
}

最后一个 for 循环无法正常工作。它打印出\n 但不打印该行。我真的不知道为什么它不起作用。所有其他部件都工作正常。

如果有人对如何解决这个问题有任何想法,我将非常感激。请记住,我只知道基本的 C 函数,而且我还没有完全完成这个程序(仍然需要将输入的单词转换为小写)。

最佳答案

应该替换 gets 以避免缓冲区溢出我使用了一些定义,当我们找到单词时,输入行将被复制并在最后打印。文件逐行读取,每行逐字分割。解析应该更新,例如允许多个空格,支持更多单词分隔符等...

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

#define WORD_LEN_MAX 100
#define LINE_LEN_MAX 500

int main() {
FILE *infile;
char inputWord1[WORD_LEN_MAX], inputWord2[WORD_LEN_MAX];
char filename[WORD_LEN_MAX];
char wordInText[WORD_LEN_MAX], line[LINE_LEN_MAX];
char firsAppear[LINE_LEN_MAX];
int word1Count, word2Count, lineCount, i;
printf("Enter the first word: ");
gets(inputWord1);
printf("Enter the second word: ");
gets(inputWord2);
printf("Enter the file name: ");
gets(filename);


infile = fopen(filename, "r");
if(infile == NULL) {
printf("Error cannot open %s", filename);
exit(1);
}

word1Count = 0; word2Count = 0; lineCount = 1;
while(fgets(line, sizeof(line), infile) != NULL) {
char *p = line;
lineCount++;
while (*p != '\0' && *p != '\n') {
i = 0;
while (*p != ' ' && *p != '\0' && *p != '\n') {
wordInText[i++] = tolower(*p++);
}
if (*p == ' ') {
p++;
}
wordInText[i] = '\0';

if(!strcmp(wordInText, inputWord1)) {
word1Count++;
if(word1Count == 1) {
strncpy(firsAppear, line, sizeof(firsAppear));

}
}
if(!strcmp(wordInText, inputWord2)) {
word2Count++;
}
}
}
printf("Word 1 appears %d times\n", word1Count);
printf("Word 2 appears %d times\n", word2Count);
printf("%s", firsAppear);
}

关于C - 从文本文件打印特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30450567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com