Hi,

I have a text file that contains data seperated by a tab. I know that there are 26 columns but the number of rows are unknown b/c the file is huge. I need to traspose the data so I wrote the following script. I open the data file 26 times and each time it changes the column into a row. I know that the problem is in the statement of
datafile.open("sample.txt"); is the problem. I would appreciate if someone can help.


Thanks

#include <fstream>
#include <iomanip>
#include <iostream>



int main()
{
using namespace std;
ifstream datafile;
ofstream outdatafile;


char c,y1;
int i,j;


//datafile.open("sample.txt");
outdatafile.open("outdatasample.txt");



//datafile.get(c);

i=0;
j=0;  // The total number of individuals

while (j <= 26)
{
datafile.open("sample.txt");
datafile.get(c);
cout<<c;
j++;
//cout<<j<<endl;
   while (!datafile.eof())
   {
   //cout<<j<<endl;    
   i=0;
   
      while (c!='\n')
      {    
      if (isspace(c))
      {
      }
        else
        {
        i++;  
          if (i==j)
          {
          y1=c;
          datafile.get(c);
          outdatafile<<y1<<'\t';
          } // if  
       } //else      


      datafile.get(c);
      } // while \n
   
datafile.get(c);

} //while eof 

outdatafile<<'\n';
datafile.close();

}// while j

outdatafile.close();
system("pause");
return 0; 

}//main

Recommended Answers

All 3 Replies

>>I open the data file 26 times
Why ??? Your program is failing to close the file before opening it again.

>>I need to traspose the data
What do you mean by that? to make columns rows and rows columns ?

Yes, I need to make the rows into columns. I have 26 columns and about 300,000 rows. i am new to C++, so i wrote the program that each time it opens the file it reads a column and make into a row. I appreciate if someone can help.

Thanks,

First I would completly write a new file with the information from the original. Read the original file 26 times (once for each column). The first time through read only the first column of each 300,000 rows and write them out to the new file as the first row. Rewind the file back to the beginning them read again but this time only read the second column. Write those out to the new file as the second row. Repeat the process for each of the 26 rows in the original file. Note that it is only necessary to open the original file ONCE, not 26 times.

If your computer has enough RAM you might be able to do this by reading the original file all into memory at one time, reformat it in memory and write it out to the output file. This would be the fastest way, but also the most memory intensive.

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.