Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help readin from a file

Status
Not open for further replies.

sharktlp

Technical User
Nov 19, 2003
5
0
0
US
I have to read a few words from a file and save it into a tree so i can search. the file contains these words.



cat
dog
car
mother
nike
hat
last





this is what i have so far

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

void main()
{
long int i=0;



char ch;
char out[100];

ifstream iFile(&quot;c:/dictionary.txt&quot;);

if (! iFile)
{
cout << &quot;Error opening input file&quot; << endl;
exit(1);
}



while(iFile.get(ch))
{
switch (ch) {


case ' ':
i=i+5;
break;

case '\n':
i=i+5;
break;
break;
default:

out=ch;

cout<<out<<i;
i++;
break;
}

}








}


i want to know how i can read and store each word in a seperate string so i can later load these strings into a tree
 
#include <iostream>
#include <fstream.h>

int main()
{
char caStr[10][32];
ifstream myfile(&quot;c:\\dictionary.txt&quot;);
if(!myfile)
{
cout << &quot;File open error&quot; << endl;
return 1;
}
int i = 0;
while(!myfile.eof())
{
myfile.getline(caStr,32);
i++;
}
for(int j = 0; j < i; j++)
{
cout << caStr[j] << endl ;
}
return 0;
}

hope this helps
Rgds
Koti
 
wooops..!!
one correction
while(!myfile.eof())
{
myfile.getline(caStr,32);
i++; //....^.... should be caStr
}


sorry for the mistake.

regards
Koti

 
wooops..!!
one correction
while(!myfile.eof())
{
myfile.getline(caStr,32);
i++; //....^.... should be caStr of i
}


sorry for the mistake.

regards
Koti

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top