I want to devide the text in file data.txt into an array of char*( mean i use the pointer char** q) by the way line 1 for q[1], line 2 for q[2] and.... but i don't know why it not work can everyone help me ??? :(

 char **q;
   const int SIZE = 10000;   
   char input[SIZE];      
   fstream nameFile;    
   int n=0;

   nameFile.open("data.txt", ios::in); 
   if (!nameFile) 
   { 
      cout << "ERROR: Cannot open file.\n"; 
   } 
   nameFile.getline(input, SIZE);  //Dùng kí tự mặc định \n như kí tự kết thúc. 
   while (!nameFile.eof()) 
   { 
      n++;
      nameFile.getline(input, SIZE);  //Chỗ này cũng vậy. 
   } 
   q=new char*[n]; //cấp phát động cho mảng q;
   for(int i=0;i<n;i++)
    { 
          q[i]= new char[SIZE];
    }
   int i =0;
   nameFile.clear();
   nameFile.seekg(0,nameFile.beg);
   while (!nameFile.eof()) //ghi vào từng hàng tương ứng với từ mảng kí tự q1,q2,..
   { 
      nameFile.getline(q[i++], SIZE);
   } 
   for(int i=0;i<n;i++)
    { 
          gets(q[i]);
    }
   nameFile.close(); 

Recommended Answers

All 5 Replies

why the loop on line 30? that is destroying the contents of the file that was read on likne 26.

what u mean but i use cout<< it not out :((

Look at line 32 -- it's gets(), not cout. gets() waits for you to enter a string from the keyboard and overwrites whatever was in that character array.

but it you know if this was cout it printed ==============================================================================================================================================================

for a whole of screen :(

I think your poblem is line 28: After reading the last line in the file line 28 attempts to read the next line, indexing one beyond the end of the q array. Change the while on line 26 to while( i < n)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.