-3
while(!Info.eof()) {
    std::getline(Info, line,'\r');

    char a[line.length()];
    char things[]= ":.\n\0";

    for(int i=0;i<sizeof(a); i++) {
       a[i]= line[i];
    }


    ptr = strtok(a, things);
    ptr = strtok(nullptr,things);


   while (ptr!= nullptr) {
        ptr = strtok(nullptr,things);
        std::cout << ptr << std::endl;
   }

Info is the ifstream input file. line is a string. When I cout << line it displays everything no problem, the problem is i need to take away everything other than the needed strings and int, which I have done but the first 2 lines don't show. When I first executed this it displayed everything, yesterday it skipped the first line and today the first two lines. I guess this has something to do with memory or something unseen, I need help please, thanks.

moey
  • 11
  • 4
  • 1
    A lot of errors in the code. Look for .eof() in a loop and VLA. – 273K Aug 25 '21 at 15:42
  • 1
    For example, [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125) and [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097) – user4581301 Aug 25 '21 at 15:54
  • what should i do i am kind of lost – moey Aug 25 '21 at 16:07
  • 2
    If you are lost, abandon the character array and use `std::string`. See `std::string::find()` and `std::string::substr()`. – Thomas Matthews Aug 25 '21 at 16:46

2 Answers2

0

One thing you're missing -- C-style strings are terminated with a zero on the end. You're not doing that.

Secondly, you did two strtoks before your while loop, which is why you're losing a few things.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • sorry but im a beginner, i took one strtok away and its only that the 1st line is missing. i know that a char holds /0 but dont know how to add it or take it away, i tried increasing the array length by 1 and 2 so it can show but it was of no use. – moey Aug 25 '21 at 15:53
0

Well, for starters, you are calling strtok() 3 times before your 1st cout print. So you are skipping the first few substrings.

Also, you have mistakes in your code, namely using eof() in a loop, and using non-standard variant-length arrays.

Try something more like this instead:

while (std::getline(Info, line))
{
    const char *things = ":.";

    ptr = strtok(line.data()/* or: &line[0]*/, things);
    while (ptr)
    {
        std::cout << ptr << std::endl;
        ptr = strtok(nullptr, things);
    }

    ...
}

Or, as a for loop:

while (std::getline(Info, line))
{
    const char *things = ":.";

    for(ptr = strtok(line.data()/* or: &line[0]*/, things);
        ptr != nullptr;
        ptr = strtok(nullptr, things))
    {
        std::cout << ptr << std::endl;
    }

    ...
}

Although, you really shouldn't be using strtok() in C++ at all. std::string has its own find_first_of() and substr() methods that you can use instead, eg:

while (std::getline(Info, line))
{
    std::string::size_type start = 0, end;
    while (start < line.size())
    {
        end = line.find_first_of(":.", start);
        if (end == std::string::npos)
        {
            std::cout << line.substr(start) << std::endl;
            break;
        }
        std::cout << line.substr(start, end-start) << std::endl;
        start = end + 1;
    }

    ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • lifesaver thanks, its more understandable to use the substr() – moey Aug 25 '21 at 18:21
  • how can i extract the info though to put them in struct parameters? – moey Aug 25 '21 at 18:22
  • An array/vector of strings would make more sense than individual struct members. But if you really needed individual members, you would have to unroll the loop and just call `strtok()`/`substr()` for each struct member individually. Or at least store the substrings into an array/vector first and then read its entries into the struct members individually as needed. You have not provided any context for your question, so it is difficult to tell you which is the best approach for your situation. – Remy Lebeau Aug 25 '21 at 18:30
  • the requirement is that we have 3 files that need the ifstream for extracting the input. we need to use randomness for the 3 files and theres a bit of calculation after the randomness. After initializing the 3 files we need to put what is in the files into variables to be able to do the randomness and calculation after the randomness – moey Aug 25 '21 at 19:23
  • Don't you think that would have been good information to present up front? Especially what the file data actually looks like, and how it needs to be used? – Remy Lebeau Aug 25 '21 at 19:24
  • i didnt want to bother you too much, i thought if i got that working i could do the rest, sorry – moey Aug 25 '21 at 19:26
  • the file is a .txt file, we need to use ifstream to extract the info of the content of the .txt file. it looks like this: ( B:word:1:2:3. ) and has 5 lines of that bellow it. the 3 files look like that and we need to extract the information of the 3 files then do some type of randomness with them. After calculate stuff and upload it to an output file – moey Aug 25 '21 at 19:29
  • So, what is the actual problem exactly? I suggest you update your question with more detail (or better, post a new question, since this question is answered as asked). – Remy Lebeau Aug 25 '21 at 19:37
  • is there somewhere i can message you privately because it doesnt let me post a new question, and its better like you said to keep the original questiion answered – moey Aug 25 '21 at 19:52
  • "*is there somewhere i can message you privately*" - StackOverflow has a [chat system](https://chat.stackoverflow.com/). "*it doesnt let me post a new question*" - it should, unless you [reached a posting limit](https://stackoverflow.com/help/question-bans) (which you should not have reached yet, as this appears to be your 1st question). – Remy Lebeau Aug 25 '21 at 20:14
  • i cant make a chat with you, since my reputation is less than 100 – moey Aug 25 '21 at 20:35