1

How I can read in those elements and store them into two different arrays., i.e. : the number in <?> and the word in [?] to be separately read in and stored.

<98>
Avs [adadada]
<45>
[adafaf] BBBHADA
asdadqd aada [Mammoth]
<-1> // ends the read

The rest of the info in the file is useless which do not require to be stored.

Edit:

Following the advice of one of the answers, here is my first attempt:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>


using namespace std;

int main(int argc, char* argv[])
{
    if(argc != 3)
        cout<< "Error! Not enough file!"<<endl;
    int** page = new int*[];
    char** words = new char*[];

}
//--------------------------------------------------------- 
void readInput(int** page1, char** name1){
    istream is;
    char par1, par2;
    int  usefulVal;
    is >> par1 >> usefulVal >> par2;

    // check if any input
    if(!is) return is;

    // check for valid input format
    if (par1 != '<' || par2 != '>'){
    // set failbit to indicate invalid input format
       is.clear(ios_base::failbit);

    // assign input values to second argument
    page1(usefulVal);

    char par_1, par_2;
    string string_value;
    is >> par1 >> string_value >> par2;
    if(!is) return is;
    if (par_1 != '[' || par_2 != ']')
    {
       is.clear(ios_base::failbit);
       return is;
    }
    name1(string_value);
    return is;
}

Question:

1. Is there a way to read and store the above elements separately?
2. What am I doing wrong?

P.S.: I'm just trying out some C++. Hope someone can shed some light on it, thanks!

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Aikman TD
  • 43
  • 6
  • Do you know in advance the layout of the file (the order of numbers, text, etc) or do you need to detect this at run-time? – Neil Kirk Sep 23 '15 at 22:18
  • 2
    What exactly is the problem? Skip spaces (`std::ws`), then `peek()` at the next character, and decide which format to use and whether it needs to be `ignore()`ed. These should be enough hints to finish the assignment. – Dietmar Kühl Sep 23 '15 at 22:23
  • @NeilKirk I know that in advance – Aikman TD Sep 24 '15 at 00:04

1 Answers1

0

To specify your format and read only specific elements you could overload the operator >>. For example, the first format: < int_value >, could be implemented with:

istream& operator>>(istream& is, class_name& array_name){
   char par2, par2;
   int  int_value;
   is >> par1 >> int_value >> par2;
   // check if any input
   if(!is) return is;
   // check for valid input format
   if (par1 != '<' || par2 != '>'){
       // set failbit to indicate invalid input format
       is.clear(ios_base::failbit);
       return is;
   }
   // assign input values to second argument
   array_name(int_value);
   // chain for consecutive use
   return is;
} 

and the second format: [ string_value ] with:

istream& operator>>(istream& is, class_name& separate_array_name){
   char par2, par2;
   string string_value;
   is >> par1 >> int_value >> par2;
   if(!is) return is;
   if (par1 != '[' || par2 != ']'){
       is.clear(ios_base::failbit);
       return is;
   }
   separate_array_name(string_value);
   return is;
} 

Note:

The second parameter in both examples: class_name& array_name and class_name& separate_array_name are not real types and are left for you to decide/ implement.

Edit:

  1. The function you've defined is not used in your main().
  2. The function needs to be either defined before the main() or forward declared.
  3. Limit the excessive use of pointers and dynamically allocated memory, as it needs to be taken care of (freed) at the end.
  4. Look up how to use istream: file name, modes, etc.
  5. Prefer simple functions, i.e. doing one single process. It is much more easier to implement and detect errors.
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • Does this using any STL stuff? I mean as long as I include iostream fstream and stringstream this can be used and requires no more right? Thank you. – Aikman TD Sep 24 '15 at 01:36
  • @Goodrich Clint yes, that is right! You can always use _try and error method_ on your compiler, i.e. write the code and try to compile it without including standard headers and then, depending on the error message make the respective corrections. – Ziezi Sep 24 '15 at 07:09
  • so actually there are lots of errors and also someone else suggested that this is just operator overloading.. I'm really not so good at c++ so can you give me some more detailed analysis? Thank you very much! @simplicis veritatis – Aikman TD Sep 24 '15 at 18:53
  • @Goodrich Clint I'm afraid that you should wait till you acquire a critical amount of knowledge to implement a proper program. SO is about dealing with specific problems, rather than tutorial-like place. You should check: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list and start from somewhere. That is all I can do for you on this stage :) – Ziezi Sep 24 '15 at 19:30