•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
c++ Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon : Nov 27th, 2007 at 8:11 pm. Reason: add code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
>>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 ?
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 ?
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Transpose matrix (w\ pointers) (C)
- Transpose & Inverse Matrix, Matrix Determinant (C++)
- Transpose a matrix (C)
- matrix transpose (C)
- How to transpose a matrix? (Java)
Other Threads in the C++ Forum
- Previous Thread: Localization application
- Next Thread: selamat pagi semua ^^ ...question about stream



Linear Mode