User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1118 | Replies: 3
Reply
Join Date: Nov 2007
Posts: 6
Reputation: SNN is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SNN SNN is offline Offline
Newbie Poster

Transpose

  #1  
Nov 27th, 2007
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

  1. #include <fstream>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5.  
  6.  
  7. int main()
  8. {
  9. using namespace std;
  10. ifstream datafile;
  11. ofstream outdatafile;
  12.  
  13.  
  14. char c,y1;
  15. int i,j;
  16.  
  17.  
  18. //datafile.open("sample.txt");
  19. outdatafile.open("outdatasample.txt");
  20.  
  21.  
  22.  
  23. //datafile.get(c);
  24.  
  25. i=0;
  26. j=0; // The total number of individuals
  27.  
  28. while (j <= 26)
  29. {
  30. datafile.open("sample.txt");
  31. datafile.get(c);
  32. cout<<c;
  33. j++;
  34. //cout<<j<<endl;
  35. while (!datafile.eof())
  36. {
  37. //cout<<j<<endl;
  38. i=0;
  39.  
  40. while (c!='\n')
  41. {
  42. if (isspace(c))
  43. {
  44. }
  45. else
  46. {
  47. i++;
  48. if (i==j)
  49. {
  50. y1=c;
  51. datafile.get(c);
  52. outdatafile<<y1<<'\t';
  53. } // if
  54. } //else
  55.  
  56.  
  57. datafile.get(c);
  58. } // while \n
  59.  
  60. datafile.get(c);
  61.  
  62. } //while eof
  63.  
  64. outdatafile<<'\n';
  65. datafile.close();
  66.  
  67. }// while j
  68.  
  69. outdatafile.close();
  70. system("pause");
  71. return 0;
  72.  
  73. }//main
Last edited by Ancient Dragon : Nov 27th, 2007 at 8:11 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Transpose

  #2  
Nov 27th, 2007
>>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 ?
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: SNN is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
SNN SNN is offline Offline
Newbie Poster

Re: Transpose

  #3  
Nov 29th, 2007
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,
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Transpose

  #4  
Nov 29th, 2007
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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC