Sorry for the ambiguity I just don't want to make it like I'm forcing my whole homework requirements down people's throat.
Basically the assignment requires me to read three types: int, float and char.
So it'd look something like this,
In file
int 3 1 2 3
float 2 1.0 4.0 |
Out file result:
int 3 3 2 1
float 2 4.0 1.0 |
There's an unknown number of lines and three fields seen above.
Type, Size of seq, and then the sequence itself.
My job is to read a file like that, reverse the sequence and then write it in the file with the same format.
So far I've dumbed down my code to the most basic level possible. However, I'm still having trouble x[.
My ostringstream is not getting the characters and it's giving me random jargon.
e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
/*
* reader.h
*/
#ifndef READER_H_
#define READER_H_
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
class Reader {
public:
Reader();
~Reader();
void inverseWrite(std::ifstream&, std::ofstream&);
void readData(std::ifstream&);
void reverseData(std::string);
void writeData(std::ofstream&);
int readLine(std::ifstream&);
private:
std::string dType;
int dSize;
std::string dSeq;
//
int *iPtr;
float *floatPtr;
char *charPtr;
int numberOfLines;
std::ostringstream fullText;
std::ostringstream reversedOutput;
};
#endif /* READER_H_ */
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
/*
* main.cpp
*/ #include "reader.h"
int main(int argc, char *argv[]){
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << "\n";
}
std::ifstream inFile(argv[1]);
if (!inFile)
std::cout << "File not open";
std::ofstream outFile(argv[2]);
Reader obj;
obj.inverseWrite(inFile, outFile);
inFile.close();
outFile.close();
}
|
My main code performs the test, nothing really important in there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/*
* reader.cpp
*/
#include "reader.h"
Reader::Reader():
numberOfLines(0)
{}
Reader::~Reader(){
delete iPtr;
delete floatPtr;
delete charPtr;
}
void Reader::inverseWrite(std::ifstream& read, std::ofstream& write){
readLine(read);
readData(read);
writeData(write);
}
void Reader::readData(std::ifstream& read){
while (read >> dType){
read >> dSize;
std::getline(read, dSeq);
reverseData(dSeq);
fullText << reversedOutput << "\n"; //HERE IS A PROBLEM
}
} //Read data and set ostringstream
void Reader::reverseData(std::string seq){
std::stringstream tempLine(seq);
if (dType == "int"){
int tempValue;
iPtr = new int[dSize];
for (int i=0 ; i < dSize; i++){
tempLine >> tempValue;
iPtr[i] = tempValue;
}
reversedOutput << dType << " " << dSize << " "; //HERE IS A PROBLEM TOO
for (int i = dSize-1; i >=0; i--)
reversedOutput << iPtr[i] << " ";
}
else if (dType == "float"){
float tempValue;
floatPtr = new float[dSize];
for (int i=0 ; i < dSize; i++){
tempLine >> tempValue;
floatPtr[i] = tempValue;
}
reversedOutput << dType;
for (int i = dSize-1; i >=0; i--)
reversedOutput << floatPtr[i] << " ";
}
else if (dType == "char"){
char tempValue;
charPtr = new char[dSize];
for (int i=0; i <dSize; i++){
tempLine >> tempValue;
charPtr[i] = tempValue;
}
reversedOutput << dType << " " << dSize << " ";
for (int i= dSize-1; i>=0; i--)
reversedOutput << charPtr[i] << " ";
}
}
void Reader::writeData(std::ofstream& write){
write << fullText;
}
int Reader::readLine(std::ifstream& read){
numberOfLines = 0;
std::string holder;
while (std::getline(read,holder))
numberOfLines++;
read.clear();
read.seekg(0);
return numberOfLines;
}
|
My main problem here is my
std::ostringstream reversedOutput
is not registering the input I'm giving.
For example:
reversedOutput << dType << " " << dSize << " ";
Would just give me an address of some sort like :
Sorry for the burden, I'm just having a really hard time mashing all the concept together with I/O streams
My whole code is a mess I've been working on this for days and it's just so hard to tackle all these bugs that I get.