Sep 9, 2013 at 3:23pm UTC
Hello,
Im trying to read a textfile. Its like this:
Time / s Port1 (1)/real
----------------------------------------------------------------------
0 -0
1.342654e-005 -0
1.3426541e-005 0.00026781721
1.3426542e-005 0.00026782084
1.3426543e-005 0.00026782326
1.3426544e-005 0.00026782683
1.3426544e-005 0.00026783112
1.3426545e-005 0.00026783649
1.3426546e-005 0.00026784233
1.3426547e-005 0.00026784882
..... .....
I want to create a new file with the same header, but i want to reduce the data. To entrys of Time are used to calculate the middle (like: 2sec + 4sec -> 3sec) and the same has to be done with the Signal Value. (like: 6Volt + 8Volt ->7Volt)
I tried to make a code. Im able to read and give out every second line now, but i cant create the doubles from the string-input to calculate the middle. Can anyone help me please? Thanks a lot...
I marked the part of the code with bold letters where im trying to convert the variables.
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inFile;
ofstream outFile;
inFile.open("CST_OutReduced.txt");
outFile.open("C_Ausgabe.txt");
string item;
char item2[];
double Freq1;
double Freq2;
double Sig1;
double Sig2;
double itemdouble;
int MeasurementNumber = 1;
int count = 0;
int wichone = 0;
while (!inFile.eof()) {
inFile >> item;
count++;
wichone++;
// write header
if (item == "Time")
{
outFile << "Time / s Port1 (" << MeasurementNumber << ")/real" << endl;
outFile << "----------------------------------------------------------------------" << endl;
MeasurementNumber++;
wichone = -20;
}
{
if (item =="----------------------------------------------------------------------")
wichone = -1;
}
//if the item is not part of the header: create a double
if (wichone >= 0)
{
item2 = item;
itemdouble=atof(item2);
}
// Select Frequency or Signal
if (wichone == 0)
{
Freq1 =itemdouble;
}
if (wichone == 1)
{
Sig1 = itemdouble;
}
if (wichone == 2)
{
Freq2 = itemdouble;
}
if (wichone == 3)
{
Sig2 =itemdouble;
}
// Calculate middle and save data in new File
if (wichone ==3)
{
outFile << " " << (Freq1 + Freq2)/2 << " " << (Sig1 + Sig2)/2 << endl;
wichone = -1;
}
}
cout << count << " items found! Last Item: " << item << endl;
int n ;
cin >> n;
outFile.close();
inFile.close();
return 0;
}
Last edited on Sep 9, 2013 at 3:25pm UTC