简体   繁体   中英

My program doesn't find EOF symbol

I have this program which deletes from an .txt file all the words, which start and end with the same symbol. In my opinion it should work, but somehow it doesn't stop when EOF is reached and ir prints me some strange chinese symbols...

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#define MAX 255

void search(char *symbolMass, FILE *duomFail, FILE *rezFail)
{
    int i = 0, k =0, j =0, p = 0;
    char symbol = 0;
    char *rezMass;
    char word[20];

    rezMass = (char*)malloc(sizeof(char)*MAX);

    while(simbolis != EOF)
    {
        printf("veikia");
        symbol = symbolMass[i];

        if (symbol != 32 && symbol != 10 && symbol != EOF)
        {
            word[j] = symbol;
            i++;
            j++;
        }

        else
        {
            word[j] = symbol;
            i++;
            if(word [0] == word[j - 1])
            {
                rezMass[k] = word[j];
                k++;
            }
            else
            {
               for (p = 0; p <= j; p++, k++)
               {
                   rezMass[k] = word[p];
               }
            }
            j = 0;
        }
    }
    for(i = 0; i <= k; i++)
        symbolMass[i] = rezMass[i];
}

int main(int argc, char* argv[])
{
    FILE *duom, *rez;
    char *symbols;


    symbols = (char*)malloc(sizeof(char)*MAX);
    if (argc > 1)
    {
        duom = fopen (argv[1],"r");
        rez =fopen (argv[2],"w");
        if (duom != NULL)
        {
            while (symbols != NULL)
            {
                fgets(symbols, MAX, duom);
                search(symbols, duom, rez);
                fputs(symbols, rez);
            }
            fclose(duom);
        }
        else
        {
            printf("There is no file with name \"%s\"\n",argv[1]);
        }
    }
    else
    {
        printf("The command has no arguments.\n");
    }

    fclose(rez);
    free(simboliai);

    return 0;
}

It works like this: it scans symbols and puts them into "word" till it reaches "space", "new line" or "EOF", then it checks if the word starts and ends with the same symbol, if yes, it prints only the "space", "new line" or "EOF", if not, then it prints the whole word. Oh, and the code is wrote in C (usiing CodeBlocks if it matters).

That's because there is no EOF character in the buffer you pass to the search function. The buffer, symbolMass is a string and like all strings in C it's terminated by the special null-character '\\0' (which incidentally happens to be the same as 0 ).

I suggest you change your loop to something like this

char symbol;

for (int i = 0; (symbol = symbolMass[i]) != '\0'; ++i)
{
    ...
}

Also, don't use "magic numbers" for characters, use the actual character literals instead, so instead of

if (symbol != 32 && symbol != 10 && symbol != EOF)

do

if (symbol != ' ' && symbol != '\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM