0

I have trouble reading a file until a word is encountered, this is what I have done but I don't think the if statement allows strings to be written within them

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

int main()
{

  int i;
  char buffer[100];
  FILE *fptr = fopen("testing.txt", "r");
  if(fptr != NULL)
    printf("file opened successfully\n");
  else {
    printf("file error occured\n");
    printf("terminating program...\n");
    return 0;
  }

  while (fgets(buffer, 100,fptr))
  {
    if(buffer != "over") {
      printf("%s ", buffer);
    }
    else
      return 0;
  }

}
Tim Zimmermann
  • 6,132
  • 3
  • 30
  • 36

3 Answers3

3

When you do

if(buffer != "over")

you compare two pointers, the pointer to buffer and the pointer to the string literal "over". Those pointers will never be the same.

To compare strings in C you have to use the strcmp function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • i edited the if statement to "if(strcmp(buffer, "over") != 0)" its still reading the over.. @Joachim Pileborg – user3151918 Jan 02 '14 at 10:36
  • @user3151918 You have to remember that [`fgets`](http://en.cppreference.com/w/c/io/fgets) leaves the newline (if one was read) in the buffer, so either you have to compare with a string containing a newline or remove the newline from the buffer. Optionally you can use [`strncmp`](http://en.cppreference.com/w/c/string/byte/strncmp) to compare only the first four characters. – Some programmer dude Jan 02 '14 at 10:38
  • @ Joachim Pileborg i edited my if statement again to: if((strncmp(buffer, "over\n",4)) != 0) doesnt work! – user3151918 Jan 02 '14 at 10:41
  • @user3151918 It seems that you are reading lines that contains *more* than the single word `"over"`. `fgets` reads the complete line into the buffer, if you want to check and print words you have to [tokenize](http://en.cppreference.com/w/c/string/byte/strtok) the line after you read it. – Some programmer dude Jan 02 '14 at 10:53
  • @JoachimPileborg I think strncmp that's inappropriate. _compare only the first four characters_ – BLUEPIXY Jan 02 '14 at 10:53
0

For comparing string you need to use strcmp() function. You can't do directly by if(buffer != "over"). It compares pointers instead of stings.

Chinna
  • 3,930
  • 4
  • 25
  • 55
0
#include <stdio.h>
#include <string.h>

int isEndWith(const char *string, const char *word){
    int len = strlen(string);
    int lenw = strlen(word);
    if(len >= lenw)
        return strncmp(string + len - lenw, word, lenw)==0;//or use strcmp
    else 
        return 0;
}

int main(void){
    char str1[] = "how are you over i am busy over\n";
    char str2[] = "how are you over i am busy over\n";
    char *p;
    if(p=strchr(str1, '\n'))
        *p = '\0';//chomp \n
    if(!isEndWith(str1, "over"))//check case end string
        printf("%s", str1);
    else {
        int len = strlen(str1);
        str1[len - 4] = '\0';// 4 : strlen("over");
        printf("%s\n", str1);//print "how are you over i am busy \n"
    }
    if(p=strstr(str2, "over"))//check case contain string
        *p = '\0';
    printf("%s\n", str2);//print "how are you \n"
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70