hi experts,
i have some prob with my code.first,i want to query data from file in string.such as this file "constraint.txt".in this file,i have this:

a1,b2,c1,
a2,b1,c2,
a2,b2,c1,
a3,b1,c2,

i open th file by using this code:

char const_file[100]="constraint.txt";
FILE *in_const[100];
 if ((in_const = fopen(const_file, "rt")) == NULL)
 {
        fprintf(stderr, "Cannot open constraints file.\n");
        getch();
        exit(0);
         }

then,i use this code to load the data into an array and compare to the string append_test_data(for example:a2,b1,c3..this variable will b change everytime we run d code) :

char constraint[MAX_PARAMETER]="";
 int id_const=0;
fseek(in_const,0,SEEK_END);// seek from end of file
fseek(in_const,0,SEEK_SET); //seek from begining of file

while(!feof(in_const) )
   {
       fgets(constraint,500,in_const);
        id_const++;

        printf("number of constraints=%d\n",id_const);
         for (int m=1;m <= id_const;m++)
          {                                     
            if(strcmp(append_test_data,constraint) == 0)
                 {
                      printf("constraint is found=%s\n",constraint);
                      printf("Append data is=%s\n",append_test_data);
                                          getch();
                                          exit(0);
                 } 
           }
     }

i got the result but its juz compare with the last data from file.in this example a3,b1,c2,.what i want is append_teast_data will compare to all the data in the file,but looks like it jus compare to the last 1.
im sorry with my language.hopefully u guys can help me since it is my homework and im searching it for weeks.....please!

Recommended Answers

All 6 Replies

ubi_ct83,
Welcome to the Daniweb.

You should have to read this Announcement - http://www.daniweb.com/forums/announcement8-3.html thread. So please read announcement and some sticky threads to understand the rules at daniweb.

Source code in your post is messy. Use bb codes tag.

Here are some suggestions regarding to your code.

1. No need to create an array of FILE pointers.

Create a FILE pointer. fopen

FILE *in_const;

2. Don't use while(!feof(..)) feof

while(fgets(constrains, 500, in_const) != NULL)
  {
    ....
   }

sorry about my thread,thanks for the reply.i'll try it.thanks again

i change to ur code.but its still compare to the last constraint(last data in file).

Post source code along with data file. Use bb code tags.
for example,

[code=c] .... statements...

[/code]

FILE *in_const;
char constraint[500]="";
char append_test_data="a2,b2,c3"; //for example
                             if ((in_const = fopen(const_file, "rt"))
                                  == NULL)
                                {
                                  fprintf(stderr, "Cannot open constraints file.\n");
                                  getch();
                                  exit(0);
                                }


     fseek(in_const,0,SEEK_END);// seek from end of file
        fseek(in_const,0,SEEK_SET);//seek from begining of file
                    int id_const=1;
                  while(fgets(constraint, 500, in_const) != NULL)
                  {
                  printf("Constraint no %d is =%s\n",id_const,constraint);
                   id_const++;

  
            if(strcmp(append_test_data,constraint) == 0)
                {

                   printf("constraint is found=%s\n",constraint);
                   printf("Append data is =%s\n",append_test_data);
                getch();
              exit(0);

                 }
          }

for example use

anyone can help me????:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.