I am trying to modify a program to read in a file that will then output it to a new file without all the whitespace in between the data. This is so I can convert it to a csv file and read it into paraview to make a visualization of a human skull. I don't have a lot of experience with fstream, but I know that is what I need to use. Here is the code I have so far

#include <fstream>
#include <iostream>
#include <string>

//pdisk.cpp

using namespace std;

class Pdisk {
public:
Pdisk(int numberofblocks, int blocksize, string diskname);
int getblock(int blocknumber, string& buffer);
int putblock(int blocknumber, string buffer);
private:
string diskname;
int numberofblocks;
int blocksize;
};

Pdisk::Pdisk(int numberofblocks, int blocksize, string diskname)
{
fstream iofile;
iofile.open(diskname.c_str(), ios::in|ios::out);
iofile.close();

if (iofile.fail())
{
cout << "File does not exist" << endl;
ofstream newfile;
newfile.open(diskname.c_str());
for (int i = 0; i < (numberofblocks * blocksize); i++)
{
newfile << "*"; // fill file with '*'
}
cout << "File created successfully" << endl;
newfile.close();
iofile.open(diskname.c_str(), ios::in|ios::out);
this->diskname = diskname;
this->blocksize = blocksize;
this->numberofblocks = numberofblocks;
iofile << numberofblocks << " " << blocksize << " ";
iofile.close();
}
else
{
iofile.open(diskname.c_str(), ios::in|ios::out);
this->diskname = diskname;
this->blocksize = blocksize;
this->numberofblocks = numberofblocks;
iofile >> numberofblocks; //read numberofblocks from file
iofile >> blocksize; //read blocksize from file
// cout << "File exists - Number of blocks: " << numberofblocks << endl;
// cout << "File exists - Blocksize: " << blocksize << endl << endl;
iofile.close();
}
} // Pdisk



int Pdisk::getblock(int blocknumber, string& buffer)
{
fstream iofile;
iofile.open(this->diskname.c_str(), ios::in|ios::out);
if (iofile.fail())
{
cout << endl << "Getblock failed when opening file" << endl;
return 0;
}
else
{
if (blocknumber > this->numberofblocks)
{
cout << endl << "Blocknumber doesn't exist. Getblock failed" << endl;
return 0;
}
else
{
iofile.seekg((blocknumber*blocksize), ios::beg);
char x;

buffer = ""; // clear buffer
for (int i=0; i<this->blocksize; i++)
{
// iofile >> x;
iofile.get(x);
buffer = buffer + x;
}
// cout << "Contents of getblock buffer are " << endl;
// cout << buffer << endl << endl;
iofile.close();
return 1;
}
}

}


int Pdisk::putblock(int blocknumber, string buffer)
{
fstream iofile;
iofile.open(this->diskname.c_str(), ios::in|ios::out);
// cout << "Filename: " << this->diskname.c_str() << endl;
// cout << "Number of blocks: " << numberofblocks << endl;
// cout << "Blocksize: " << blocksize << endl;
if (iofile.fail())
{
cout << endl << "Putblock failed when opening file" << endl;
return 0;
}
else if (blocknumber == 0)
{
cout << "Block 0 is write-protected. Unable to complete request!!!" << endl;
return 0;
}
else if (blocknumber > this->numberofblocks)
{
cout << endl << "Blocknumber is out of range. Getblock failed" << endl;
return 0;
}
else
{
iofile.seekp((blocknumber*blocksize), ios::beg);
cout << endl << "Putblock success" << endl;
// cout << "Buffer.size = " << buffer.size() << endl;
// cout << "Blocksize = " << blocksize << endl;

if (buffer.size() == blocksize)
{
iofile << buffer; // write contents of buffer to file
}
else
{
cout << "Size of buffer is not the same as blocksize" << endl;
return 0;
}
iofile.close();
return 1;
}
}

int no_blocks = 16;
int size_of_block = 30;
int putblockfail;
int getblockfail;

int main()
{
Pdisk disk1(no_blocks, size_of_block, "hw1.txt");
string block1, block2, block3, block4;
for (int i=0; i<size_of_block; i++)
{
block1 = block1 + "1";
block2 = block2 + "2";
block3 = block3 + "3";
block4 = block4 + "$";
}
putblockfail = disk1.putblock(8, block1);
if (putblockfail == 1)
{
cout << endl << "Block3 before getblock" << endl;
cout << block3 << endl << endl;
disk1.getblock(8, block3);
cout << "Block3 after getblock" << endl;
cout << block3 << endl;
}

putblockfail = disk1.putblock(3, block2);
if (putblockfail == 1)
{
cout << endl << "Block2 before getblock" << endl;
cout << block2 << endl << endl;
getblockfail = disk1.getblock(17, block2);
if (getblockfail == 1)
{
cout << "Block2 after getblock" << endl;
cout << block2 << endl;
}
disk1.putblock(1, block4);
}

} //end main

The file that I have to read in is 50 MB so I won't post the whole thing, but it has five columns of data for each line that are separated by more than 1 whitespace character. Here is a small example of the file:

0000000 4063268 7012368 2883603 3407915
0000020 1376285 1572886 2424870 2686998
0000040 2097198 458787 1572897 3604518
0000060 1114132 1835028 3276857 2228282
0000100 4390976 1310739 2228237 3604540
0000120 1114173 5242947 3539070 589868
0000140 3211327 786440 2424883 1114177
0000160 2424853 3932182 2556003 3342365
0000200 2031635 2818068 9240661 6684683
0000220 3014715 1507397 2883652 3145746
0000240 1835030 3866671 786491 1835049
0000260 1441804 1179663 4522007 1179688
0000300 1966109 3801126 6684716 2490408
0000320 917521 2883601 5374043 4587569

The int main() needs to be re-written but I am not sure how to change the Pdisk class itself.

Thanks

You may not have to do anything with that file other than rename it with csv extension. If paraview is like Excell then you can specifiy column deliminator, and in your case it is a space. If paraview can not do that, then your program should be fairly simple

open input file
open output file
while getline() is ok
    replace all spaces with a comma
    write line to the output file
end of while loop
end of program

If you use std::string objects your job is very simple because it has a function to replace all instances of one character with another.

It's quite rare to see a first-time poster use code tags correctly.
It's rarer still to expect the code to be indented in any meaningful way.

I know how to follow directions and I am normally very good at indenting my code for easy reading

I know the sample data I provided looks like there is only one space between the data, if that were the case, I wouldn't need help. There are actually about 5 spaces between each data input.

Ok, I see that now. well, the easiest way to do it is like this:

ifstream in("infile.txt"); // or whatever file name you gave it
ofstream out("outfile.csv");
string word;
string line;
while( getline(in,line) )
{
   stringstream stream;
   stream << line;
   while( stream >> word )
   {
         out << word << ",";
   }
   out << "\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.