:-| I am desperate...
Can someone help me to debug this program..
It is a dynamic allocation file .....

Write a program that will open a sorted text file, compute how many lines are in the text file, and then create an index that will tell at which byte of the file each line begins.
Once the index is created, let the program's user enter search criteria, and then use a binary search of the file, using your index, to determine if there is a match in the file.

It gives me five error messages....and mainly it says unable to open this file
....can you please point out and tell me what should i do?

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;


int binary_search(const char onetarget[],ifstream& file);


int main()
{
int line_num = 0, ArrayIndex = 0, countbyte = 0;
char buffer1[1025];
char buffer2[1025];
int* array;


char* onetarget;
fstream.open("i:\\markp\\cis223\\populated_places_us_sorted.txt");


ifstream file(i:\\markp\\cis223\\populated_places_us_sorted.txt)  // file name ..data stored


file.getline(buffer1, 1024);// count number of lines in file
while(file)
{
line_num++;
file.getline(buffer1, 1024);
}


array = new int[line_num];


file.clear();                    // Reset
file.seekg(0,ios::beg);


file.getline(buffer1, 1024);
while(file)
{
array[ArrayIndex] = countbyte;
countbyte += strlen(buffer1) + 2;
ArrayIndex++;
file.getline(buffer1, 1024);
}


for (int i = 0; i < line_num; i++)    // Show byte index in the screen //check
cout << array << "\n";


do
{
cout << "Enter a State abbreviation : "; // enter target to be found
cin.getline( buffer1,1024 );


if (strcmp(buffer1,"")==0)
break;


cout << "Enter a city or location in that state: ";
cin.getline( buffer2,1024 );



onetarget = new char[ strlen(buffer1)+strlen(buffer2)+5 ];    // build array to keep target
strcpy(onetarget,"\"");
strcat(onetarget,buffer1);
strcat(onetarget,"\",\"");
strcat(onetarget,buffer2);


file.clear();
int found = binary_search(onetarget, file); // finding the actual location of      //target string
if (found == -1)
cout << "FILE NOT FOUND!!!!\n";



else
{
file.seekg(array[found],ios::beg);
file.getline(buffer1,1024);
cout << "\n" << buffer1 << "\n\n";            // output
}
delete [] onetarget;
}while (1);


delete [] array;        // reset
//delete [] onetarget;
return 0;



}



int binary_search(const char onetarget[], ifstream& file)
{
int first, last, middle;    // using 3 variables
first = 0;    // setting first to first element and last to last elements in the list
last = line_num;



while(1)
{
middle = (first+last) / 2;    // setting middle to find the average of both


file.clear();
file.seekg(array[middle],ios::beg);
file.getline(buffer1, 1024); // calculate the middle element
if( strncmp(onetarget,buffer1,strlen(onetarget))==0 )    // comparing
return middle;
else if (strncmp(onetarget,buffer1,strlen(onetarget))>0 ) // If smaller than the required number
first = middle + 1; // Then set first to middle+1
else if (strncmp(onetarget,buffer1,strlen(onetarget))<0 )    // if larger than the required number
last = middle - 1;    // Then set last to middle-1
if (first > last)
return -1;
}
}

PLEASE HELP ME AS SOON AS POSSIBLE!

Edited 3 Years Ago by happygeek: fixed formatting

>>fstream.open("i:\\markp\\cis223\\populated_places_us_sorted.txt");

fstream is a c++ class. You have to create an object of that type then call it's open method. Something like this:

fstream in;
in.open("i:\\markp\\cis223\\populated_places_us_sorted.txt");

or a little simplier

fstream in("i:\\markp\\cis223\\populated_places_us_sorted.txt");

Thanks for posting the message!!
Now it says that line_num is an undeclared identifier...
It points out this particular line
last = line_num;
it also says array and buffer1 are undeclared identifier!!

it points out
these two lines :

file.seekg(array[middle],ios::beg);


and


file.getline(buffer1, 1024); // calculate the middle element


Thanks a lot
BM

variables have to be declared in the function in which they are used. function binary_search() is not the same function that line_num was declared in.

This article has been dead for over six months. Start a new discussion instead.