Hey everyone,
I am working on a function that reads a text file (portion below) of data points arranged in two columns and stores the numbers only in an array of signal[2500][2]. The problem is, I need to do some search and replace on some of the data points. Some are mV and msec, while others are uV and usec. As there are negative signs on some values, I cannot simply choose the first six places in each line (or similar). My intent is to convert those lines that are mV and ms to uV and usec and then store all the values in the signal array. However, I'm getting a few error messages and would like some feedback on how to fix them. They are listed at the bottom.
The more I look at this, the more I'm not sure I should be using strtok, or I'm not using it the right way. I know it needs to accept a 'const char *' but I'm blanking on what to send it.
An outline of my thought process:
- Use a while loop to read each line until EOF
- Define each line as string1
- Tokenize string 1 after the character 's'
- Use if statements to determine whether a 'm V' or 'm s' is present in the string
- Convert string1 to a double using atof().
- If conversion is needed, do it. If not, do nothing. Store new value as newValue.
- Use two for loops to cycle through the array columns and rows and populate each with newValue.
A snippet of the text file is here. There are 2501 lines total, the first being simply a header line.
-1.0060m s 112.00m V
-1.0040m s 112.00m V
-1.0020m s 116.00m V
-1.0000m s 116.00m V
-998.00u s 120.00m V
-996.00u s 120.00m V
-994.00u s 124.00m V
My code for the function is here:
Relevant header files:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cmath>
#include "spectrum.h"
using namespace std;
Relevant parameters defined in header file:
char* header, lineData[100];
char* string1;
char string2;
double voltage, secondage, newValue;
float signal[2500][2];
The function:
void Spectrum::searchReplace(ifstream &inFile)
{
inFile.getline(header, 99); //gets first header line and does nothing with it. now "pointer" is at second line
while (!inFile.eof())
{
string1 = inFile.getline(lineData,99); //Note 1
strtok(string1, 's');//Note 2
if(strchr(string1,'m V'))
atof(string1)=voltage;//Note 3
newValue=voltage*1000;
if(strchr(string1, 'm s'))
atof(string1)=secondage; //Note 3 (again)
newValue=secondage*1000;
else //Note 4
atof(string1)=newValue;
for(i=0;i<=2499;i++)
for(j=0;j<=1;j++)
signal[i][j]=newValue;
}
Note 1: inFile is underlined in red because 'no suitable conversion from 'std::basic_istream<char, std::char_traits<char>>' to 'char *' exists"
Note 2: 's' is underlined because "argument of type 'char' is incompatible with parameter of type 'const char *'"
Note 3: atof is underlined in red because it says "the expression must be a modifiable value"
Note 4: "error: expected a statement"
Thanks for your help.