我做了一个示例项目,将文件读入缓冲区.当我使用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) 谷歌搜索文件输入我发现了两种从文件输入文本的方法--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++的新手,想要为我的代码添加错误检查,我想确保我使用良好的编码实践.我使用以下命令将ASCII文件中的一行读入字符串:
ifstream paramFile;
string tmp;
//open input file
tmp.clear();
paramFile >> tmp;
//parse tmp
Run Code Online (Sandbox Code Playgroud)
如何进行错误检查以确保输入文件读取成功?
我看到从那里读取ASCII文件的更复杂的方法.我这样做的方式是"安全/健壮"吗?
众所周知的创建fstream对象的方法是:
ifstream fobj("myfile.txt");
Run Code Online (Sandbox Code Playgroud)
即.使用文件名.
但我想使用文件描述符创建一个ifstream对象.
原因:我想使用执行命令
_popen()._popen()将输出返回为FILE*.所以有一个FILE*指针涉及但没有文件名.
我正在尝试了解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) 我有一个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++的新手,因为我来自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) 我希望用户给我文件存在的完整路径,而不仅仅是文件名.如何以这种方式打开文件?
它是这样的:
ifstream file;
file.open("C:/Demo.txt", ios::in);
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.
在我的C++代码中,我想从文本文件(*.txt)中读取并标记每个条目.更具体地说,我希望能够从文件中读取单个单词,例如"格式","堆栈","杰森","欧洲" 等.
我选择用来fstream执行这个任务,我不知道如何设置它的分隔符给我想要使用的(空格,\n以及连字符,甚至是"麦当劳"中的撇号).我想空间并且\n是默认分隔符,但连字符不是,但我想把它们当作分隔符,这样在解析文件时,我会把"blah blah xxx animal - cat"中的单词简单地称为"blah"," blah","xxx","animal","cat".
也就是说,我希望能够从"堆栈溢出","你是" 等获得两个字符串,并且仍然能够同时维护\n和空格作为分隔符.