C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Reading an input string from a file Posted by ri_pe_jo on 16 Oct 2006 at 2:01 AM
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 <stdio.h>
#include <ctype.h>
#include <string.h>

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("\n\t\tSORRY......\n\t\t CANNOT OPEN FILE\n");

}
else{
printf("\n\t\tFILE data.txt FOUND!\n");
printf("\n\nSearch what string?\t");
gets(str1);

loop1:
do{
ch=getc(fp);
if(s==strlen(str1)){
str2[s]='\0';
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("\n\n\n\t\tNO STRING %s FOUND\n",str1);
}
else{
printf("\n%s OCCURRENCES OF STRING %s FOUND",t,str1);
}
}
}
How to deal with this?
Report
Re: Reading an input string from a file Posted by Lundin on 16 Oct 2006 at 3:47 AM
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:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#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.\n", key, count);
  getchar();  
  return 0;
}

Report
re:next Posted by sushant_pate on 16 Oct 2006 at 3:47 AM
This message was edited by sushant_pate at 2006-10-16 3:57:12

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
Report
Re: Reading an input string from a file Posted by sushant_pate on 16 Oct 2006 at 3:50 AM
//Well You can use this soln.
//Sample input file must be "c:\read"


#include<stdio.h>
#include<conio.h>


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]!='\0' && ch!=EOF)
{
ch=getc(ptr);
printf("%c",ch);
i++;
}
if(name[i]=='\0')
count++;
i=0;
}

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


Report
Re: Reading an input string from a file Posted by ri_pe_jo on 17 Oct 2006 at 12:46 AM
Thanks to lundin and sushan pate for your replies.
Im still a neophyte.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.