will someone please tell me why this won't open the file "defs.dat", i have the "defs.dat" in the same folder as the .cpp program, i don't understand...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
// class definition
class wordanddef
{
public:
char word[20];
char def[80];
};
// function definitions
int loaddata(char *fn, wordanddef *wadptr[]);
void sortdata(int numlines, wordanddef *wadptr[]);
int findword(int numlines, wordanddef *wadptr[], char *wrd);
// main program
int main(void)
{
char fname[100], sword[20], origword[20];
int linecnt, fwordnum, ccnt;
wordanddef *pwordanddef[400];
// setup file name
strcpy(fname, "defs.dat");
// load data from file, returns number of line read from file
linecnt=loaddata(fname, pwordanddef);
if(linecnt==-1)
{
cout<<"File open failed.\n";
system("Pause");
exit(1);
}
// sort the data
sortdata(linecnt, pwordanddef);
// allow user to search for words
while(1)
{
// prompt user for search word
cout<<"\nEnter the word to search for, or leave blank to quit: ";
cin.getline(sword, 20);
strcpy(origword, sword);
// change all characters to upper case
for(ccnt=0; ccnt<strlen(sword); ccnt++)
sword[ccnt]= toupper(sword[ccnt]);
// exit for blank search word
if(strcmp(sword, "")==0) exit(1);
// search for the word
fwordnum=findword(linecnt, pwordanddef, sword);
// print the result
if(fwordnum==-1) cout<<"\nNot found.\n";
else
{
cout<<"\nWord found.\n";
cout<<"Original word as typed (LaRue): "<<origword<<"\n";
cout<<"Word: "<<pwordanddef[fwordnum]->word<<"\n";
cout<<"Definition: "<<pwordanddef[fwordnum]->def<<"\n";
}
}
}
// function to load data from file
int loaddata(char *fn, wordanddef *wadptr[])
{
ifstream file;
char line[80];
int i, j;
int cnt=0;
// open the file
file.open(fn);
// make sure file opened okay
if(file.fail()) return -1;
// read until end of file
while(!file.eof())
{
// set character counters
i=0;
j=0;
// get line into character string
file.getline(line, 80, '\n');
// new class instance
wadptr[cnt] = new wordanddef;
// copy the word into the new class, change all characters to upper case
while(i<strlen(line))
{
if(line[i]!=' ') wadptr[cnt]->word[i]=toupper(line[i]);
else break;
i++;
}
wadptr[cnt]->word[i]=0;
// skip the white space between word and definition
while(line[i]==' ')
i++;
// copy the definition into the new class
while(i<strlen(line))
{
if(line[i]!=' ') wadptr[cnt]->def[j]=line[i];else break;
i++;
j++;
}
wadptr[cnt]->def[j]=0;
// increase the line count
cnt++;
}
// close the file
file.close();
// return the number of lines
return cnt;
}
// function to sort data by alphabet
void sortdata(int numlines, wordanddef *wadptr[])
{
int swap, sortcnt;
wordanddef *twadptr;
// sort the pointers, alpha by word
swap=1;
while(swap)
{
swap=sortcnt=0;
while(sortcnt<numlines-1)
{
if(strcmp(wadptr[sortcnt]->word, wadptr[sortcnt+1]->word)>0)
{
twadptr=wadptr[sortcnt];
wadptr[sortcnt]=wadptr[sortcnt+1];
wadptr[sortcnt+1]=twadptr;
swap=1;
}
sortcnt++;
}
}
}
// search data for a word
int findword(int numlines, wordanddef *wadptr[], char *wrd)
{
int i;
for(i=0; i<numlines; i++)
{
if(strcmp(wadptr[i]->word, wrd)==0) return i;
}
return -1;
}