fstream 读取正常但不能写入用户输入字符串的问题

安东

我在我的代码中找不到问题。readFilefunction 运行良好,但writeFilefunction 不会对文件进行任何更改:

#include <iostream>
#include <fstream>

using namespace std;
const int BUF_SIZE = 1024;

void readFile(fstream &file, char buffer[BUF_SIZE]);
void writeFile(fstream &file);

void readFile(fstream &file, char buffer[BUF_SIZE])
{
    int position;
    cout << "Please enter a position to read from the file some info" << endl;
    cin >> position;
    file.seekg(position, ios::beg);
    file.read((char*) buffer, BUF_SIZE); // <<<<<

    for(int i = 0; i < file.gcount(); i++){
        cout << buffer[i];
    }
}

void writeFile(fstream &file)
{
    char temp[100] = "HHHH";
    //cout << "Please enter some info to add to the file" << endl;
    file.write((char*) &temp, 100);
    for(int i = 0; i < file.gcount(); i++){
        cout << temp[i];
    }
}

int main(int argc, char const *argv[])
{
    char buffer[BUF_SIZE];

    if (argc != 2){
        cout << "Program usage: prog_name file_name";
        return 1;
    }

    fstream file(argv[1], ios::in | ios::out | ios::binary | ios::app);
    if (!file){
        cout << "File can not open or doesn't exist";
        return 1;
    }

    //Try to read & write some info from/to file in particular position

    readFile(file, buffer);
    writeFile(file);

    file.close();

    return 0;
}

当我创建一个新的时,ostream它运行良好,但我想了解为什么fstream输入/输出模式在我的代码中仅用于阅读。

布图

我看到几个问题:

  1. 写入问题背后的原因可能是因为您到达了文件的末尾(文件是否小于BUF_SIZE字节?)。这会设置 EOF 位,这会使任何写操作失败。您必须先清除该位(使用该std::fstream::clear方法):

    void readFile(fstream &file, char buffer[BUF_SIZE])
    {
        int position;
        cout << "Please enter a position to read from the file some info" << endl;
        cin >> position;
        file.seekg(position, ios::beg);
        file.read(buffer, BUF_SIZE);
    
        for(int i = 0; i < file.gcount(); i++){
            cout << buffer[i];
        }
    
        file.clear(); // clears EOF
    }
    
  2. 该行file.write((char*) &temp, 100);是错误的,因为您实际上是将一个点传递给temp变量,该变量也是一个指针,但它被演员表伪装了。这些都可以:file.write(temp, 100);file.write(&temp[0], 100);

  3. 打印写入的字符时,您使用的是std::fstream::gcount,字面意思是获取计数(上次获取操作中读取的字符)。你在写(put)而不是在读(get)。实际上,您实际上是在表明您愿意写入多少字节,因此请使用它:

    file.write(temp, 100);
    for(int i = 0; i < 100; i++){
        cout << temp[i];
    }
    
  4. 最后,您总是写入 100 个字符,可能包括缓冲区中的一些垃圾。正如我所看到的,您想让用户选择要写的内容(注释行),您可以改为:

    const size_t size = strlen(temp);
    file.write(temp, size);
    for(size_t i = 0; i < size; i++){
        cout << temp[i];
    }
    

此外,还有一些建议:

  1. 使用 astd::string读取用户输入,这样可以避免可能的缓冲区溢出(如果用户输入超过 100 个字符)。

    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // read the link bel
    string temp;
    getline(cin, temp); // need #include <string>
    file.write(temp.c_str(), temp.size());
    

    您可能希望阅读此答案以了解有关第一行的更多信息(基本上它避免了getline在使用后被跳过cin >> position)。

  2. 避免使用 for 循环打印用户输入。对于缓冲区和std::string选项,您只需cout << temp << endl;.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 fstream 读取 csv,仅使用 fstream

来自分类Dev

使用fstream读取文件

来自分类Dev

Fstream管理问题

来自分类Dev

在类中使用fstream的问题

来自分类Dev

使用FStream无法正常写入文件

来自分类Dev

使用fstream向文件写入问题

来自分类Dev

fstream不是逐字符读取而是整数

来自分类Dev

为什么std :: fstream不写入文件?

来自分类Dev

C ++ fstream写入文件非常慢

来自分类Dev

fstream 无法在树莓派上写入/打开文件

来自分类Dev

Visual Studio中的fstream指针问题

来自分类Dev

关于c ++中fstream的几个问题

来自分类Dev

为什么fstream :: open()需要C样式的字符串?

来自分类Dev

递增变量字符串和Multi fstream open C ++

来自分类Dev

将字符文字{输出到fstream C ++

来自分类Dev

(C ++)在fstream中切断字符

来自分类Dev

使用fstream在c ++中读取文件时显示额外的字符串

来自分类Dev

c ++使用fstream从二进制文件读取字符串

来自分类Dev

C ++使用fstream对象读取和写入文本文件时遇到问题

来自分类Dev

在继续之前如何等待cpp fstream完成写入?

来自分类Dev

在C ++中使用sstream和fstream写入文件名

来自分类Dev

如何使用fstream在.txt文件中写入不同的值

来自分类Dev

C ++我不能使用fstream变量保存到文件

来自分类Dev

如何在IOS中使用fstream读取XML文件

来自分类Dev

用fstream读取带有浮点数的向量

来自分类Dev

从std :: fstream读取浮点数将返回负零

来自分类Dev

如何使用fstream(C ++)从文件中读取特定行

来自分类Dev

如何在 C++ 中使用 fstream 读取 .txt 文件

来自分类Dev

使用fstream访问linux用户主目录