1

I have a *.txt files having integers, one on each line. So the file would look something like

103123
324
4235345
23423
235346
2343455
234
2
2432

I am trying to read these values from a file line by line so I can put them in an array. Below is some code I wrote to achieve that

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int nArray[1000];
int i = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream file("C:\Users\Chinmay\Documents\Array.txt");
    //fstream file("C:\Users\Chinmay\Documents\Array.txt", ios_base::out );
    //fstream file();
    //file.open("C:\Users\Chinmay\Documents\Array.txt", ios_base::out );

        bool b = file.is_open();
    //file.seekg (0, ios::beg);
    int i = file.tellg();
    while(!file.eof())
    {
        //string str;
        //getline(file, str);
                //nArray[i++] = atoi(str.c_str());
        char str[7] = {};
        file.getline(str,7);
        nArray[i++] = atoi(str);
    }
    file.close();
    return 0;
}

The file opens as the bool 'b' returns true. But the while loop exits in one run. and the array is empty. I looked up online and tried other things like the code given here at

code tutorial

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int nArray[100000];
int i = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream in("C:\Users\Chinmay\Documents\Array.txt");
    bool b = in.is_open();

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[255];

  while(in) {
    in.getline(str, 255);  // delim defaults to '\n'
    if(in) cout << str << endl;
  }

  in.close();

  return 0;

}

This returns immediately as well. The file opens but no data is read. The file is not empty and has the data in it. Could someone explain where I am going wrong? I am using Visual Studio 2011 beta.

Chinmay Nerurkar
  • 495
  • 6
  • 22
  • 2
    Why don't you use `int tmp; cin >> tmp` and store the result in a `std::vector` or `std::list`? – Niklas B. Mar 27 '12 at 00:45
  • 2
    `std::vector` is your friend, indeed. `int nArray[100000];` could be very wasteful memory-wise. – Alex Z Mar 27 '12 at 00:49
  • I agree with using std::vector instead of allocating the array on the stack. This was just a first draft and I was just trying to get the 'reading from file' part right. I will also try with what Niklas B. suggested. Still curious though why the fstream and fstream.getline() didn't work. Thanks. – Chinmay Nerurkar Mar 27 '12 at 01:07
  • @NiklasB. Should be `file >> tmp` right? – Drew Galbraith Mar 27 '12 at 01:33
  • Ah.. vector.. uggh.. this is obviously homework.. so manage your own memory.. – baash05 Mar 27 '12 at 01:38
  • @Drew: Right, of course. – Niklas B. Mar 27 '12 at 01:46
  • Better still, `std::vector numbers; std::copy(std::istream_iterator(ifs), std::istream_iterator(), std::back_inserter(numbers));`. – johnsyweb Mar 27 '12 at 06:12

3 Answers3

2

This isn't doing what you think it's doing:

ifstream file("C:\Users\Chinmay\Documents\Array.txt");

Use forward slashes (even on Windows) and check the file open succeeded immediately:

std::ifstream ifs("C:/Users/Chinmay/Documents/Array.txt");
if (!ifs) 
{
    // Failed to open file. Handle this here.
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

I don't see anything much wrong with the second version.

However, in the first version, you're calling file.getline(str,7); where the line sometimes contains a 7-digit number. This reads until the delimiter (default '\n') is hit, or until 6 characters have been read, in which case the failbit is set.

Because you're only testing for eof in the while loop, it doesn't exit.

If you change the 7 to an 8 in the getline call and the char array declaration in the line above, it should work.

All that being said, @Niklas B's suggestion of using int tmp; file >> tmp; and storing in a vector would probably be the simplest solution.

Fraser
  • 74,704
  • 20
  • 238
  • 215
-1

This is a nice bit of code http://www.java2s.com/Code/Cpp/File/readingatextfile.htm
If this works on your file, then simply add your assignment

nArray[i++] = atoi(line); after the cout.


If it still works.. then comment out the cout.. Might be good to leave it in there commented out, as it might show your teacher your process. Some profs just want to see the finished product, so that's up to you

baash05
  • 4,394
  • 11
  • 59
  • 97