How do I read from a file into a char*

Reply

Join Date: Oct 2006
Posts: 6
Reputation: matt611 is an unknown quantity at this point 
Solved Threads: 0
matt611 matt611 is offline Offline
Newbie Poster

How do I read from a file into a char*

 
0
  #1
Nov 26th, 2007
I need to read a file one word at a time. When I read the word it needs to be stored in a char* so I can pass it into my hash function. Whenever I try getline or infile>> my program just crashes. could someone please tell me what I need to use for this?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I read from a file into a char*

 
0
  #2
Nov 26th, 2007
>could someone please tell me what I need to use for this?
Something besides char* because you obviously don't know what you're doing. I'd suggest using the string class, then you can use the c_str member function to get a pointer to const char for your hash function.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: matt611 is an unknown quantity at this point 
Solved Threads: 0
matt611 matt611 is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #3
Nov 26th, 2007
I already tried that but then the compiler spits out
cannot convert parameter 1 from 'const char *' to 'char *'
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How do I read from a file into a char*

 
0
  #4
Nov 26th, 2007
Do you know what a std::string is?
Last edited by iamthwee; Nov 26th, 2007 at 5:23 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: matt611 is an unknown quantity at this point 
Solved Threads: 0
matt611 matt611 is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #5
Nov 26th, 2007
The string I was using was declared as an std::string, I just get that error when I try to compile.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How do I read from a file into a char*

 
0
  #6
Nov 26th, 2007
Can you post your code along with a sample of your input file.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: matt611 is an unknown quantity at this point 
Solved Threads: 0
matt611 matt611 is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #7
Nov 26th, 2007
  1. std::string tmp; //temp string value
  2. char* temp=""; //Temp value to hold strings
  3. ifstream dict, input; //input files for the dictionary file and the article file
  4. ofstream outfile; //outfile file
  5. dict.open("words08k.txt", ios::in);
  6. while (!dict.eof())
  7. {
  8. dict.getline(str,20);
  9. hsh.insert(temp);
  10. }

This is without using the std:string, it compiles correctly this way, but crashes when it reaches dict.getline . the input file is a text file with a word on each line.

a simple example is

ahead
angst
angular


I know my hash functions work fine, I've tested them, I just need a way to get my input into a char* variable before I pass it to the hash function.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: matt611 is an unknown quantity at this point 
Solved Threads: 0
matt611 matt611 is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #8
Nov 26th, 2007
I feel like an idiot, I figured out the problem. I'm really sorry for wasting you guys time on this.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: ravenous is an unknown quantity at this point 
Solved Threads: 3
ravenous's Avatar
ravenous ravenous is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #9
Nov 26th, 2007
on line 1 you have
  1. char* temp=""; //Temp value to hold strings
but, I think that you should assign this pointer to some memory before you use it, i.e. something like
  1. char *temp = new char;
Also, you have
  1. dict.getline(str,20);
  2. hsh.insert(temp);

but, you don't declare str at any point?

If you fix these bits then you might have more luck.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: ravenous is an unknown quantity at this point 
Solved Threads: 3
ravenous's Avatar
ravenous ravenous is offline Offline
Newbie Poster

Re: How do I read from a file into a char*

 
0
  #10
Nov 26th, 2007
Sorry, I think I was posting that reply as you were posting yours. Glad you fixed your code :-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1344 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC