Solved

Searching a number/letter in a file

Posted on 2003-10-24
4
202 Views
Last Modified: 2010-04-15
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
Comment
Question by:oommrrii123
  • 3
4 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9616912
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
 

Author Comment

by:oommrrii123
ID: 9617884
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
 
LVL 4

Accepted Solution

by:
dhyanesh earned 100 total points
ID: 9618874
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
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9618878
sorry for initial comment regarding homework
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question