-2

This is sort of a weird question but I wrote a code that reads from a text file full of numbers as strings and converts them into integers. The file contains 400 numbers and the program reads only 392.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
    int main() {
    string line;
    ifstream read;
        int a[20][20];
    vector<int>v;
        int m;
        string y;
        string  lines [20] ;
        read.open("/Users/botta633/Desktop/Untitled.txt",ios_base::binary);
       while (read>>line){
            y="";
            for (int i=0; i<line.length(); i++) {
                y+=line[i];
            }
                m=stoi(y);
                v.push_back(m);
            }

       for (int i=0; i<20; i++) {
            for (int j=0; j<20; j++) {
                int k=i*20+j;
                a[i][j]=v[k];
            }
        }
        for (int i=0; i<20; i++) {
            for (int j=0; j<20; j++) {
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        cout<<v.size()<<endl;


    return 0;
}

when I tried to change the number of integers inside the file it did read just some of them as well.

the file looks like this

89 41  
92 36  
54 22  
40 40  
Student
  • 805
  • 1
  • 8
  • 11
ahmed botta
  • 7
  • 1
  • 6
  • 2
    Lot of stuff going on in there. I recommend scaling back and starting with only reading the file. If you can't read the file correctly, the rest of the program isn't worth that much, and it really sucks when to fix a bug you need to throw out and rewrite the other code. It's really not worth writing more than one thing at a time. – user4581301 Jul 22 '18 at 05:55
  • Why not int m; while ( read >> m ) v.push_back(m);? – zdf Jul 22 '18 at 06:13
  • when i read into an integer it doesn't read any thing – ahmed botta Jul 22 '18 at 06:19
  • It may be because you are using two digit numbers and you use line[i] to get a number. Try using single digits to check if that is the issue. – VishnuVS Jul 22 '18 at 06:25
  • Strange. Check the file was successfully opened: ifstream is(...); if (!is) return -1; Change the name of the input stream from read to is, for instance. – zdf Jul 22 '18 at 06:27
  • can u check the solution below and see the problem i am facing the solution owner faced the same problem while reading from this file – ahmed botta Jul 22 '18 at 06:31
  • maybe it's a mac problem – ahmed botta Jul 22 '18 at 06:31
  • Check the file encoding. – zdf Jul 22 '18 at 06:36
  • Use @username to notify the username about a comment. – zdf Jul 22 '18 at 07:12
  • 1
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Jul 22 '18 at 19:55

1 Answers1

1
  1. Your file is UTF-8 encoded. It contains non-breaking spaces (C2 A0) which are interpreted by ifstream as non-white-space and non-digits. Therefore, it stops reading. Save it with western encoding - the editor will replace the non-breaking space with space.
  2. This is how you read a file of integers:

// source
ifstream is("full_path"); // open the file
// OR, if wide characters
// wifstream is("full_path"); // open the file
if (!is) // check if opened
{
  cout << "could not open";
  return -1;
}

// destination
vector< int > v;

// read from source to destination - not the only way
int i;
while (is >> i) // read while successful
  v.push_back(i);

cout << v.size();
zdf
  • 4,382
  • 3
  • 18
  • 29