0

New to c++ and programming, and I am having trouble figuring this out.

Pseudo idea; Assuming I can define the delimiters

 ',' ',' '\n' 

then read file line by line while adding each element to each array.

#define ARRAY_SIZE 1000 

string someString[ARRAY_SIZE];
double someDoubble[ARRAY_SIZE];
double someDoubble[ARRAY_SIZE]; 

Then the text file that I am reading is

somestring, someDouble, someDouble\n    

Or in other words the delimiters are comma, comma, newline char.

pemby
  • 1
  • 1
  • Are you finding difficulties in reading file or tokenizing the read line based on delimeter ? – Mahesh Aug 17 '13 at 04:01
  • No issues reading the file line by line, Just I have no clue on how to tokenizing the read line by delimiter in c++ – pemby Aug 17 '13 at 04:03
  • [Tokenize a string based on delimeter](http://stackoverflow.com/questions/10051679/c-tokenize-string). – Mahesh Aug 17 '13 at 04:06
  • Thank you for your input! Due to my level of knowledge, the function that is listed makes no sense to me, and leaves me wondering how would I use that to get my data into an array. The language I am strongest in is bash, and I am really new to c++. Could you illuminate for me how you see this being applicable, to what I am asking? Sorry, it seems a bit over my head. – pemby Aug 17 '13 at 04:11
  • @BrianDanielPemberton see the edit. It has been approved now. – Saksham Aug 17 '13 at 09:15

1 Answers1

1

if you are very sure about the delimiter and the count, then you may use

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    fstream fs;
    fs.open("abc.txt",ios::in);         //1
    string str;
    int counter = 0;
    vector<string> vecString;
    vector<double> vecDouble1;
    vector<double> vecDouble2;
    while(getline(fs, str))             //2
    {
        char *sArr = new char[str.length()+1];          //3
        strcpy(sArr, str.c_str());
        char *pch = strtok(sArr,",");                   //4
        vecString.push_back(pch);                       //5
        pch = strtok(NULL,",");
        vecDouble1.push_back(atof(pch));                //6
        pch = strtok(NULL,",");
        vecDouble2.push_back(atof(pch));
        delete[] sArr;                                  //7
    }
    cin.ignore();
}

I would advise you to use arrays over vectors.

Explanation on demand:

  1. Opens a text file in input mode via stream

  2. Gets text from file line by line.

  3. This intermediate char array is used as it is unsafe to cast a const to non `const.

  4. Tokenizes the input line based on the delimiters(a singlecomma in your case). Refer THIS for tutorial.

  5. Adds the element to vector at the end.

  6. atof() is used to convert string to double.

  7. Free out the intermediate character pointer.

Saksham
  • 9,037
  • 7
  • 45
  • 73
  • Thank you for you input also! Would you mind walking me trough what this code is doing so I understand what is happening? – pemby Aug 17 '13 at 04:26
  • The `fs.getline()` call in the while-expression is reading up to 100 chars of data into a string that has no allocated space at all. Therefore, #2 is a segfault waiting to happen. Likewise, `const_cast<>` is only safe if the origin of the datum passed was also non-const, which it isn't in this case. (see [this question](http://stackoverflow.com/questions/357600/is-const-cast-safe)). In short, don't do it this way. – WhozCraig Aug 17 '13 at 07:31
  • @WhozCraig I welcome your suggestion but a downvote. cmon man – Saksham Aug 17 '13 at 07:41
  • Fix the code and I'll gladly not only remove it, I'll upvote it. The exiting code will more-than-likely fault on the first `getline()`. Why not use the optional delimiter parameter of `getline()` for some help? And perhaps the natural stream extraction operators for conversions of the doubles. – WhozCraig Aug 17 '13 at 07:42
  • @WhozCraig I hope it is fine now. Any other suggestions?? – Saksham Aug 17 '13 at 08:35
  • Yeah. I'll lift the down vote first now that the seg-fault has been lifted. I would do this entire thing differently, but for your specific code, I would use a `std::vector sArr(str.length()+1);` and probably the std lib's `std::copy()` instead of `strcpy()`. Why the vector ? [See RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). There is little reason for `new` and `delete` in most modern C++ programs. Thanks for fixing the seg-fault. – WhozCraig Aug 17 '13 at 08:41
  • @WhozCraig Thanks for making me LEARN – Saksham Aug 17 '13 at 09:14