0

I have a .txt file that contains some thousand lines of float values I have to work with. I don't know a priori how many values every specific line has. I want to store all the values in one line into a single array.

This is all I've got so far:

string line;
int row = 0;
ifstream file_input (input.c_str());
while ( getline(file_input, line) )
{
    row++;
    if (row == 8)    // if I want to read the 8th line
    {
        cout << line;
    }
}

This code print all the content of the 8th line, which is a large amount of values altogether. I want to store all these values into an array. How can I do that?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42

2 Answers2

2

You can use std::stringstream (or std::istringstream) to read those floats from the line and push them into std::vector.

std::vector<float> vec;
std::string line;
std::ifstream file_input (input);
int row = 0;
while (getline(file_input, line))
{
    ++row;

    if (row == 8)    // if I want to read the 8th line
    {
        cout << line;

        std::istringstream iss(line);
        float value; // auxiliary variable to which you extract float from stringstream

        while(iss >> value)        // yields true if extraction succeeded
            vec.push_back(value);  // and pushes value into the vector

        break;
    }
}

Or see this post for some more professional ways.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
0

Since you have taken a line of the file into a string, you can convert the string into a vector of char and make a char* point to this vector and pass the char* into the following function as the first parameter:

char * strtok ( char * str, const char * delimiters );

The second parameter in your case would be a blank space " ". The received char pointer can be then converted to a float and each token can be stored in an array!

int nums(char sentence[ ])  //function to find the number of words in a line
    {
     int counted = 0; // result

    // state:
    const char* it = sentence;
    int inword = 0;

    do switch(*it) {
        case '\0': 
        case ' ': case '\t': case '\n': case '\r': 
            if (inword) { inword = 0; counted++; }
            break;
        default: inword = 1;
    } while(*it++);

    return counted;
}

int main()
{
ifstream file_input (input.c_str());
int row=0;
while ( getline(file_input, abc) )
{

vector<char> writable(abc.begin(), abc.end());
vector<char> buf(abc.begin(), abc.end());

writable.push_back('\0');
buf.push_back('\0');

char *line=&writable[0];

char *safe=&buf[0];


char* s= "123.00";

        float array[100];
        char *p;
        int i=0;

       p= strtok (line, " ");
       array[i]=atof(p);
       cout<<" "<<array[i];

       i++;
         while (i<nums(safe))
          {
              p = strtok (NULL, " ");   //returns a pointer to the token
              array[i]=atof(p);
              cout<<" "<<array[i];
              i++;
          }
     return 0;
}

Unfortunately the loop: while(p!=NULL) is not working out and resulting in a segmentation fault so I have had to pre-calculate the number of words in the line using another vector since strtok() modifies the string passed as char* to it.

Aditya Sanghi
  • 289
  • 1
  • 3
  • 18