| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reading files into vectors
Hi,
I've been trying to figure out how to do this for a while and I've never managed any luck with it. I've got several assignments for a class that require it in various forms. Basically I need to figure out how to read a txt file of words into a vector or array that stores strings. I already have problems getting files read into a program in general, but the things I've tried to get the strings (just words in most cases) into vectors hasn't worked yet. If anyone could help me figure this out I'd greatly appreciate it. If I need to provide more details just let me know. Jyn |
|
#2
|
|||
|
|||
|
You mean you need to create buffers for storing texts?
One of the appraoch is to overload ">>" operator, then suppose IfStream object is called as InArray , your ">>" operator would work like InArray>>File.read() , where File is File object [Similar to cin.getname() or cin>>Text] HTH |
|
#3
|
|||
|
|||
|
Essentially yah that's what I need, but I'm so bad with reading in files, I don't know how to code it.
I know I need an ifstream object and all that, I just don't know how to get it from the object into the vector. The only example I have of reading in a file, goes character by character and I've got an idea of how to do it with that, I'm just hoping there's something simpler I can do. Is there a way to have the ifstream object read in a string rather than a character? without using getline probably cause I need it do words and not full lines, though I suppose if I have to I could break it down after reading, but I don't think it'd be very efficient cause the files I need to read in are up to a million words. |
|
#4
|
|||
|
|||
|
Best is to dynamically allocate memory and copy all file's content to it. This allocated memory can store many words. This is particularly easy in VC++ where there is concept of file mapping and globally allocating memory block.
For your case, there are methods that read line or string of text. example cin.getline(). |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Reading files into vectors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|