Vector!! Can you tell me what's wrong? 
Author Message
 Vector!! Can you tell me what's wrong?

#include <iostream>
#include <vector>
#include <fstream>
using namespace std ;

typedef vector<char*> INTVECTOR;
  char  pFileName[1024];
  strcpy(pFileName, "IPAddress.txt");
// I wrote 4 lines into a txt file, then wanted to read them into a vector
  ofstream oftmp(pFileName,ios::out | ios::ate);
  oftmp << "IOS 2"<< endl;
  oftmp << "IOS 3"<< endl;
  oftmp << "IOS 4"<< endl;
  oftmp << "IOS 6"<< endl;
  oftmp.close();

  fstream ifPtr;
  char* buf = new char[1024];
  const int MAX_BUF_LEN   = 255;
  int iLineCount = 0;

  INTVECTOR theVector;
  ifPtr.open(pFileName);
  while(ifPtr.getline(buf, MAX_BUF_LEN - 1)) {
// Here I pushed those data into the vector
   theVector.push_back(buf);

  }

// But I can't  get those data using the below statement
    cout << "First element: " << theVector.front() << endl;
    cout << "Last element: " << theVector.back() << endl;
// but the size is correct.
    cout << "Elements in vector: " << theVector.size() << endl;

//Can you tell me what's wrong?



Fri, 24 Oct 2003 11:40:25 GMT  
 Vector!! Can you tell me what's wrong?
Does it work using the standard begin and end functions (with derefencing) ?
e.g.

     cout << "First element: " << *(theVector.begin()) << endl;
     cout << "Last element: " << *(theVector.end()-1) << endl;

or the index operator ?

     cout << "First element: " << theVector[0] << endl;
     cout << "Last element: " << theVector[theVector.size()-1] << endl;

I've never use the front/back functions, but the above methods should also
work and tell you whether it's a problem with you files or your vectors.

Craig


Quote:
> #include <iostream>
> #include <vector>
> #include <fstream>
> using namespace std ;

> typedef vector<char*> INTVECTOR;
>   char  pFileName[1024];
>   strcpy(pFileName, "IPAddress.txt");
> // I wrote 4 lines into a txt file, then wanted to read them into a vector
>   ofstream oftmp(pFileName,ios::out | ios::ate);
>   oftmp << "IOS 2"<< endl;
>   oftmp << "IOS 3"<< endl;
>   oftmp << "IOS 4"<< endl;
>   oftmp << "IOS 6"<< endl;
>   oftmp.close();

>   fstream ifPtr;
>   char* buf = new char[1024];
>   const int MAX_BUF_LEN   = 255;
>   int iLineCount = 0;

>   INTVECTOR theVector;
>   ifPtr.open(pFileName);
>   while(ifPtr.getline(buf, MAX_BUF_LEN - 1)) {
> // Here I pushed those data into the vector
>    theVector.push_back(buf);

>   }

> // But I can't  get those data using the below statement
>     cout << "First element: " << theVector.front() << endl;
>     cout << "Last element: " << theVector.back() << endl;
> // but the size is correct.
>     cout << "Elements in vector: " << theVector.size() << endl;

> //Can you tell me what's wrong?



Sun, 26 Oct 2003 17:25:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. can you tell me what's wrong with the code

2. what's wrong the code??[vector]

3. Vector of vectors - compiler doesn't understand?

4. can somebody tell what is wrong?

5. Please tell me what went wrong...

6. Please look at this code n tell me where i am wrong

7. Who can tell me where is wrong?

8. Can anyone tell me what is wrong?

9. Can anyone tell me what is wrong?

10. Please tell me what im doing wrong :(

11. Can anyone tell me what is wrong?

12. Can anyone tell me what is wrong?

 

 
Powered by phpBB® Forum Software