简体   繁体   English

使用fstream从C ++中的* .txt文件读取数字

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

I have a *.txt files having integers, one on each line. 我有一个* .txt文件,其中包含整数,每行一个。 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. 当bool'b'返回true时,文件打开。 But the while loop exits in one run. 但是while循环一次运行就退出了。 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. 我正在使用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: 使用正斜杠(即使在Windows上也是如此)并检查文件打开是否成功:

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); 但是,在第一个版本中,您正在调用file.getline(str,7); where the line sometimes contains a 7-digit number. 该行有时包含一个7位数字。 This reads until the delimiter (default '\\n' ) is hit, or until 6 characters have been read, in which case the failbit is set. 读取该内容,直到达到定界符(默认为'\\n' )为止,或者直到读取了6个字符为止,在这种情况下,将设置failbit

Because you're only testing for eof in the while loop, it doesn't exit. 因为您只在while循环中测试eof ,所以它不会退出。

If you change the 7 to an 8 in the getline call and the char array declaration in the line above, it should work. 如果在getline调用中将7更改为8,并在上一行将char数组声明更改为8,则它应该可以工作。

All that being said, @Niklas B's suggestion of using int tmp; file >> tmp; 话虽如此,@ Niklas B关于使用int tmp; file >> tmp;的建议int tmp; file >> tmp; int tmp; file >> tmp; and storing in a vector would probably be the simplest solution. 并将其存储在vector可能是最简单的解决方案。

This is a nice bit of code http://www.java2s.com/Code/Cpp/File/readingatextfile.htm 这是一段不错的代码http://www.java2s.com/Code/Cpp/File/readingatextfile.htm
If this works on your file, then simply add your assignment 如果这对您的文件有效,则只需添加您的作业

nArray[i++] = atoi(line); 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. 如果它仍然有效..然后注释掉cout ..最好将其留在注释中,因为它可能会告诉您的老师您的过程。 Some profs just want to see the finished product, so that's up to you 有些教授只想看成品,这取决于您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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