我刚刚在这个答案中发现了一条评论说iostream::eof在循环条件下使用"几乎肯定是错误的".我通常使用类似的东西while(cin>>n)- 我猜是隐式检查EOF,为什么检查eof显式使用while (!cin.eof())错误?  
它与scanf("...",...)!=EOF在C中使用有何不同(我经常使用没有问题)?
我有一个大约11.1G的二进制文件,其中存储了Kinect的一系列深度帧.此文件中有19437个帧.要每次读取一帧,我在fstream中使用ifstream,但它在文件的真实结束之前达到eof.(我只得到了前20帧,并且由于eof标志,函数停止了)
然而,所有的帧可以通过读取FREAD在标准输入输出来代替.
谁能解释这种情况呢?感谢您在我的问题上花费宝贵的时间.
这是我的两个功能:
// ifstream.read() - Does Not Work: the loop will stop after 20th frame because of the eof flag
ifstream depthStream("fileName.dat");
if(depthStream.is_open())
{
  while(!depthStream.eof())
  {
    char* buffer = new char[640*480*2];
    depthStream.read(buffer, 640*480*2);
    // Store the buffer data in OpenCV Mat
    delete[] buffer;
  }
}
// fread() - Work: Get 19437 frames successfully
FILE* depthStream
depthStream = fopen("fileName.dat", "rb");
if(depthStream != NULL)
{
  while(!feof(depthStream))
  {
    char* …我有一个简单的问题......假设我想从标准输入中读取行,只要有东西,但我不知道会有多少行。例如我在做功课,输入是
a
ababa
bb
cc
ba
bb
ca
cb
我不知道到底会有多少行,所以我试过了
string *line = new string[100];
    int counter = 0;
   while(getline(cin,line[counter]))
   {
    counter++;
   }
但它不起作用...感谢您的帮助。
我在Windows XP上使用Dev C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    string STRING;
    ifstream infile;
    infile.open ("sample.txt");
        while(!infile.eof)
        {
            getline(infile,STRING); 
            cout<<STRING; 
        }
    infile.close();
    return 0;
}
此代码给出以下错误
C:\C++\read.cpp: In function `int main()':
C:\C++\read.cpp:11: error: could not convert `infile.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>]' to `bool'
C:\C++\read.cpp:11: error: in argument to unary !
我不知道这里有什么问题我无法编译代码请帮忙
好.所以我有这个函数,init():
void init()
{
fstream file;
int index = 0;
char temp_list[60000][15];
listlen = 0;
current_index = 0;
file.open("en_US.dic");
while(!file.eof())
{   
    file >> temp_list[index];
    index++;
}
listlen = index;
file.close();
file.open("en_US.dic");
word_list = new char*[listlen];
int count = 0;
for(int i = 0; i < listlen; i++)
{
    word_list[i] = new char[21];
    file >> word_list[i];
}
file.close();
}
此代码编译并正确运行,没有错误.但是,当我改变线
word_list[i] = new char[21]
至
word_list[i] = new char[x] //x < 21
我收到以下错误:
dict: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char …我写了一个程序来计算文本文件中的字母数字字符数.但是,它返回的数字总是大于在线字符计数器返回的数字.
例如,程序将计算此文本中的字母数字字符数:
如果这些人有奇怪的时尚和对最特别的事情的期望服从,他们至少准备为他们的怪癖付出代价
再次运行程序,它会说文本中有164个字符.再次运行,它会说有156个字符.使用这个在线字符计数器,似乎字符数应该低于144(在线字符计数器也包括空格).
这是代码:
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
    char line[100];
    int charcount = 0;
    ifstream file("pg1661sample.txt");
    while (!file.eof()) {
        file.getline(line, 99);
        for (int i = 0; i < 100; i++) {
            if (isalnum(line[i])) {
                charcount++;
            }
        }
    }
    cout << endl << "Alphanumeric character count: " << charcount;
    cin.get();
    return 0;
}
我究竟做错了什么?
我有一个名为scores.txt的文本文件,其中包含以下数据:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
我正在尝试从此文件中读取数据并使用以下C++代码进行打印:
ifstream input;
input.open("scores.txt");
string firstName;
char mi;
string lastName;
int score;
while (input) {
    input>>firstName>>mi>>lastName>>score;
    cout<<firstName<<" "<<mi<<" "<<lastName<<" "<<score<<endl;
}
input.close();
cout<<"Done"<<endl;
输出是:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Marie A Bell 50
为什么最后一行(Marie A Bell 50)被打印两次?我怎样才能防止这种情况发生?
我对文件结尾感到很困惑.假设我正在运行无限循环.在这个循环中,我将一个整数作为输入,然后处理它,直到我找到一个文件结束.但是,我是否检查输入是否为文件结尾.我该如何打破循环?
我使用Windows,所以对于EOF我输入CTRL + Z.
#include<iostream>
#include<cstdio>
using namespace std;
int main(void)
{
  int n;
  while(true)
  {
    cin >> n;
    if(n==EOF)break;
    cout << n << endl;
  }
  return 0;
}
当我运行此代码并输入CTRL + z时,它只打印我给出的最后一个输入,无休止地.
该代码应该计算输入文本文件中的a,b,c,d,e和f字符的数量,并将输出打印到第二个文本文件中.当我运行代码时,它会创建输出文件,但不会在其中写入任何内容.
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main(){
// establish counters for the number of each character
char x;
int acount=0;
int bcount=0;
int ccount=0;
int dcount=0;
int ecount=0;
int fcount=0;
 ifstream iFile("plato.txt"); //define & open files
 ofstream oFile("statistics.txt");
 if(!iFile){
  cout<<"The file could not be opened.";
  exit(1);
 }
 if(!oFile){
  cout<<"The file could not be opened.";
  exit(1);
 }
 iFile>>x;
 while(!iFile.eof()){
  if(x=='a'||x=='A'){
   acount++;
  }
  else if(x=='b'||x=='B'){
   bcount++;
  }
  else if(x=='c'||x=='C'){
   ccount++;
  }
  else if(x=='d'||x=='D'){
   dcount++;
  }
  else if(x=='d'||x=='D'){
   dcount++;
  }
  else …