2016-11-25 1 views
-2

쉼표로 구분 된 CSV 파일에서 데이터를 읽습니다. 파일에 데이터가 없는지 확인하기 위해 파일을 읽은 후 오류가 있는지 확인하고 싶습니다.csv 파일에 데이터가 없는지 확인하는 방법은 무엇입니까?

const char * sample_data_file = "sample_data1.csv"; std :: ifstream 파일 (sample_data_file);

감사합니다.

+3

가능한 복제본 [C++에서 빈 파일 확인] (http://stackoverflow.com/questions/2390912/checking-for-anempty-file-in-c) – sashoalm

+0

나는 그것을 시도했으나하지 않았다. 작업. std :: ifstream 파일 (sample_data_file); \t if (file.peek() == std :: ifstream :: traits_type :: eof()) \t \t cout << "빈 파일"<< endl; – Steve

답변

-1

파일을 열 때 파일 크기를 확인 하시겠습니까? 파일이 비어있는 경우

// reading a text file 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 

    ifstream myfile ("C:/temp/sample1.csv"); 

    // this gives you the number of bytes in the file. 

    if (myfile.is_open()) 
    { 
     long begin, end; 

     begin = myfile.tellg(); 
     myfile.seekg (0, ios::end); 
     end = myfile.tellg(); 

     if(end-begin == 0) 
     { 
      cout << "file is empty \n"; 

     } 
     else 
     { 
     cout << "size: " << (end-begin) << " bytes." << endl; 
     } 

     myfile.close(); 

    } 

    else cout << "Unable to open file \n"; 

    return 0; 
} 
+0

어떻게하면됩니까? – Steve

0

stat에 대한 간단한 전화는 당신을 말할 것이다. 그것은 당신의 문제를 해결하기에 충분해야합니다.

관련 문제