943,651 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 973
  • C++ RSS
Jun 20th, 2009
0

Read text into array(Ive searched forums)

Expand Post »
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
Similar Threads
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Jun 20th, 2009
0

Re: Read text into array(Ive searched forums)

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?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jun 20th, 2009
0

Re: Read text into array(Ive searched forums)

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.
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Jun 21st, 2009
0

Re: Read text into array(Ive searched forums)

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 21st, 2009
0

Re: Read text into array(Ive searched forums)

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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 21st, 2009
0

Re: Read text into array(Ive searched forums)

>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
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to use SHFileOperation() ?
Next Thread in C++ Forum Timeline: What is wrong?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC