如何在文本文件中找到单词的中间字符?

时间:2012-12-15 11:55:34

标签: c file

如何在文本文件中找到第一个单词的中间字符,然后使用它来计算该字符的其他出现次数?我有一个函数来计算文本文件中的单词和计算第一个单词长度的函数。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#define maxBuf 128

void howMany(FILE * file);
void words(FILE * file);

int main() {
FILE *temp, *file;
file = fopen("test.txt", "r");
temp = fopen("temp.txt","w");
char sd1[maxBuf];

howMany(file);
words(file);

fclose(file);
fclose(temp);

unlink("test.txt");
rename("temp.txt","test.txt");

return 0;  
}
void howMany(FILE * file){
char sd1[maxBuf];
file = fopen("test.txt", "r");
int i, z, c;
char word[1000];

while (!feof(file)) {
        if(fscanf(file,"%s",word)==1);
        printf("word read is: %s\n", word);
        printf("word read is: %i\n", strlen(word));                  
        break;
}
}
void words(FILE * file){
file = fopen("test.txt", "r");
char sd1[maxBuf];
int iwant; 
int nwords = 0; 

char word[1000];
int i, z;

while (fscanf(file, "%s", sd1) == 1) {

++nwords;
}
printf("There are %i words. \n", nwords);
}

1 个答案:

答案 0 :(得分:0)

缩进你的代码。

检查fopen, fgets ...

等功能的结果

MAX_BUFmaxBuf

的更好名称
size_t middle, n = strlen(sd1);
middle = (n / 2) + (n % 2 != 0);