无法使用fopen()打开C中的现有文本文件

时间:2015-05-24 10:54:20

标签: c file-io text-files fopen dev-c++

我的任务是阅读文本文件并计算其中有多少单词,字符和唯一单词,以及计算有多少单词有1,2,...,30个字母。最后,我必须将上述统计信息保存在另一个文本文件中。该程序运行良好,但是当我创建createHistFile()函数时,我再次编译了我的项目,但程序无法读取它。我还检查了我是否有权阅读它,我试图将我的代码复制到一个全新的项目,但这些都没有。我在Windows XP上使用Dev-C ++。

我真的很感激任何帮助,因为我认为没有解决方案。

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

#define MAX_LEN 30
#define MAX_NUM 30

void blankCharArray(char [][MAX_LEN + 1]);
int countChars(char *);
int countUniqueAndList(char *, char [][MAX_LEN + 1]);
int countWords(char *);
FILE *createFile(char *);
void createHistFile(char *, int, int, int, char [][MAX_LEN + 1]);
void createWordList( char *, char [][MAX_LEN + 1]);
void letterHist(char [][MAX_LEN + 1]);
void nameFile(char *);
FILE *readFile(char *);
void removePunctAndLower(char *);
int scanArray(char *, char text [][MAX_LEN + 1]);

// Names files.
void nameFile(char *fileName) { 
    printf("Dose to onoma tou arxeiou: ");
    scanf("%s", fileName);
    printf("\n");
}

// Creates a file to write to.
FILE *createFile(char *fileName) { 
    FILE *filePtr;

    if ( ( filePtr = fopen(fileName, "w") ) == NULL ) {
        printf("Error: To arxeio %s den dimiourgithike.", fileName);
        exit(1);
    }

    return filePtr;
}

// Reads a file.
FILE *readFile(char *fileName) { 
    FILE *filePtr;

    if ( ( filePtr = fopen(fileName, "r") ) == NULL ) {
        printf("Error: To arxeio %s den diavastike.", fileName);
        exit(1);
    }

    return filePtr;
}

// Counts the words that are in the text file.
int countWords(char *fileName) { 
    int count = 0;
    char word[MAX_LEN + 1];
    FILE *filePtr;

    filePtr = readFile(fileName);

    while ( fscanf(filePtr, "%s", word) != EOF )
        count++;

    fseek(filePtr, 0, SEEK_SET);
    fclose(filePtr);

    printf("To sinolo ton lexeon sto keimeno %s einai: %d\n", fileName, count);

    return count;
}

// Counts the number of characters there are in the text file.
int countChars(char *fileName) {
    int count = 0;
    char ch;
    FILE *filePtr;

    filePtr = readFile(fileName);

    while ( fscanf(filePtr, "%c", &ch) != EOF ) {
        if ( ch != ' ' )
            count++;
    }

    fseek(filePtr, 0, SEEK_SET);
    fclose(filePtr);

    printf("To sinolo ton xaraktiron sto keimeno %s einai: %d\n", fileName, count);

    return count;
}

// Checks if a string (word) is in the array of strings called text.
int scanArray(char *word, char text[MAX_NUM][MAX_LEN + 1]) {
    int i;

    for (i = 0; i < MAX_NUM; i++) {
        if ( strcmp(word, text[i]) == 0)
            return 1;
    }

    return 0;
}

void removePunctAndLower(char *word) {
    char *initPtr = word, *newPtr = word;

    while (*initPtr) {
       if ( ispunct(*initPtr) )
          initPtr++;
       else if ( isupper(*initPtr) ) {
          *newPtr = tolower(*initPtr);
          initPtr++;
          newPtr++;
       }
       else if (initPtr == newPtr) {
          initPtr++;
          newPtr++;
       }
       else {
          *newPtr = *initPtr;
          initPtr++;
          newPtr++;
      }
    }

    *newPtr = '\0';
}

// Counts the number of unique words in the text file and fills an array
// with those words.
int countUniqueAndList(char *fileName, char wordList[MAX_NUM][MAX_LEN + 1]) {
    int i, uniqueNum = 0;
    char word[MAX_LEN + 1];
    FILE *filePtr;

    filePtr = readFile(fileName);

    while ( fscanf(filePtr, "%s", word) != EOF ) {
        for (i = 0; i < MAX_NUM; i++) {
            removePunctAndLower(word);
            if ( scanArray(word, wordList) == 0 ) {
                strcpy( wordList[uniqueNum], word );
                uniqueNum++;
            }       
        }
    }

    fseek(filePtr, 0, SEEK_SET);
    fclose(filePtr);

    printf("To sinolo ton diaforetikon lexeon sto keimeno %s einai: %d\n", fileName, uniqueNum);

    return uniqueNum;
}

