• C

Searching a number/letter in a file

Can I using C accsess a file and find a number/letter from a list of numbers.
How can I see if it had found more then one hits.
oommrrii123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhyaneshCommented:
Hi

This seems to be homework. Also you have not specified how the letters/numbers are stored and what separates them in case of numbers.

you can  use fopen to open the file and read it character by character comparing with required letter. This is done till end of file and a counter can be used to find no. of hits. However for numbers it is different

Dhyanesh
0
oommrrii123Author Commented:
What I am trying to do is a Tic Tac Toe game where every time some one wins the cordinates of the moves are being saved in a txt file. Then when the computer comes across moves it already knows it'll find the cordinate in the file and use them. I manged to
store the moves in the file but I couldn't manage to define to the computer to find the moves that were played in order to make it's moves.

here is what i've done so far:

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

main()
{
FILE *stream,*stream2;
int row,col,place,crow,ccol,moves[9],movenumber=0;
char br[3][3]={'7','8','9','4','5','6','1','2','3'};
do
{
if((br[0][0]==br[0][1] && br[0][1]==br[0][2]) || (br[1][0]==br[1][1] && br[1][1]==br[1][2]) || (br[2][0]==br[2][1] && br[2][1]==br[2][2]) || (br[0][0]==br[1][0] && br[1][0]==br[2][0]) || (br[0][1]==br[1][1] && br[1][1]==br[2][1]) || (br[0][2]==br[1][2] && br[1][2]==br[2][2]) || (br[0][0]==br[1][1] && br[1][1]==br[2][2]) || (br[0][2]==br[1][1] && br[1][1]==br[2][0]))
{
printf("Circle wins");
if(movenumber!=8)
moves[movenumber]=0;
delay(800);
stream = fopen("Wins.txt", "a");
fprintf(stream, "\n%d", moves[0]);
for(movenumber=1;movenumber<9 && moves[movenumber]!=0;movenumber++)
{
fprintf(stream, "%d", moves[movenumber]);
}
fclose(stream);
abort();
}
else if(br[0][0]!='7' && br[0][1]!='8' && br[0][2]!='9' && br[1][0]!='4' && br[1][1]!='5' && br[1][2]!='6' && br[2][0]!='1' && br[2][1]!='2' && br[2][2]!='3')
{
printf("It's a tie");
delay(800);
stream2 = fopen("Ties.txt", "a");
fprintf(stream2, "\n%d", moves[0]);
for(movenumber=1;movenumber<9;movenumber++)
{
fprintf(stream2, "%d", moves[movenumber]);
}
fclose(stream2);
abort();
}
else
{
clrscr();
for(row=0;row<3;row++)
 {
  for(col=0;col<3;col++)
   {
    printf("|%c|",br[row][col]);
   }
  printf("\n");
 }
do{
printf("\nEnter the number of the of the square that you want to place your mark: ");
scanf("%d",&place);
switch (place){
case 7: ccol=1; crow=1; break;
case 8: ccol=2; crow=1; break;
case 9: ccol=3; crow=1; break;
case 4: ccol=1; crow=2; break;
case 5: ccol=2; crow=2; break;
case 6: ccol=3; crow=2; break;
case 1: ccol=1; crow=3; break;
case 2: ccol=2; crow=3; break;
case 3: ccol=3; crow=3; break;
}
}while(br[crow-1][ccol-1]=='X' || br[crow-1][ccol-1]=='O');
br[crow-1][ccol-1]='X';
moves[movenumber]=place;
movenumber++;
clrscr();
for(row=0;row<3;row++)
 {
  for(col=0;col<3;col++)
   {
    printf("|%c|",br[row][col]);
   }
  printf("\n");
 }
}
if((br[0][0]==br[0][1] && br[0][1]==br[0][2]) || (br[1][0]==br[1][1] && br[1][1]==br[1][2]) || (br[2][0]==br[2][1] && br[2][1]==br[2][2]) || (br[0][0]==br[1][0] && br[1][0]==br[2][0]) || (br[0][1]==br[1][1] && br[1][1]==br[2][1]) || (br[0][2]==br[1][2] && br[1][2]==br[2][2]) || (br[0][0]==br[1][1] && br[1][1]==br[2][2]) || (br[0][2]==br[1][1] && br[1][1]==br[2][0]))
{
printf("X wins");
if(movenumber!=8)
moves[movenumber]=0;
delay(800);
stream = fopen("Wins.txt", "a");
fprintf(stream, "\n%d", moves[0]);
for(movenumber=0;movenumber<9 && moves[movenumber]!=0;movenumber++)
{
fprintf(stream, "%d", moves[movenumber]);
}
fclose(stream);
abort();
}
else if(br[0][0]!='7' && br[0][1]!='8' && br[0][2]!='9' && br[1][0]!='4' && br[1][1]!='5' && br[1][2]!='6' && br[2][0]!='1' && br[2][1]!='2' && br[2][2]!='3')
{
printf("It's a tie");
delay(800);
stream2 = fopen("Ties.txt", "a");
fprintf(stream2, "\n%d", moves[0]);
for(movenumber=1;movenumber<9;movenumber++)
{
fprintf(stream2, "%d", moves[movenumber]);
}
fclose(stream2);
abort();
}
else
{
do{
printf("\nEnter the number of the of the square that you want to place your mark: ");
scanf("%d",&place);
switch (place){
case 7: ccol=1; crow=1; break;
case 8: ccol=2; crow=1; break;
case 9: ccol=3; crow=1; break;
case 4: ccol=1; crow=2; break;
case 5: ccol=2; crow=2; break;
case 6: ccol=3; crow=2; break;
case 1: ccol=1; crow=3; break;
case 2: ccol=2; crow=3; break;
case 3: ccol=3; crow=3; break;
}
}while(br[crow-1][ccol-1]=='X' || br[crow-1][ccol-1]=='O');
  br[crow-1][ccol-1]='O';
  moves[movenumber]=place;
  movenumber++;
  clrscr();
  for(row=0;row<3;row++)
   {
    for(col=0;col<3;col++)
     {
      printf("|%c|",br[row][col]);
     }
    printf("\n");
   }
  }
 }
while(0<5);
}


   
0
dhyaneshCommented:
hi

First the way you store in the file. Store the '\n' after the moves like this:

stream = fopen("Wins.txt", "a");
for(movenumber=0;movenumber<9 && moves[movenumber]!=0;movenumber++)
{
fprintf(stream, "%d", moves[movenumber]);
}
fputc('\n',stream);
fclose(stream);
exit(0);//This is better than abort();

Also if the 'moves' array which is int would be better if it is character array.

then to store you would only require

fputs(moves,stream);
fputc('\n',stream);

Also it is preferable to store the wins for circle and wins for X in different files. This is because it will be easier to know who won in a sequence which is stored. The sequence can be retrieved like:

char storedmoves[15];
stream = fopen("Wins.txt","r");
fgets(storedmoves,15,stream);

Here moves will have the entire first line. You can compare this with current moves array. Then using a loop you can go on comparing until end of file like:

while (!feof(stream))
{
   .....
   ......
     ....
      
}

The comparing part can be like:

found = 1;
for(i=0;i<movesnumber;i++)
   if (moves[i] != storedmoves[i])
   {
     found = 0;
     break;                        //Match not found  
   }


In addition if you keep file sorted then you can use binary search to quicken the search in the file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhyaneshCommented:
sorry for initial comment regarding homework
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.