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:

  1. Use a while loop to read each line until EOF
  2. Define each line as string1
  3. Tokenize string 1 after the character 's'
  4. Use if statements to determine whether a 'm V' or 'm s' is present in the string
  5. Convert string1 to a double using atof().
  6. If conversion is needed, do it. If not, do nothing. Store new value as newValue.
  7. 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.

Something tells me the problem comes in with the first error (ie when you solve that it might take the others away as it seems to be a chain taking place in the order they reference each other)

I do not carry much knowledge on "std::basic_istream<char, std::char_traits<char>>" type coding as I use external variables for my programs, however that second error I would know anyplace

What it is telling you is that you are trying to use a char variable where a pointer to a char variable is expected

in other words if you create something like the following it should solve that:

char* sString = new char();
sString = 's';
strtok(string1, sString);//strtok(string1, 's');

using the same logic it might mean that you need to load the "lineData" variable into a char* as well and change the string in the same way... it MIGHT work... wont hurt to try I guess?

Thanks, Eagletalon, your thoughts make sense to me. However, I'm now getting a new "error" in this area. Any thoughts? I must be mixing up my char * and char.

Here is my new function. There is a new error on the line with Note 1. The atof function errors remain as does the inFile error.

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);	 
		char* sString = new char();	
		sString = 's'; //Note1
		strtok(string1, sString);					
												
		if(strchr(string1,'m V'))
			atof(string1)=voltage;				
			newValue=voltage*1000;
		if(strchr(string1, 'm s'))
			atof(string1)=secondage;			
			newValue=secondage*1000;
		else									
			atof(string1)=newValue;
		for(i=0;i<=2499;i++)
			for(j=0;j<=1;j++)
				signal[i][j]=newValue;
	}
	cout<<endl<<signal[i][j]<<endl;

	inFile.close()
}

Note 1: The equal sign is underlined in red, because, "A value of type 'char' cannot be assigned to an entity of type 'char *'"

Certainly looks self-explanatory to me. You can't make a char* (sString array) equal to a single character ('s')

If you think you "must be mixing up my char * and char" then look at the statements in question and verify it. Why ask if that's what you think and waste an hour waiting for us to confirm and spoon feed an answer?

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.