3

I need to compare the first and the last letter in a word; if these letters are the same, I need to output that word to a file. But I take words from another file. My problem is i can't guess how i should output all words because in my code, it outputs only the first word. So i understand that i have no transition to others.

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

int main()
{
    char my_string[256];
    char* ptr;

    FILE *f;
    if ((f = fopen("test.txt", "r"))==NULL) {
        printf("Cannot open  test file.\n");
        exit(1);
    }

    FILE *out;
    if((out=fopen("result.txt","w"))==NULL){
        printf("ERROR\n");
        exit(1);
    }

    fgets (my_string,256,f);
    int i;
    int count = 1;

    printf("My string is %d symbols\n", strlen(my_string));

    for (ptr = strtok(my_string," "); ptr != NULL; ptr= strtok(NULL," "))
    {
        int last = strlen(ptr) - 1;
        if ((last != -1) && (ptr[0] == ptr[last]))
        {
            printf("%s\n",ptr);
        }
    }

    printf("\n%s\n",my_string);
    fprintf(out,"%s\n",my_string);
    system("pause");
    fclose(f);
    fclose(out);

    return 0;
}

In my first file there are words:

high day aya aya eye that

From my words from the first file, it outputs only the first word

high

to the second file. I expect the following:

high aya aya eye
anatolyg
  • 26,506
  • 9
  • 60
  • 134
Nikita Gusev
  • 143
  • 10
  • What do you mean "it does onlythe first word"? For the specified input, what is the actual and expected output? And have you tried stepping through the code line by line in a debugger? Do the `my_strlen` and `mystrtok` functions work as they should? *Why* do you have your own string functions instead of using the standard functions? – Some programmer dude Dec 08 '16 at 16:19
  • What's with the use of `my_strlen` and `mystrtok`? What library are you using? – Dellowar Dec 08 '16 at 16:19
  • It means that it outputs the first word(with the same fisrt and the same last letter) what i need but there are more than one. my functions work. For checking i can use the library i use my own functions because its a special condition for this program. – Nikita Gusev Dec 08 '16 at 16:27
  • Well this code looks fine to me. I suspect the issue may lay in those custom functions. – Dellowar Dec 08 '16 at 16:33
  • ahah ok, but when i take these functions from the library. All the same it doesnt work:(( – Nikita Gusev Dec 08 '16 at 16:38
  • "doesnt work" is a pretty general description; you should explain better. For example: what does your program output? What do you expect it to output? Does it crash? Please [edit] your question and add these details. See also: [mcve]. BTW: it's good that you specified the **input** to your program. Definitely a good start. – anatolyg Dec 08 '16 at 16:39
  • I changed it ,now there are not my functions. From my words from the first file, it outputs only the word "high" in the second file. I expect for "high aya aya eye". No, it doesn't crash. – Nikita Gusev Dec 08 '16 at 16:43
  • i need to think for the space in strok() , i shouldnt think about others . – Nikita Gusev Dec 08 '16 at 16:46

1 Answers1

2

You're not outputting anything to the file except at the very end when you fprintf the entire string:

fprintf(out,"%s\n",my_string);

You need to change printf("%s\n",ptr); to fprintf(out,"%s\n",ptr); in that for loop. Otherwise it will just output everything to the console.

Dellowar
  • 3,160
  • 1
  • 18
  • 37
  • Thank you a lot! Now i get what i wanted. it is pretty pretty easy string but i needed in it. Ahahha thanks,thanks,thanks!!!! – Nikita Gusev Dec 08 '16 at 16:55