Dec 21, 2012 at 10:09am UTC
Hello all,
I need to read the double values of the following file:
-----------------------------
Weight = 85.45 tons
LCG = 8.32104 m
VCG = 0.6096 m
Ca = 0
Bx = 4.8768 m
ThrustArm = 0 m
Deadrise = 19 deg
ShaftAng = 14.4 deg
Use App = true
Use MFac = false
Use Air = true
Use Spray = true
Af = 13.9355 m^2
Ws = 0 knots
Vk = 10 knots
-----------------------------
Now I'm using:
ifstream readFile("Inputs/Calm Water/Planing.txt");
while(!readFile.eof())
{
getline(readFile,line);
stringstream strStream(line);
double n;
strStream >> n;
cout << n <<endl;
system("pause");
}
But it's not working. I'm using codeblocks on Windowns 7.
Can anyone help me?
Thanks
Last edited on Dec 21, 2012 at 10:10am UTC
Dec 21, 2012 at 12:21pm UTC
I tried, but I failed. This was my solution in c/c++:
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
double n;
char BUFF[120];
FILE* readFile = fopen("Planing.txt" , "r+" );
if (NULL != readFile)
{
cout<<"The file opened success!" <<endl;
while (fgets(BUFF, sizeof (BUFF), readFile) != NULL)
{
cout << BUFF;//just to see what Buff got
if (sscanf(BUFF, "%lf" , &n))
cout << " " << n <<endl;
}
}
fclose(readFile);
return EXIT_SUCCESS;
}
I can't believe it didn't work. I am surprised
Last edited on Dec 21, 2012 at 12:29pm UTC
Dec 21, 2012 at 1:08pm UTC
I thinks it should work also.
I solved creating a class:
.h File:
------------------------------------------------------------------------------------
enum flagsPlaning { Weight, LCG, VCG, Ca, Bx, ThrustArm, Deadrise, ShaftAng, UseApp, UseMFac, UseAir, UseSpray, Af, Ws, Vk };
class ControlPCD
{
public:
/** Default constructor */
ControlPCD();
/** Default destructor */
virtual ~ControlPCD();
void readInPlaning(NSavitsky_v2 &obj,std::string fileName);
protected:
private:
void initializaMapEnumPlaning();
std::map<std::string, flagsPlaning> m_mapEnumPlaning;
};
--------------------------------------------------------------------------
cpp file:
------------------------------------------------------------------------
using namespace std;
ControlPCD::ControlPCD()
{
initializaMapEnumPlaning();
//ctor
}
ControlPCD::~ControlPCD()
{
//dtor
}
void ControlPCD::initializaMapEnumPlaning()
{
m_mapEnumPlaning["Weight"] = Weight;
m_mapEnumPlaning["LCG"] = LCG;
m_mapEnumPlaning["VCG"] = VCG;
m_mapEnumPlaning["Ca"] = Ca;
m_mapEnumPlaning["Bx"] = Bx;
m_mapEnumPlaning["ThrustArm"] = ThrustArm;
m_mapEnumPlaning["Deadrise"] = Deadrise;
m_mapEnumPlaning["ShaftAng"] = ShaftAng;
m_mapEnumPlaning["UseApp"] = UseApp;
m_mapEnumPlaning["UseMFac"] = UseMFac;
m_mapEnumPlaning["UseAir"] = UseAir;
m_mapEnumPlaning["UseSpray"] = UseSpray;
m_mapEnumPlaning["Af"] = Af;
m_mapEnumPlaning["Ws"] = Ws;
m_mapEnumPlaning["Vk"] = Vk;
}
void ControlPCD::readInPlaning(NSavitsky_v2 &obj,std::string fileName)
{
std::string line;
std::string flag;
std::string aux;
double val;
bool boolVal;
std::ifstream readFile(fileName.c_str());
if(readFile)
{
while(!readFile.eof())
{
getline(readFile,line);
std::stringstream strStream(line);
strStream >> flag;
strStream >> aux;
switch(m_mapEnumPlaning[flag])
{
case Weight:
strStream >> val;
obj.setWeight(val);
break;
case LCG:
strStream >> val;
obj.setLCG(val);
break;
case VCG:
strStream >> val;
obj.setVCG(val);
break;
case Ca:
strStream >> val;
obj.setCa(val);
break;
case Bx:
strStream >> val;
obj.setBx(val);
break;
case ThrustArm:
strStream >> val;
obj.setThrustArm(val);
break;
case Deadrise:
strStream >> val;
obj.setDeadrise(val);
break;
case ShaftAng:
strStream >> val;
obj.setShaftAng(val);
break;
case UseApp:
strStream >> std::boolalpha >> boolVal;
obj.setUseApp(boolVal);
break;
case UseMFac:
strStream >> std::boolalpha >> boolVal;
obj.setUseMFactor(boolVal);
break;
case UseAir:
strStream >> std::boolalpha >> boolVal;
obj.setUseAir(boolVal);
break;
case UseSpray:
strStream >> std::boolalpha >> boolVal;
obj.setUseSpray(boolVal);
break;
case Af:
strStream >> val;
obj.setAf(val);
break;
case Ws:
strStream >> val;
obj.setWs(val);
break;
case Vk:
strStream >> val;
vector<double> *vecVk;
vecVk = obj.getVk();
vecVk -> clear();
vecVk -> push_back(val);
break;
}
}
}
}
------------------------------------------------------------------------------------
Thanks for your help
Dec 21, 2012 at 1:10pm UTC
It looks like the strStream >> val command could not read a double values if it was not on the right File position .