I am working on a fairly large project for my chemistry teacher (NOT homework!). I thought that it would be nice if the program could search a file for a user entered string and display all lines that had that string. I have gotten it as far as I can and it still doesn't work. I am fairly certain that I know where the problem is but even if I was positive I wouldn't know how to fix it. Here's the code that deals with the user entered string (which doesn't have any problems itself):
void results();
void display_results(int posx,int posy);
void display_results(int posx,int posy)
{
ofstream fout;
ifstream fin;
char ch;
//Set up result[] variable
int result_size=1;
int result_int=0;
fin.open("Temp");
while (fin.get(ch))
result_size++;
fin.close();
char result[result_size];
while (fin.get(ch))
{
result[result_int]=(char) ch;
result_int++;
}
rectfill(screen,0,0,SCREEN_W,SCREEN_H,GREY);
textprintf_ex(screen,font,posx,posy,BLACK,CLEAR,"%s",result);
}
void results()
{
ofstream fout;
ifstream fin;
char ch;
int i=0;
int posx=5;
int posy=5;
//Set up the search[] variable
int search_size=1;
int search_int=0;
fin.open("Temp1");
while (fin.get(ch))
{
search_size++;
}
fin.close();
char search[search_size];
fin.open("Temp1");
while (fin.get(ch))
{
search[i]=(char) ch;
i++;
}
//Set up the file[] variable
int file_size=1;
int file_int=0;
fin.open("Element File.txt");
while (fin.get(ch))
{
file_size++;
}
fin.close();
char file[file_size];
i=0;
fin.open("Element File"); //I'm fairly certain that
while (fin.get(ch)) //the problem occurs
{ //here while reading
file[i]=(char) ch; //from the file into
i++; //the array.
}
//The curline[] variable is too unpredictable
//to formally set up so we won't
//but we should clear it
char curline[30];
int curline_int=0;
for (int i=0; i<30; i++)
curline[i]='\0';
while (file_int!=file_size)
{
if (file[file_int]!='\n')
{
curline[curline_int]=file[file_int];
fout.open("Temp",ios::app);
fout << curline[curline_int];
rest(1000);
fout.close();
curline_int++;
}
else
{
curline_int=0;
while(curline_int<30)
{
if (curline[curline_int]==search[search_int])
search_int++;
if (search_int==search_size)
display_results(posx,posy);
curline_int++;
}
search_int=0;
curline_int=0;
}
file_int++;
}
}
I'm pretty sure that the problem when it tries to load the Element file into the array, but does any one have any other suggestions or notice any problems right off? As a side note search[] which is the array that holds the user entered search is written to file "Temp1" the write works but I'm not positive about the read. I didn't use parameters because for some reason it always passed a pointer, which the program views as a number instead of a character string.
Whew, that took a while.
Thanks in advance.