Problem in Reading Array Variable

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Problem in Reading Array Variable

 
0
  #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

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. FILE *fp;
  7. struct air
  8. {
  9. char a[3];
  10. int b;
  11. int dop[8];
  12. };
  13. struct air ia;
  14. int da=0;
  15. fp=fopen("SC.txt","rb");
  16. if(fp==NULL)
  17. {
  18. puts("Cannot open file");
  19. exit(0);
  20. }
  21. printf("Enter da: ");
  22. scanf("%d",&da);
  23. for(i=0;i<=50;i++)
  24. {
  25. while(fscanf (fp,"%s %d %d",ia.a,&ia.b,ia.dop[i]) != EOF)
  26. {
  27. if(da==ia.dop[i])
  28. printf("Code %s ",ia.a);
  29. }
  30. }
  31. fclose(fp);
  32. }


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???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in Reading Array Variable

 
0
  #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 4:50 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in Reading Array Variable

 
1
  #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.
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. int main()
  5. {
  6. FILE *fp;
  7. struct air
  8. {
  9. char a[50];
  10. int b;
  11. char dop[50];
  12. };
  13. struct air ia;
  14.  
  15. char da = '2'; //change the variable here
  16.  
  17. fp=fopen("SC.txt","rb");
  18. if(fp==NULL)
  19. {
  20. puts("Cannot open file");
  21. exit(0);
  22. }
  23.  
  24.  
  25.  
  26. while(fscanf (fp,"%s %d %s",ia.a,&ia.b,ia.dop) != EOF)
  27. //don't use EOF for next time!!
  28. {
  29.  
  30. int tmp = strlen(ia.dop);
  31. int j;
  32. for (j=0;j<tmp;j++)
  33. {
  34. if(da==ia.dop[j])
  35. {
  36. printf("Code %s ",ia.a);
  37. printf("\n");
  38. break; //so dat it don't repeat
  39. }
  40. }
  41. }
  42.  
  43. fclose(fp);
  44. getchar();
  45. return 0;
  46. }

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 5:47 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

 
0
  #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 8:23 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in Reading Array Variable

 
1
  #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?

  1. char da;
  2.  
  3. da = getchar();
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

 
0
  #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 10:02 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in Reading Array Variable

 
1
  #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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem in Reading Array Variable

 
0
  #9
Jul 26th, 2006
One way to do it is to create a struct called result like this
  1. typedef struct result_
  2. {
  3. int frequency;
  4. char* line;
  5. } 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 2:10 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: hiphoprules is an unknown quantity at this point 
Solved Threads: 0
hiphoprules hiphoprules is offline Offline
Newbie Poster

Re: Problem in Reading Array Variable

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC