hi!

this is my input file by name SC.txt

CD 737 123457
AM 320 246
XY 543 357
AR 222 2
TY 212 1357

now i want to read this file and compare the last column entires with a user entered number.

i.e user enters a no. for example 2 then i need to compare this no. with last entry...which lets say is represented by dop...

here's my code

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
struct air
{
char a[3];
int b;
int dop[8];
};
struct air ia;
int da=0;
fp=fopen("SC.txt","rb");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}
printf("Enter da: ");
scanf("%d",&da);
for(i=0;i<=50;i++)
{
      while(fscanf (fp,"%s %d %d",ia.a,&ia.b,ia.dop[i]) != EOF)
     {
    if(da==ia.dop[i])
    printf("Code %s ",ia.a);
      }
}
fclose(fp);
}

But when i run this program using da=2 entered by user
it only shows output as
Code : AR

and doesn't show other codes where dop contains 2
i.e output should be

Code : CD
Code : AM
Code : AR

can anyone tell me where iam going wrong???

Recommended Answers

All 16 Replies

Member Avatar for iamthwee

don't use void main, use int main.
don't use conio.h.
don't use !EOF.
don't use scanf, instead follow this:

http://www.daniweb.com/tutorials/tutorial45806.html

But when i run this program using da=2 entered by user
it only shows output as
Code : AR

and doesn't show other codes where dop contains 2
i.e output should be

Code : CD
Code : AM
Code : AR

can anyone tell me where iam going wrong???

Instead of declaring dop as int[8] declare it as a char array.

char[50]. Then you check loop through each element checking if any contain the number you have declared. If it's more than one number you will have to think of another way to do it.

didn't get you
you mean by declaring dop as char dop[50] should work?????
or are you trying to say something else?????

Member Avatar for iamthwee

> you mean by declaring dop as char dop[50] should work?????

Yes it makes no sense to declare it as an array of integers.

For example the following code may be what you want.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
   FILE *fp;
   struct air
   {
    char a[50];
    int b;
    char dop[50];
   };
    struct air ia;

    char da = '2'; //change the variable here

    fp=fopen("SC.txt","rb");
    if(fp==NULL)
    {
     puts("Cannot open file");
     exit(0);
    }
    
    
  
      while(fscanf (fp,"%s %d %s",ia.a,&ia.b,ia.dop) != EOF)
      //don't use EOF for next time!!
      {
            
            int tmp = strlen(ia.dop);
            int j;
            for (j=0;j<tmp;j++)
            {
              if(da==ia.dop[j])
             {
               printf("Code %s ",ia.a);
               printf("\n");
               break; //so dat it don't repeat
             }
            }
      }
    
fclose(fp);
getchar();
return 0;
}

However, notice it only works for individual chars.

For example it only works to find ' 2 ' in 1234567 .

However, if you want to find 23 in 1234567 it would be quite different. You would be searching for a substring within a string.

Thanks alot .Yeah it is exactly what i wanted.
But another problem is how to change value of da.
I mean what if the user wants to enter value of da himself without intialising as in the statement char da='2'; ?

Member Avatar for iamthwee

Thanks alot .Yeah it is exactly what i wanted.
But another problem is how to change value of da.
I mean what if the user wants to enter value of da himself without intialising as in the statement char da='2'; ?

Maybe this?

char da; 
    
da = getchar();

okay.
Thanks once again

one more problem
if the input text file is

CD 737 123457
AM 320 246
CD 543 357
AR 222 2
TY 212 1357
AM 737 267

then when da=2 output will be
CD
AM
AR
AM

can it be like
CD - 1
AM - 2
AR - 1

in other words, i mean total codes rather than AM printing again and again.
Please help ...

Member Avatar for iamthwee

okay.
Thanks once again

one more problem
if the input text file is

CD 737 123457
AM 320 246
CD 543 357
AR 222 2
TY 212 1357
AM 737 267

then when da=2 output will be
CD
AM
AR
AM

can it be like
CD - 1
AM - 2
AR - 1

in other words, i mean total codes rather than AM printing again and again.
Please help ...

Yes, you can do that, but it's more tricky.

One way to do it is to create a struct called result like this

typedef struct result_
{
   int frequency;
   char* line;
} result;

And dynamically create an array of this struct to add the results found as well as the frequency of occurance. While getting every single line from the file comparisions have to be made with the temp array of struct and if a match is found to increment the frequency.

Mind u this method is very computationally intensive so think of some optimizations that can be made.

Hope it helped,
bye.

I would really appreciate if you can write a code for that problem. Cuz i tried solving that problem but in vain.
Please help

Better post ur effort along the guidelines which i have provided and then we will see where is ur code lacking and what needs to be changed. But first and foremost ur effort at solving the prob is a must.

i swear i tried... i wrote what i wanted but in the end everything just got messed up. And now iam really confused...that's why iam asking for help...please mr understand

Hmm i can try it for u but it takes time so dont expect the ans to come in the minute u see this post. I will see what can be done. Maybe tomo i would be in a better frame of mind to think about your problem.

Bye.

Member Avatar for iamthwee

What you are asking for is more or less like a word frequency counter.

If you were writing that for c++ it would be no problem. You could use the std::map.

In c it is much more difficult to write, especiallly an efficient one.

Member Avatar for iamthwee

But if you still want to do this try looking up hash tables in c.

The mechanism to create the string (word) frequency counter depends on your project requirements and your current knowledcge base.

If you know the number of unique strings that can be associated with any given value of da, then you could use a static array of type struct where the struct is similar to that provided by s.o.s. If you don't know the number of possible unique strings you could encounter then you could use a very large array to make it less likely you will run out of room in the array during the running of the program, or you could use dynamic memory to declare a container----resizing your own array as needed (poor choice) or using a list or other container designed to use dynamic memory. If you use a container like a list, then the struct will need to have a pointer embedded in it and you will need to write the code to create and manipulate the containers contents. (If this were C++ there are standard containers, like maps, vectors and lists, you could use to make this process much more straight forward).


You can even do this sort of project if you aren't familiar with use of structs by developing parallel containers, one for the unique strings and one for the frequencies of each string, as long as you coordinate the containers so you know which frequency of occurrence goes with which string.

To finish your project you will need to determine which of the multitude of options you wish to use. If you have trouble implementing your selection, then repost.

Thanks alot to all you guys...
The problem somehow just got solved...
so once again thanks aton to all you who helped me out...and to those you read my query/problem.
Bye

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.