标签: filestreams

如果我从不在打开的文件流上调用`close`会发生什么?

以下是相同案例的代码.

#include <iostream>
#include <fstream>

using namespace std;

int main () {
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    //myfile.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我取消注释该myfile.close()行,会有什么不同?

c++ file filestreams

20
推荐指数
3
解决办法
8854
查看次数

Python:打开文件而不创建锁

我正在尝试用Python创建一个脚本来备份一些文件.但是,这些文件可以随时重命名或删除.我不希望我的脚本通过锁定文件来阻止它; 在备份期间,该文件应该仍然可以随时删除.

我怎么能用Python做到这一点?而且,会发生什么?如果无法读取流,我的对象是否会变为空?

谢谢!我对Python有些新意.

python backup file-locking filestreams

4
推荐指数
2
解决办法
5081
查看次数

错误:'无效操作数'到二元运算符<<

我收到以下错误:
类型'char'的无效操作数和未解析的重载函数类型>'到二进制'运算符<<'

这是什么意思?

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

int main()
{
    ifstream inFile;
    inFile.open("records.txt");

    ofstream outFile;
    outFile.open("corrected.txt");

    while (inFile.good())
    {
        string num, temp;
        inFile >> num;
        outFile << temp.at(0)=num.at(9) << temp.at(1)=num.at(8) 
                << temp.at(2)=num.at(7) << temp.at(3)=num.at(6) 
                << temp.at(4)=num.at(5) << temp.at(5)=num.at(4) 
                << temp.at(6)=num.at(3) << temp.at(7)=num.at(2) 
                << temp.at(8)=num.at(1) << temp.at(9)=num.at(0) << endl; 
    // invalid operands of types 'char' and unresolved overloaded function type>'
    // to binary 'operator<<'
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序应该反转已反转的电话号码列表.

c++ compiler-errors filestreams

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

为什么TFileStream.write会生成错误的数据?德7 7

我试图创建一个函数/方法,它应该写入一个带有填充零的标题的文件

header := StringOfChar(char(0), 11*offsetSize);
Run Code Online (Sandbox Code Playgroud)

但是当我检查创建的文件时,它具有以下值(十六进制):

C026A200160000000100000006000000264C656B63650000B2000000B04D45005C1EA200306CA20000000000
Run Code Online (Sandbox Code Playgroud)

我希望输出文件应该包含

0000 0000 0000 0000 0000 0000
Run Code Online (Sandbox Code Playgroud)

然后我也尝试填充它,chr(1)结果是类似的:

C026A200160000000100000006000000264C656B63650000B2000000B04D45005C1EA200306CA20000000000
Run Code Online (Sandbox Code Playgroud)

(看起来一样).

当我在Delphi 7中调试时,我在用零填充时观察标题:

#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0
Run Code Online (Sandbox Code Playgroud)

当我填写它chr(1)然后它包含...

#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,为什么会发生这种情况?输出文件包含错误的数据我做错了什么?

unit Dictionary_io;

interface

uses
  Classes, Windows, SysUtils, Dialogs;

const
  offsetTableEntriesCount = 10;

type
  TCountType = dword;
  TOffsetType = dword;
  TTextType = ANSIString;

const
  testItemsCount = 1;

type
  Dictionary = class
  private
    fsInput, fsOutput: TFileStream;
    fileName: string;
    msOffsets: TMemoryStream;
    offsetSize: TOffsetType;
    ofsTableEntries: TCountType;
    lng: integer;
    itemsCount: byte;
    header: TTextType;
  public
    constructor Create;
    procedure dicRewrite; …
Run Code Online (Sandbox Code Playgroud)

delphi tfilestream filestreams

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