// Creates an array that contains every word of the text file.
void createWordList(char *fileName, char wordList[MAX_NUM][MAX_LEN + 1]) {
    int i;
    char word[MAX_LEN + 1];
    FILE *filePtr;

    filePtr = readFile(fileName);

    for (i = 0; i < MAX_NUM; i++)
        if ( fscanf(filePtr, "%s", word) != EOF ) {
            removePunctAndLower(word);
            strcpy(wordList[i], word);
        }

    fseek(filePtr, 0, SEEK_SET);
    fclose(filePtr);
}

// "Initializes" a char array with '\0' 
void blankCharArray(char arr[MAX_NUM][MAX_LEN + 1]) {
    int i;

    for (i = 0; i < MAX_NUM; i++)
        strcpy(arr[i], "");
}

// Prints how many words have 1, 2, ... , 30 letters to the console.
void letterHist(char wordList[MAX_NUM][MAX_LEN + 1]) {
    size_t i, t, count = 0;

    printf("\nIstogramma plithous grammaton ana lexi tou keimenou\n\n");
    for (i = 0; strcmp(wordList[i], "") != 0; i++)
        if ( strlen(wordList[i]) == 1 )
            count++;
    if (count != 0)
        printf("Oi lexeis pou exoun 1 gramma einai: <%d>\n", count);
    for (t = 2; t < 31; t++) {
        count = 0;
        for ( i = 0; strcmp(wordList[i], "") != 0; i++)
            if ( strlen(wordList[i]) == t )
                count++;
        if (count != 0)
            printf("Oi lexeis pou exoun %d grammata einai: <%d>\n", t, count);
    }
}

// Creates a file with the above statistics.
void createHistFile(char *fileName, int wordsNum, int charsNum, int uniqueNum, char wordList[MAX_NUM][MAX_LEN + 1]) {
    size_t i, t, count = 0;
    FILE *filePtr;

    filePtr = createFile(fileName);

    fprintf(filePtr, "To sinolo ton lexeon sto keimeno %s einai: %d\n", fileName, wordsNum);
    fprintf(filePtr, "To sinolo ton xaraktiron sto keimeno %s einai: %d\n", fileName, charsNum);
    fprintf(filePtr,"To sinolo ton diaforetikon lexeon sto keimeno %s einai: %d\n\n", fileName, uniqueNum);

    fprintf(filePtr, "\nIstogramma plithous grammaton ana lexi tou keimenou\n\n");
    for (i = 0; strcmp(wordList[i], "") != 0; i++)
        if ( strlen(wordList[i]) == 1 )
            count++;
    if (count != 0)
        fprintf(filePtr, "Oi lexeis pou exoun 1 gramma einai: <%d>\n", count);
    for (t = 2; t < 31; t++) {
        count = 0;
        for ( i = 0; strcmp(wordList[i], "") != 0; i++)
            if ( strlen(wordList[i]) == t )
                count++;
        if (count != 0)
            fprintf(filePtr, "Oi lexeis pou exoun %d grammata einai: <%d>\n", t, count);
    }

    fseek(filePtr, 0, SEEK_SET);
    fclose(filePtr);
}

int main(void) {
    int wordsNum, charsNum, uniqueNum;
    char inputFileName[MAX_LEN + 1], outputFileName[MAX_LEN + 1], wordList[MAX_NUM][MAX_LEN + 1], uniqueWordList[MAX_NUM][MAX_LEN + 1];

    blankCharArray(wordList);
    blankCharArray(uniqueWordList);

    nameFile(inputFileName);

    wordsNum = countWords(outputFileName);  
    charsNum = countChars(outputFileName);
    uniqueNum = countUniqueAndList(outputFileName, uniqueWordList);

    createWordList(outputFileName, wordList);
    letterHist(wordList);

    nameFile(outputFileName);


    printf("\n");
    system("pause");

    return 0;
}

0 个答案:

没有答案