标签: fstream

"seekp"和"seekg"是否可以互换?

好吧,我只是注意到通过改变位置-in microsoft visual studio-通过"seekp"我隐式地也改变了处理文件时的读取位置.

我想知道这是否是"便携式"行为?我能指望阅读和写作的位置始终如一吗?因此:将tellp&tellg总是返回相同的值?

c++ fstream position binaryfiles

24
推荐指数
2
解决办法
5803
查看次数

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

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

c++ io file-io fstream ifstream

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

从文本文件中读取行并将字符串放入向量中?

我试图读取文本文件的每一行,每行包含一个单词并将这些单词放入向量中.我该怎么做呢?

这是我的新代码:我认为它仍有问题.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    std::string line;
    vector<string> DataArray;
    vector<string> QueryArray;
    ifstream myfile("OHenry.txt");
    ifstream qfile("queries.txt");

    if(!myfile) //Always test the file open.
    {
        cout<<"Error opening output file"<<endl;
        system("pause");
        return -1;
    }
    while (std::getline(qfile, line))
    {
        QueryArray.push_back(line);
    }
    if(!qfile) //Always test the file open.
    {
        cout<<"Error opening output file"<<endl;
        system("pause");
        return -1;
    }

    while (std::getline(qfile, line))
    {
        QueryArray.push_back(line);
    }

    cout<<QueryArray[0]<<endl;
    cout<<DataArray[0]<<endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ fstream

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

是否可以将cout或fout传递给函数?

我正试图找到一种方法将fout或cout传递给函数.我意识到有一些逻辑上简单的方法可以解决这个问题,例如将ifs放在任何输出数据的函数中,或者甚至只是双向编写函数.然而,这似乎是原始的和低效的.我不相信这段代码会起作用,我把它放在这里是为了确保我很容易看到我喜欢做什么.请注意,我正在使用c ++进行算法设计课程,我绝不是一个经验丰富的c ++程序员.我的课程仅限于使用您看到的标题.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
void helloWorld(char);
ofstream fout;

int main()
{
    fout.open("coutfout.dat");
    helloWorld(c);
    helloWorld(f);

    return 0;
}
void helloWorld(char x)
{
    xout << "Hello World";
    return;
}
Run Code Online (Sandbox Code Playgroud)

c++ fstream ofstream

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

将数据从fstream复制到stringstream而没有缓冲区?

无论如何我可以将数据从fstream(一个文件)传输到stringstream(内存中的流)吗?

目前,我正在使用缓冲区,但这需要双倍的内存,因为您需要将数据复制到缓冲区,然后将缓冲区复制到字符串流,直到您删除缓冲区,数据在内存中重复.

std::fstream fWrite(fName,std::ios::binary | std::ios::in | std::ios::out);  
    fWrite.seekg(0,std::ios::end); //Seek to the end  
    int fLen = fWrite.tellg(); //Get length of file  
    fWrite.seekg(0,std::ios::beg); //Seek back to beginning  
    char* fileBuffer = new char[fLen];  
    fWrite.read(fileBuffer,fLen);  
    Write(fileBuffer,fLen); //This writes the buffer to the stringstream  
    delete fileBuffer;`
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何在不使用inbetween缓冲区的情况下将整个文件写入字符串流?

c++ buffer fstream stl stringstream

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

在C++中有没有办法转到文本文件中的特定行?

如果我使用fstream打开文本文件有一种简单的方法可以跳转到特定的行,例如第8行?

c++ fstream

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

fstream不会创建文件

我只是想创建一个文本文件,如果它不存在,我似乎fstream无法做到这一点.

#include <fstream>
using std::fstream;

int main(int argc, char *argv[]) {
    fstream file;
    file.open("test.txt");
    file << "test";
    file.close();
}
Run Code Online (Sandbox Code Playgroud)

我是否需要在open()函数中指定任何内容才能使其创建文件?我已经读过您无法指定ios::in,因为预期已存在的文件存在,但我不确定是否需要为尚不存在的文件指定其他参数.

c++ file-io fstream

21
推荐指数
2
解决办法
9万
查看次数

fstream seekg(),seekp()和write()

我正在寻找关于你何时写文件的方式seekg()seekp()工作的一些说明.比方说我有一个像这样的文件:

offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
Run Code Online (Sandbox Code Playgroud)

现在我想打开文件并尝试读取和写入值.

fstream file;
file.open("file.txt", fstream::in |fstream::out | fstream::binary);
file.seekp(0, ios::end) // seek to the end of the file
int eofOffset = file.tellp(); // store the offset of the end-of-file, in this case 20

int key = 0;

file.seekg(12, ios::beg); // set the seek cursor to offset 12 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in …
Run Code Online (Sandbox Code Playgroud)

c++ file-io fstream seekg

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

为什么 GCC 的 ifstream &gt;&gt; 双分配这么多内存?

我需要从一个以空格分隔的人类可读文件中读取一系列数字并进行一些数学运算,但是我在读取文件时遇到了一些真正奇怪的内存行为。

如果我阅读数字并立即丢弃它们......

#include <fstream>

int main(int, char**) {
    std::ifstream ww15mgh("ww15mgh.grd");
    double value;
    while (ww15mgh >> value);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的程序根据 valgrind 分配了 59MB 的内存,相对于文件的大小线性缩放:

$ g++ stackoverflow.cpp
$ valgrind --tool=memcheck --leak-check=yes ./a.out 2>&1 | grep total
==523661==   total heap usage: 1,038,970 allocs, 1,038,970 frees, 59,302,487 
Run Code Online (Sandbox Code Playgroud)

但是,如果我ifstream >> string改为使用然后使用sscanf来解析字符串,我的内存使用情况看起来更加正常:

#include <fstream>
#include <string>
#include <cstdio>

int main(int, char**) {
    std::ifstream ww15mgh("ww15mgh.grd");
    double value;
    std::string text;
    while (ww15mgh >> text)
        std::sscanf(text.c_str(), "%lf", &value);
    return …
Run Code Online (Sandbox Code Playgroud)

c++ memory fstream libstdc++

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

试图引用已删除的功能

我正在尝试了解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万
查看次数