•
•
•
•
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
![]() |
•
•
Join Date: Jul 2006
Posts: 12
Reputation:
Rep Power: 3
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
#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???
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.
> 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.
#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.
•
•
•
•
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.
•
•
Join Date: Jul 2006
Posts: 12
Reputation:
Rep Power: 3
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 9: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 ...
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.
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.
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 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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C Forum
- Previous Thread: Print Prime Numbers(using for loop)
- Next Thread: Reading a input file



Linear Mode