Read text into array(Ive searched forums)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 111
Reputation: clutchkiller is an unknown quantity at this point 
Solved Threads: 1
clutchkiller's Avatar
clutchkiller clutchkiller is offline Offline
Junior Poster

Read text into array(Ive searched forums)

 
0
  #1
Jun 20th, 2009
So say I want to open a text document for reading using an instance of ifstream.open, etc.. etc... So once i open the txt file to read from it, what functions do i use to scan each character and put each character into an an array slot? Ive seen loops of if( i = 0, i < 20, i ++0) etc, but is there anyone who can walk me through it with a little more detail? Thanks
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Read text into array(Ive searched forums)

 
0
  #2
Jun 20th, 2009
Why do you want to read a character at a time? Why not use std::getline() to read a line at a time into an std::string?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 111
Reputation: clutchkiller is an unknown quantity at this point 
Solved Threads: 1
clutchkiller's Avatar
clutchkiller clutchkiller is offline Offline
Junior Poster

Re: Read text into array(Ive searched forums)

 
0
  #3
Jun 20th, 2009
i did not know there was a getline, how many chars will it default out to, because i also want to do this in binary mode as well with things other then text documents. ill do some testing with that. But also for more technical dirty work, is there a scanchar function or something similar? Something that i can research on msdn.
Last edited by clutchkiller; Jun 20th, 2009 at 7:11 pm.
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.
>Looks like i have some quasi specifics to investigate! Up Up And AWAY!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Read text into array(Ive searched forums)

 
0
  #4
Jun 21st, 2009
There are lots of tools available. You can read one char at a time aor you can read delimited text that may be stored as text or converted to numerical types. You can create code to read in a defined group of variables using a single written command. You can read a certain number of bytes at a time. You can read files stored in binary format or as text. There is a Bitset class in the standard template library. Etc.

There are 2 versions of getline(), one for C style strings, that have to have a predifined upper size limit, and one for STL strings that don't need a predifined upper size limit, though they probably have an upper size limit defined someplace that I've never bothered to track down since I've never tried to write something that big. getline() needs some way to now when to stop. It may be the amount of data to put in (C style strings), it may a specified char, or it may be the end of file indicator. The default delimiting char for both versions of getline() is the new line char.

You can learn about all of this stuff by finding a good tutorial or picking up a good reference book. I feel (a) good reference book(s) is/are invaluable if you are serious about writing code, even if it's just for your own personal enjoyment. There are a number of options listed in the sticky post section of this board.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Read text into array(Ive searched forums)

 
0
  #5
Jun 21st, 2009
Move file pointer to the end of file using seekg(); use tellg() to get the length of file. Create char pointer variable; allocates memory to accomodate file content (i.e. length of file). Move file pointer to begin of file and start to read character by character using get() till end of file.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Read text into array(Ive searched forums)

 
0
  #6
Jun 21st, 2009
>Move file pointer to the end of file using seekg(); use tellg() to get the length of file.
Not likely. While the file is opened in text mode, the seek and tell functions do not necessarily deal in pure byte offsets from the beginning of the file.
Read this FAQ: http://www.c-faq.com/stdio/textvsbinary.html third paragraph
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
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: 693 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC