简体   繁体   中英

Having reading numbers from a *.txt file in C++ using fstream

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.

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.
}

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; int tmp; file >> tmp; and storing in a vector would probably be the simplest solution.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM