标签: ifstream

如何在C++中计算文件的行数?

我怎么能算使用标准班线,fstream以及ifstream

c++ io file-io fstream ifstream

23
推荐指数
3
解决办法
8万
查看次数

tellg()函数给出错误的文件大小?

我做了一个示例项目,将文件读入缓冲区.当我使用tellg()函数时,它给我一个比读取函数实际读取的值更大的值.我认为有一个错误.

这是我的代码:

编辑:

void read_file (const char* name, int *size , char*& buffer)
{
  ifstream file;

  file.open(name,ios::in|ios::binary);
  *size = 0;
  if (file.is_open())
  {
    // get length of file
    file.seekg(0,std::ios_base::end);
    int length = *size = file.tellg();
    file.seekg(0,std::ios_base::beg);

    // allocate buffer in size of file
    buffer = new char[length];

    // read
    file.read(buffer,length);
    cout << file.gcount() << endl;
   }
   file.close();
}
Run Code Online (Sandbox Code Playgroud)

主要:

void main()
{
  int size = 0;
  char* buffer = NULL;
  read_file("File.txt",&size,buffer);

  for (int i = 0; i < size; i++) …
Run Code Online (Sandbox Code Playgroud)

c++ file ifstream

23
推荐指数
2
解决办法
3万
查看次数

阅读文本文件 - fopen与ifstream

谷歌搜索文件输入我发现了两种从文件输入文本的方法--fopen和ifstream.以下是两个片段.我有一个文本文件,包含一行我需要读入的整数.我应该使用fopen还是ifstream?

SNIPPET 1 - FOPEN

FILE * pFile = fopen ("myfile.txt" , "r");
char mystring [100];
if (pFile == NULL) 
{
    perror ("Error opening file");
}
else 
{
    fgets (mystring , 100 , pFile);
    puts (mystring);
    fclose (pFile);
}
Run Code Online (Sandbox Code Playgroud)

SNIPPET 2 - IFSTREAM

string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
    while ( myfile.good() )
    {
        getline (myfile,line);
        cout << line << endl;
    }
    myfile.close();
}
else 
{  
    cout << "Unable to open file"; 
}
Run Code Online (Sandbox Code Playgroud)

c++ fopen file ifstream

22
推荐指数
2
解决办法
6万
查看次数

C++ ifstream错误检查

我是C++的新手,想要为我的代码添加错误检查,我想确保我使用良好的编码实践.我使用以下命令将ASCII文件中的一行读入字符串:

ifstream paramFile;
string tmp;

//open input file

tmp.clear();

paramFile >> tmp;

//parse tmp
Run Code Online (Sandbox Code Playgroud)
  1. 如何进行错误检查以确保输入文件读取成功?

  2. 我看到从那里读取ASCII文件的更复杂的方法.我这样做的方式是"安全/健壮"吗?

c++ error-handling ifstream

21
推荐指数
1
解决办法
3万
查看次数

从FILE*指针创建fstream对象

众所周知的创建fstream对象的方法是:

ifstream fobj("myfile.txt");
Run Code Online (Sandbox Code Playgroud)

即.使用文件名.

但我想使用文件描述符创建一个ifstream对象.

原因:我想使用执行命令_popen(). _popen()将输出返回为FILE*.所以有一个FILE*指针涉及但没有文件名.

c++ popen ifstream

20
推荐指数
1
解决办法
1万
查看次数

试图引用已删除的功能

我正在尝试了解fstream类,我遇到了一些麻烦.我创建了几个txt文件,一个带有笑话,另一个带有一个妙语(joke.txt)和(punchline.txt),只是为了阅读和显示内容.我向用户询问文件名,如果发现它应该打开它,清除标志然后读取内容.但我甚至无法测试它读取的内容因为我目前收到有关已删除功能的错误但我不知道我知道这意味着什么

错误1:

"IntelliSense: function "std::basic_ifstream<_Elem, _Traits>::basic_ifstream(const std::basic_ifstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 818 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function
Run Code Online (Sandbox Code Playgroud)

第二个错误是完全相同但是对于第二个函数(displayLastLine())

和错误3:

Error   1   error C2280: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' : attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#include "stdafx.h" 
#include <string>    
#include <iostream>  
#include <fstream>   

using namespace std; 

void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);

 int main()          
{
    string fileName1, fileName2;
    ifstream jokeFile, punchlineFile;


    // Desribe the assigned project …
Run Code Online (Sandbox Code Playgroud)

c++ string fstream function ifstream

20
推荐指数
1
解决办法
7万
查看次数

C++,可以采用 ifstream 或 istringstream 的函数

我有一个do_something从流中读取无符号字符的函数。

可以从给定文件名的文件创建流。或者它可以通过将给定的字符串视为数据来创建。我想在这两种情况下重用该功能。

下面的代码在第二种情况下给出了一个错误:“错误 C2664: 'do_something: 无法将参数 1 从 'std::basic_istringstream' 转换为 'std::basic_istream'”

这样做的正确方法是什么?

static void do_something(std::basic_istream<unsigned char>& in)
{
   in.get();
}

static void string_read(unsigned char* in)
{
   std::basic_ifstream<unsigned char> file(std::string("filename"));
   do_something(file);

   std::basic_istringstream<unsigned char> str(std::basic_string<unsigned char>(in));
   do_something(str);
}
Run Code Online (Sandbox Code Playgroud)

c++ ifstream istringstream

19
推荐指数
1
解决办法
434
查看次数

使用ifstream检查文件是否成功打开

我有以下将打开文件进行阅读.但是,我想检查以确保文件已成功打开,因此我使用失败来查看是否已设置标志.但是,我不断收到以下错误:

我是C++的新手,因为我来自C.所以我不确定我是否理解这个错误:

不能调用成员函数'bool std :: basic_ios <_CharT,_Traits> :: fail()const [with _CharT = char,_Traits = std :: char_traits]'没有对象

码:

int devices::open_file(std::string _file_name)
{
    ifstream input_stream;

    input_stream.open(_file_name.c_str(), ios::in);

    if(ios::fail() == true) {
        return -1;
    }

    file_name = _file_name;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ io ifstream

18
推荐指数
3
解决办法
4万
查看次数

在C++中以完整路径打开文件

我希望用户给我文件存在的完整路径,而不仅仅是文件名.如何以这种方式打开文件?

它是这样的:

ifstream file;
file.open("C:/Demo.txt", ios::in);
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.

c++ file path ifstream

17
推荐指数
2
解决办法
16万
查看次数

在使用std :: fstream读取文本文件时,如何使用非默认分隔符?

在我的C++代码中,我想从文本文件(*.txt)中读取并标记每个条目.更具体地说,我希望能够从文件中读取单个单词,例如"格式","堆栈","杰森","欧洲" .

我选择用来fstream执行这个任务,我不知道如何设置它的分隔符给我想要使用的(空格,\n以及连字符,甚至是"麦当劳"中的撇号).我想空间并且\n是默认分隔符,但连字符不是,但我想把它们当作分隔符,这样在解析文件时,我会把"blah blah xxx animal - cat"中的单词简单地称为"blah"," blah","xxx","animal","cat".

也就是说,我希望能够从"堆栈溢出","你是" 获得两个字符串,并且仍然能够同时维护\n和空格作为分隔符.

c++ fstream ifstream

17
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×10

ifstream ×10

file ×3

fstream ×3

io ×2

error-handling ×1

file-io ×1

fopen ×1

function ×1

istringstream ×1

path ×1

popen ×1

string ×1