[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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.
0
oommrrii123
Asked:
oommrrii123
  • 3
1 Solution
 
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
 
dhyaneshCommented:
sorry for initial comment regarding homework
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now