0
while(fin >> a)

or

while(fin.eof())

won't work. In first case, the fin stream variable keeps taking the last char in file as input.

My input file looks like this:

ab5c71,lj4j6v7;
sd9vg0,kj9ref9,fjuwe;

Here's my code:

[![int main()
    {
        int count = 0, col = 0, row = 0;
        ifstream fin;
        fin.open("Text.txt");
        char a;
        //count number of rows and cols
        while (fin >> a)
        {
            while (a != ',')//marks end of one word in a sentence
            {
                if (a >= '0'&&a <= '9')//sees if the input is a number
                {
                    count++;
                }
                if (a == ';')
                {
                    if (count > 0)
                    {
                        col++;
                    }
                    if (col > 0)
                    {
                        row++;
                        count = 0;
                        col = 0;
                    }
                }
                fin >> a;
            }
            if (count > 0)
            {
                col++;
            }
            count = 0;
        }
        cout << row << " " << col;
    return 0;
    }][1]][1]

Well it's not the complete code.the actual desired output f the code is to read the file and output a matrix containing all the digits, the digits that are in one word (i.e before a comma) are supposed to be one complete number and make up one col, each line ends with a semi colon which marks end of a row. It's possible that a word may not contain any digit, in which case no input is taken in matrix and it is also possible that an entire row/sentence is made up of such words in which case no row is added. The code I have written so far only calculates how many rows and cols are required. It is assumed that all rows will contain equal columns.

The desired output of the sample file attached would be a matrix=[57,467 ; 90,99] (the coma's and semi colons are not supposed to be a part of matrix, just added them to mark the end of one element(coma) and row(semi colon))

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388

1 Answers1

4

The problem here (almost certainly) isn't with your while (fin>>a), its with the fin >> a; inside the loop, which isn't testing the result, so if it's called at the end of the file, you continue inside the loop indefinitely.

iostreams aren't particularly helpful in doing what you're trying to do here. In particular, it would be really nice if we could ask a stream to read up to the next comma or semicolon (whichever came first) and tell us which one ended a particular read. That would let us grab the next "field", look at its contents, and react accordingly.

Lacking that, we have a few alternatives available. One is to read a complete "record" (everything up to a semicolon), the split that into fields, and look at each one:

std::string record;

while (std::getline(fin, record)) {
    std::istringstream buffer(record);
    std::string field;
    while (std::getline(buffer, field))
         if (std::any_of(field.begin(), field.end(), 
             [](unsigned char ch) { return isdigit(ch); })
         {
             ++col;
         }
    // when we get here, we've looked at all the fields of the current record.   
}
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
  • your method seems too long, getting a whole line and then breaking it to cheek. i did however try to put a check on the fin>>a inside the loop to check first if the file has reached the end, if not, only then takr the next input. i.e if(!fin.eof()) fin>>a; but it still goes in an infinite loop, i debudeg the prog to check and turns out the eof flag is never set and it always returns false eventhough the file has actually reached the end – Sara Iftikhar Sep 03 '16 at 10:47