2

Basically, I know virtually nothing about C++ and have only programmed briefly in Visual Basic.

I want a bunch of numbers from a csv file to be stored as a float array. Here is some code:

string stropenprice[702];   
float openprice[702];
int x=0;
ifstream myfile ("open.csv");
if (myfile.is_open())
{
  while ( myfile.good() )
  {
    x=x+1;
    getline (myfile,stropenprice[x]);
    openprice[x] = atof(stropenprice[x]);
    ...
  }
  ...
}

Anyways it says:

error C2664: 'atof' : cannot convert parameter 1 from 'std::string' to 'const char *'

1 Answer 1

4

Well, you'd have to say atof(stropenprice[x].c_str()), because atof() only operates on C-style strings, not std::string objects, but that's not enough. You still have to tokenize the line into comma-separated pieces. find() and substr() may be a good start (e.g. see here), though perhaps a more general tokenization function would be more elegant.

Here's a tokenizer function that I stole from somewhere so long ago I can't remember, so apologies for the plagiarism:

std::vector<std::string> tokenize(const std::string & str, const std::string & delimiters)
{
  std::vector<std::string> tokens;

  // Skip delimiters at beginning.
  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  std::string::size_type pos     = str.find_first_of(delimiters, lastPos);

  while (std::string::npos != pos || std::string::npos != lastPos)
  {
    // Found a token, add it to the vector.
    tokens.push_back(str.substr(lastPos, pos - lastPos));
    // Skip delimiters.  Note the "not_of"
    lastPos = str.find_first_not_of(delimiters, pos);
    // Find next "non-delimiter"
    pos = str.find_first_of(delimiters, lastPos);
  }

  return tokens;
}

Usage: std::vector<std::string> v = tokenize(line, ","); Now use std::atof() (or std::strtod()) on each string in the vector.


Here's a suggestion, just to give you some idea how one typically writes such code in C++:

#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>

// ...

std::vector<double> v;

std::ifstream infile("thefile.txt");
std::string line;

while (std::getline(infile, line))
{
  v.push_back(std::strtod(line.c_str(), NULL));  // or std::atof(line.c_str())
}

// we ended up reading v.size() lines
Sign up to request clarification or add additional context in comments.

3 Comments

holy crap, this site makes me feel dumb. I don't even know how to respond to you directly. Hopefully this notifies you somehow. I have set it up so far that I only work with one long column of data, so I don't have to worry about commas yet. Anyways its working now, and I will be working with commas eventually, so thank you so much. Cheers, Ben
@user1040781: No problem :-) (Apparently I do get notified of comments to my own posts; in general, use the @ syntax to create an alert.) Good luck!
@user1040781: While you're at it, you might replace the rather clunky fixed-size array with a vector as well. I'll edit the post to add a suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.