User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,494 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,457 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1698 | Replies: 16 | Solved
Reply
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Problem in Reading Array Variable

  #1  
Jul 26th, 2006
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???
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Problem in Reading Array Variable

  #2  
Jul 26th, 2006
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.
Last edited by iamthwee : Jul 26th, 2006 at 3:50 am.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

  #3  
Jul 26th, 2006
didn't get you
you mean by declaring dop as char dop[50] should work?????
or are you trying to say something else?????
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Problem in Reading Array Variable

  #4  
Jul 26th, 2006
> 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.
Last edited by iamthwee : Jul 26th, 2006 at 4:47 am.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

  #5  
Jul 26th, 2006
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'; ?
Last edited by hiphoprules : Jul 26th, 2006 at 7:23 am.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Problem in Reading Array Variable

  #6  
Jul 26th, 2006
Originally Posted by hiphoprules
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();
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

  #7  
Jul 26th, 2006
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 ...
Last edited by hiphoprules : Jul 26th, 2006 at 9:02 am.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Problem in Reading Array Variable

  #8  
Jul 26th, 2006
Originally Posted by hiphoprules
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.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Problem in Reading Array Variable

  #9  
Jul 26th, 2006
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.
Last edited by WolfPack : Jul 26th, 2006 at 1:10 pm.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

  #10  
Jul 26th, 2006
I would really appreciate if you can write a code for that problem. Cuz i tried solving that problem but in vain.
Please help
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 7:03 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC