0

so I have a file that's structured like this

01
,
10
11
00
,
00
;

each line can only hold 2 0s or 1s, and there can be any amount of lines over 0 in between the commas and I need to count every line until there's a comma and append that value to an array then set the counter to 0. if the function finds a ; it saves that value then breaks the loop

currently I'm stuck on getting it to count up correctly, maybe I'm just not understanding how getline works

#include <iostream>
#include <fstream>
#include <string>



int main()
{
  std::string line[4];
  std::ifstream infile("in.txt");
  std::ofstream outfile("inData.txt");


  int x;
  while(!infile.eof())
  {
    getline(infile,line[0],'\n'); // tried with a , for \n and that correctly increases x for every comma
    ++x;
    std::cout << "x: " << x << "\n"; 


    // if(getline(infile, line[0],';')) {break;}
    // need to break if ; is found but from what I've seen it just makes it iterate once then break when I was using a , in the first getline
  }
  std::cin.get();
}
  • 1
    `int x;` is not initialized, so `++x;` invokes *undefined behavior* – UnholySheep May 23 '22 at 18:55
  • 1
    Did you try stepping through the code with a debugger to see what exactly happens during reading the input? Unrelated to this error but relevant: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/7691729) – Quimby May 23 '22 at 18:59
  • Why is `line` an array? For detecting `,` and `;` can't you just use `line[0] == ","` and `line[0] == ";"`? – Alan Birtles May 23 '22 at 19:24

1 Answers1

1

To explain you simply how the getfile function works :

  • It takes as first parameter the file (or more generally any object of type input stream) to be processed, as second parameter the object where the result will be obtained (here a string). The third parameter allows to define the end-of-line delimiter, which is by default the \n character.
  • Then, this function will "read" the first line of your file, up to the delimiter, then copy the result in the string.
  • Each time you call this function again, it will "read" the next line, until the end of the file.
  • When you reach the end of the file, the eof() function will return true, which will stop your while loop.

This is how the file reading works.

Now to the problems with your program:

  • First, why is line an array? Actually, you only use the first value of this array.
  • Secondly, you are using the ++x statement. This statement allows you to increment (add 1) the value of x. Now, at the beginning, x has no value. So, how do you want to increment it if it has no value. You have to put a value to it.
  • Then you need to know if you encounter any special characters, which are , and ;. You can simply check the value of the line (which is in the line variable), to see if it matches any of these values.

I'll leave you a working draft of the code, don't hesitate if you have other questions!

    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    
    int main()
    {
        std::string line;
        std::ifstream infile("in.txt");
        std::ofstream outfile("inData.txt");
    
        int x = 0; //You need to set a default value because when you do ++x, you increment the 
        //value of x, but if x has no value, you can't increment it
    
        while (!infile.eof())
        {
            getline(infile, line); // It retrieves the next line of the file
    
            //Here you can make different tests for the particular values you are looking for
            if (line == ";")
                break;
            if (line == ",")
            {
                //do what you need when you find a comma
                ;
            }
            ++x;
            std::cout << "x: " << x << "\n";
        }
        std::cin.get();
    }
Lucas
  • 11
  • 4
  • `When you reach the end of the file, the eof() function will return false,` I hope you meant that it will return `true` – Alexey May 23 '22 at 19:44
  • Small mistake from me, thank you! – Lucas May 23 '22 at 19:46
  • I think when I was trying to initialize line it didn't work because of some other weird code I had and making it an array fixed it, I guess I just didn't change it when I removed that –  May 23 '22 at 20:14
  • ok so I tried this code and it incrementing for each line worked, but it doesn't do anything when there's a comma. maybe there's some issue with the file but I couldn't find anything, do you know if its an issue with the code or the file and what should I do to set x to 0 then just print something to see if its working for now –  May 23 '22 at 21:01
  • It is normal that nothing happens when there is a comma. Try to understand the code. Look at the comments to help you. – Lucas May 23 '22 at 21:21