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

Problems with fstream reads ok but not write user input string

我在我的代码中找不到问题。 readFile 函数运行良好,但 writeFile 函数没有对文件进行任何更改:

#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 in/out 模式在我的代码中仅用于阅读。

我看到几个问题:

  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,字面意思是get count(上次get操作读取的字符数).您正在写 (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. 使用std::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());
    

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

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