Reading an input string from a file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading an input string from a file

ri_pe_jori_pe_jo Posts: 6Member
Hello People!!
Good day.

I'm new to this site. Yea, I've got some problem on string reading. I was asked to design a programm which reads an user-input string and determine how many times the string occurs inside the file,if the file exists.

I design a program but there's a problem. Here's my program:
#include
#include
#include

FILE *fp;

void main() {
unsigned int t=0,s=0;
char str1[80],str2[100];
char ch;

fp=fopen("data.txt","r");

if(fp==NULL){
printf("
SORRY......
CANNOT OPEN FILE
");

}
else{
printf("
FILE data.txt FOUND!
");
printf("

Search what string? ");
gets(str1);

loop1:
do{
ch=getc(fp);
if(s==strlen(str1)){
str2[s]='';
goto loop2;
}
else{
str2[s]=ch;
}


loop2:
if(!strcmp(str1,str2)){
t++;
strcpy(str2," ");
goto loop1;
}
else{
strcpy(str2," ");
goto loop1;
}

}while(ch!=EOF);

if(t==0){
printf("


NO STRING %s FOUND
",str1);
}
else{
printf("
%s OCCURRENCES OF STRING %s FOUND",t,str1);
}
}
}
How to deal with this?

Comments

  • LundinLundin Posts: 3,711Member
    I fail to see the logic in that program, but apart from that, it has many issues:

    - You are checking the variables before you have placed any valid data inside them.
    - Don't use global variables or goto unless motivated. It isn't motived in your program, it just mess things up and makes the code unreadable.
    - void main() isn't ANSI C. Use int main().
    - Use variable names that make sense. Not s, t, x, y, z.

    Here is the working code based on your program:

    [code]
    #include
    #include
    #include

    #define MAX_STR_LEN 80

    int main()
    {
    FILE* fp;
    char key[MAX_STR_LEN];
    char candidate[MAX_STR_LEN];
    int count;


    fp = fopen("data.txt","r");
    if(fp == NULL)
    {
    printf("error message here");
    exit(1);
    }

    printf("Enter string: ");
    gets(key);

    count=0;

    while(!feof(fp)) /* while not end of file */
    {
    fscanf(fp, "%s", candidate);
    if(strcmp(key, candidate) == 0)
    count++;
    }

    fclose(fp);

    printf("Found "%s" %d times.
    ", key, count);
    getchar();
    return 0;
    }
    [/code]
  • sushant_patesushant_pate Posts: 10Member
    [b][red]This message was edited by sushant_pate at 2006-10-16 3:57:12[/red][/b][hr]
    The above solution wont work : suppose that you need to count horse in the file

    horsehorsehorsehorse gallops horse

    it will display the number of occurences as 2 which is wrong

    see the next message for the correct code
  • sushant_patesushant_pate Posts: 10Member
    //Well You can use this soln.
    //Sample input file must be "c:
    ead"


    #include
    #include


    int main()
    {
    FILE *ptr;
    int i=0;
    char name[20];
    int count=0;
    char ch;
    printf("Enter the String to check");
    scanf("%s",name);
    ptr=fopen("c:\input","r");

    // check if file exists

    if(ptr!=NULL)
    {

    while(ch!=EOF)
    {
    ch=getc(ptr);

    while(ch==name[i] && name[i]!='' && ch!=EOF)
    {
    ch=getc(ptr);
    printf("%c",ch);
    i++;
    }
    if(name[i]=='')
    count++;
    i=0;
    }

    }
    printf("Number of Occurences= %d",count);
    getch();
    return 0;
    }


  • ri_pe_jori_pe_jo Posts: 6Member
    Thanks to lundin and sushan pate for your replies.
    Im still a neophyte.

Sign In or Register to comment.