Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何查找输入文件的大小_C++_File_While Loop_Eof_Filesize - Fatal编程技术网

C++ 如何查找输入文件的大小

C++ 如何查找输入文件的大小,c++,file,while-loop,eof,filesize,C++,File,While Loop,Eof,Filesize,我编写了从输入文件(它只是一列1和0)获取数据的代码,并将其转换为任意多个32位数字。当我知道输入文件中有多少个数字时,这种方法就可以很好地工作。我正试图修改它,使它适用于不同的输入文件,我不知道大小,这就是我遇到问题的地方 我的输入文件是rad。 我试过: int x=0; while(!rad.eof()) { x++; } cout << x; intx=0; 而(!rad.eof()){ x++; } cout>x; cout x; cerr)操作员 无符号长数值=0; 而

我编写了从输入文件(它只是一列1和0)获取数据的代码,并将其转换为任意多个32位数字。当我知道输入文件中有多少个数字时,这种方法就可以很好地工作。我正试图修改它,使它适用于不同的输入文件,我不知道大小,这就是我遇到问题的地方 我的输入文件是rad。 我试过:

int x=0;
while(!rad.eof()) {
x++;
}
cout << x;
intx=0;
而(!rad.eof()){
x++;
}
cout>x;
cout x;
cerr)操作员
无符号长数值=0;

而(j请看一下手册:

它包含一个如何读取数据的示例。该示例也可用于计算数据

您可以这样重写它:

std::ifstream is("example.txt");   // open file

int cnt = 0;
char c;
while (is.get(c))                  // loop getting single characters
   cnt++;

std::cout << "Character count in file: " << cnt;

is.close();
std::ifstream是(“example.txt”);//打开文件
int-cnt=0;
字符c;
while(is.get(c))//循环获取单个字符
cnt++;

std::cout假设
rad
是您可以使用的


无需担心流中的值的数量。只需将它们复制到容器中即可。您可以检查容器的大小,但不一定需要

#include<iostream>
#include<fstream>
#include<iterator>
#include<vector> 

  // and in a function somewhere, assuming rad is a valid stream

std::vector<int> data;

std::copy(std::istream_iterator<int>(rad), 
          std::istream_iterator(), 
          std::back_inserter<std::vector<int> > (data));
#包括
#包括
#包括
#包括
//在某个函数中,假设rad是一个有效流
std::矢量数据;
std::copy(std::istream_迭代器(rad),
std::istream_迭代器(),
标准::反向插入器(数据);

上述假设你需要一次读取一个代码> int <代码>,并将它们添加到vector中。

因为你使用C++,为什么不使用一个STD::vector代替一个数组。这样你就不用担心文件中有多少记录了。我不知道如何使用STD::vector,我有一个基本的想法,就是如何使用数组。
close()
std::ifstream
析构函数上触发。摘自:
此函数[close()]当stream对象超出范围时,由basic_ifstream的析构函数调用,通常不会直接调用。
@Patryk我不知道,谢谢。手册中的示例出于某种原因明确使用了它。已经阅读了该页,它没有真正的帮助。它只返回0(或者很明显,无论我最初将cnt设置为相等,只要尝试cnt=5,它就会返回5@Hester文件的内容是什么?我假设您用文件名替换了
example.txt
?请尝试使用
is\u open()
以确定文件是否已正确加载。请参阅:是的,我正在使用is_open()将example.txt替换为我的文件名。我发现我的文件没有正确打开,这可能是什么都不起作用的原因。我使用名称空间std;before int main()所以我没有在所有操作之前使用std::(idk这正是我被教导要做的),在没有所有std::的情况下,我该如何做你说的事情?@Hester你可以在使用
名称空间std;
时简单地删除
std:
前缀。在复制内容中也可以?@Hester是的。每一个
std:
前缀都是不必要的。这就是
使用名称空间std;
的目的。而且,这也会给我一个机会包含输入文件中所有值的向量,对吗?然后如何使用向量?是否用它替换数组
/*const int m=32,n=40000; //n isnt 40000, change it so that its the size of    radioactivedata
    int a[n]; // variable to hold each number as it is read from file
    int i=0,j=0; //define variables
    for ( i=0 ; i<n ; i++ ) a[i]=0;
    for ( i=0 ; i<n ; i++ ); rad >> a[i]; //read numbers from rad into array with (>>) operator

    unsigned long Num=0;
    while(j<n){
        i = 0; //reinitialize i and Num 
        Num = 0;
        while ( i<m ){
            Num = Num + (a[j])*pow(2.,i);
            i++;
            j++;
            }

        cout << j/m << "\t" << Num << endl;
        out << j/m << "\t" << Num << endl;
 }*/
std::ifstream is("example.txt");   // open file

int cnt = 0;
char c;
while (is.get(c))                  // loop getting single characters
   cnt++;

std::cout << "Character count in file: " << cnt;

is.close();
  // std::ios_base::ate seeks to the end of the file on construction of ifstream
  std::ifstream rad ("file.txt", std::ios_base::ate);
  int length = 0;
  if (rad) {
    length = is.tellg();
    is.seekg (0, std::ios_base::beg); // reset it to beginning if you want to use it
  }
  // .. use length
#include<iostream>
#include<fstream>
#include<iterator>
#include<vector> 

  // and in a function somewhere, assuming rad is a valid stream

std::vector<int> data;

std::copy(std::istream_iterator<int>(rad), 
          std::istream_iterator(), 
          std::back_inserter<std::vector<int> > (data));