| | |
Problem in Reading Array Variable
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2006
Posts: 12
Reputation:
Solved Threads: 0
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
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???
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
C Syntax (Toggle Plain Text)
#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???
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
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.
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???
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*
> 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.
However, notice it only works for individual chars.
For example it only works to find '
However, if you want to find
Yes it makes no sense to declare it as an array of integers.
For example the following code may be what you want.
C Syntax (Toggle Plain Text)
#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 5:47 am.
*Voted best profile in the world*
•
•
•
•
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'; ?
C Syntax (Toggle Plain Text)
char da; da = getchar();
*Voted best profile in the world*
•
•
Join Date: Jul 2006
Posts: 12
Reputation:
Solved Threads: 0
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 ...
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.
•
•
•
•
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 ...
*Voted best profile in the world*
One way to do it is to create a struct called result like this
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.
C Syntax (Toggle Plain Text)
typedef struct result_ { int frequency; char* line; } result;
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.
![]() |
Other Threads in the C Forum
- Previous Thread: help in C, Find local Ip address
- Next Thread: Reading a input file
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






