My program not work as expected

tatatatat

I need to find the position of some strings. These strings are stored in a file named queryfile , from an other file named datafile.

However, my program does not work as expected. Can some one help me?

My program

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    FILE *queryfile;
    queryfile = fopen("op2query.txt","r");

    FILE *datafile;
    datafile = fopen("op2data.txt","r" );

    int i = 1;
    char word[99];
    char search[99];

    if(queryfile==NULL) {
      printf("Error in reading Query File");
      exit(1);
    }
    if(datafile==NULL) {
      printf("Error in reading Data File");
      exit(1);
    } 

     while(fscanf(queryfile,"%98s",search)==1){  
         while(fscanf(datafile,"%98s",word)==1){
             if (strcmp(word,search)==0){
                 printf("\n %i %s ", i, search);
                 rewind(datafile);
                 i=1;
                 break;
              }
              else 
                 i++;   
         }
     }

    fclose(datafile);
    fclose(queryfile);
    return 0;
    }
Weather Vane

I build an array of each set of words to be tested, by splitting the query string into words. These words can span a line break in the data file. I mark the data file position on the second word of the set, if the search fails I seek to that point (if necessary). The program succeeds even if I duplicate every word "age" in the data file.

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

#define MAXWORDS    5
#define MAXLEN      99

int main()
{
    int j, i, done, words, count;
    long mark;
    char word[MAXLEN];
    char search[MAXLEN];
    char *tok, *sptr[MAXWORDS];
    FILE *queryfile;
    FILE *datafile;

    if ((queryfile = fopen("op2query.txt","r")) == NULL) {
         printf("Error in reading Query File");
         exit(1);
    }
    if ((datafile = fopen("op2data.txt","r" )) == NULL) {
         printf("Error in reading Data File");
         exit(1);
    } 

    while(fgets(search, MAXLEN, queryfile) != NULL){
        words = 0;
        done = 0;
        count = 0;
        mark = -1;
        tok = strtok(search, " \r\n");
        while (tok && words < MAXWORDS) {       // build array of query
            sptr[words++] = tok;
            tok = strtok(NULL, " \r\n");        // strips newline too
        }
        if (words < 1)                          // no more queries
            break;
        rewind(datafile);                       // beginning of file

        while (!done) {                         // until none to read
            count++;
            if (mark >= 0)                      // when more than one word to search
                fseek (datafile, mark, SEEK_SET);
            mark = -1;
            for (j=0; j<words; j++) {
                if (j == 1)                     // mark for next search
                    mark = ftell(datafile);
                if (fscanf(datafile, "%98s", word) != 1){
                    done = 1;                   // end of file
                    break;
                }
                if (strcmp(sptr[j], word)!=0)
                    break;                      // failed multi word search
            }
            if (done) 
                printf("NOT FOUND!");
            else if (j == words) {              // if all words found
                printf("%d", count);
                done = 1;                       // success
            }
        }
        for (i=0; i<words; i++)
            printf(" %s", sptr[i]);             // show array of words asked
        printf("\n");
    }
    fclose(datafile);
    fclose(queryfile);
    return 0;
}

Program output:

18 wisdom
40 season
NOT FOUND! summer
22 age of foolishness

UPDATE - I print NOT FOUND! when query not found. Added "summer" to query file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My program not work as expected

From Dev

Why does my program not work as expected?

From Dev

#undef not work as expected in my C program

From Dev

Program Wont Work As Expected

From Dev

Why is my program not giving the expected output?

From Dev

my .exe program is not what I expected

From Dev

why does my hamcrest "contains" not work as expected?

From Dev

My collision list does not work as expected

From Dev

why does my hamcrest "contains" not work as expected?

From Dev

Why will my code sometimes not work as expected?

From Dev

My static instance function of my class does not work as I expected

From Dev

malloc doesn't look to work in my program

From Dev

my click's counter program wont work

From Dev

How to make strcmp() work in my C program

From Dev

there is a bug that is ons-tab not work in my program

From Dev

Why won't my program work in python?

From Dev

how to get my android USSD program to work

From Dev

My Linked List Program won't work

From Dev

my program doesn't work on an special system

From Dev

Why won't my program work in python?

From Dev

how to get my android USSD program to work

From Dev

The decryption part of my program doesn't work

From Dev

Why do fseek & fputc not work in my program?

From Dev

How to make my program work faster?

From Dev

My JS Program doesn't work

From Dev

Why won't my input Scanner and the rest of my program work?

From Dev

My crond job doesn't work as expected, why?

From Dev

Why does my CSS selector does not work as expected?

From Dev

Why doesn't my Jpanel borderlayout work as expected?