标签: fstream

C++ exit(1) 导致 IO 丢失?

这是我的程序:

#include <fstream>

int main() {
    std::ofstream fout("data.dat", std::ios::binary);
    for (int isam=0; isam<500; isam++) fout.write((char*) &isam, 4);
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

当我在 MacBook 上运行此程序时,文件 data.dat 的长度为 1 字节(在 AWS 上为 0 字节),而不是 2000。如果我增加 500,我最终会得到更多字节,但永远不会达到预期的数量。但是,当我注释掉 exit() 时,无论字节数如何,一切都会按预期工作。这就好像 exit() 停止了前一行代码的缓冲输出。这不是一个bug吗?谢谢

c++ fstream exit

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

无限循环读取文件到数组

尝试读取文件时,我有一个无限循环.文件从用户输入中保存,然后使用单独的功能进行读取,最后显示.

这是我的阅读功能.循环没有找到eof(); 从文件.我看不出是什么问题.没有编译器错误.

void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
    myFile.open("insured.dat", ios::in | ios::binary);                                      // Open myFile
    yourFile.open("uninsured.dat", ios::in | ios::binary);                                  // Open yourFile
    for(int i = 0; i < AMOUNT; i++)
    {
        myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
        yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
        for(i = 0; i < AMOUNT; i++)
        {
            while (myFile)
            {
                cout << "description: ";
                cout << items[i].description << endl;
                cout << "quantity: ";
                cout << items[i].quantity << endl;
                cout << "price: ";
                cout << items[i].price << endl;
                cout << "insured: "; …
Run Code Online (Sandbox Code Playgroud)

c++ fstream infinite-loop

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

如何将fstream对象用作成员变量?

以前,我会将fstream对象的地址传递给执行I/O操作的任何函数,包括构造函数.但我想尝试将fstream对象作为成员变量使用,以便所有后续I/O操作都可以使用这些变量而不是将它们作为参数传递.

考虑以下Java程序:

public class A {
    Scanner sc;

    public A(Scanner read) {
        sc = read;
    }
}
Run Code Online (Sandbox Code Playgroud)

什么是C++相当于此?我试过这样做

class A {
    ofstream *out;

    public:
        A (ofstream &output) {
            out = output;
        }
};
Run Code Online (Sandbox Code Playgroud)

但这给了我一个编译错误:

[错误]无效的用户定义从'std :: ofstream {aka std :: basic_ofstream}'转换为'std :: ofstream*{aka std :: basic_ofstream*}'[-fpermissive]

c++ io fstream

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

if语句在for循环中不起作用

好吧,我有这个问题:如果不想工作,我不知道为什么.

该程序应该从文件中取一些数字,首先表示第二行的数字数量,并说出该行的最小值和最大值.

#include <iostream>
#include <fstream>


using namespace std;

int main()
{
 ifstream f("file.in");
 ofstream g("file.out");
 int minim,x,maxim,i,n;

 f>>n;
 f>>maxim;
 minim=maxim;

 for(i=2;i<=n;i++){
    f>>x;
    if(minim > x)x=minim;
    if(maxim < x)x=maxim;
 }
    g << "min=" << minim;
    g << "\n" << "max=" << maxim;
    f.close();
    g.close();

return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是"如果"根本不起作用.

抱怨英语不好

c++ fstream for-loop if-statement file

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

为什么fstream不能在Linux中编写文件?

fstream 使用以下代码不能在Linux中写入文件:

#include <iostream>                     //for console input and output
#include <fstream>                      //for file input and output

int main(int argc, char **argv) {
    std::ofstream outFile("~/test_ws/src/data_logger/src/myFile.txt", std::ios::out | std::ios::binary);
    outFile.open("~/test_ws/src/data_logger/src/myFile.txt", std::ios::out | std::ios::binary);
    if(outFile.is_open()) {
        outFile << "Writing whether you like it or not.\n";
        std::cout << "YES!\n";
    } else std::cout << "Nope!\n";
    outFile.close();
}
Run Code Online (Sandbox Code Playgroud)

问题是什么?或者,我可以使用另一个C++ API在Linux中编写文件吗?

c++ linux api fstream ofstream

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

在控制台 C++ 上打印文本文件的内容

我试图将文件的内容打印到控制台,但它显示的是地址。请帮帮我(另外,请提出另一种方式而不是std::cout,因为我们在课堂上没有研究过,我不明白,但我想我应该使用它)。

这是代码:

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

int main()
{
    ifstream f("board.txt");
    if(f.is_open())
    {
        cout<<f;
    }
    else{
        cout<<"ERROR:no file opened.";
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ fstream file-handling

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

C++ - 从ifstream读取字符

我正在尝试从文件中读取字符,但陷入无限循环,这是代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream ifs("ifile.txt", ios_base::in);
    int b;
    while ((b = ifs.get()) != EOF)
    {
        cout << b << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我在正在创建 .exe 文件的项目 Debug 文件夹中创建了一个名为 ifile.txt 的文件(我使用的是 Microsoft Visual Studio 2013)

当我运行它时,它陷入无限循环。不知道为什么会发生这种情况,但我认为它可能与我创建 ifile.txt 的地方有关?

c++ fstream file visual-studio-2013

-3
推荐指数
1
解决办法
4731
查看次数

fstream没有创建文本文件

似乎当我尝试运行这个代码源而不是创建Attempt1.txt,或Attempt2.txt或Attempt3.txt时,它只是创建一个名为Attempt的文件.

    string str;
    int num_attempts_x_million = 1;

    str = "Attempt";
    str += num_attempts_x_million;
    str += ".txt";
    textfile.open(str); 
    textfile << password << endl;
    textfile.close();
Run Code Online (Sandbox Code Playgroud)

c++ string fstream

-4
推荐指数
1
解决办法
51
查看次数

为什么文件没有打开C++?

我只是第一次尝试这个代码.我无法找到错误的根源.

这是代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    fstream file;
    file.open("C:\\Users\\AfzaalAhmad\\Documents\\text.txt");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该文件存在于该位置.这是文件系统的屏幕截图.

文件系统的屏幕截图

在这种情况下没有例外,但文件永远不会打开!

我在哪里丢失代码?

c++ fstream

-5
推荐指数
1
解决办法
819
查看次数

c ++ std :: exception ...我可以请更多吗?

我浏览了http://en.cppreference.com/w/cpp/error/exceptionhttp://en.cppreference.com/w/cpp/io/basic_fstream,寻找可能引发的I/O异常.我知道我可以创建一个自定义异常类,并且有很多网站提供示例代码,但我想要一些专门针对最佳实践的指导.

还有,我应该集中精力使用图书馆让我的生活更轻松吗?

c++ fstream exception-handling exception c++11

-6
推荐指数
1
解决办法
304
查看次数

在抛出“std::ios_base::failure”what() 实例后调用终止:基本

我对 C++ 几乎是新手,当我想从文件中读取矩阵时遇到了一些问题。这是我在文件中的矩阵:

0   0   0   0   0   0   0
0.0344828   0.323538    0.0108884   2.6302  -0.00601043     0.0019921   2.37015
0.0689655   0.315323    0.00207342  3.92065     -0.00923253     0.00402598  3.35243
0.103448    0.175781    -0.063686   4.23273     -0.00881114     0.0056472   3.79407
0.137931    0.0224418   -0.136528   4.29968     -0.00513587     0.00650649  4.04466
0.172414    -0.0932421  -0.183356   4.33218     -0.000438406    0.00697551  4.20336
0.206897    -0.148808   -0.193323   4.40354     0.00352467  0.00698478  4.31558
0.241379    -0.16686    -0.172197   4.51575     0.00628366  0.00647283  4.40149
0.275862    -0.164914   -0.129917   4.63925     0.00770706  0.00567122  4.46758
0.310345    -0.128657   -0.092887   4.73243     0.00787571  0.00482414  4.52067
0.344828    -0.0776035  -0.0664981  4.79118     0.00743055  0.00400328  4.56789 …
Run Code Online (Sandbox Code Playgroud)

c++ fstream

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

c ++在一行中打开文件

我有这两行代码

ifstream inputFile;
inputFile.open("data.txt");
Run Code Online (Sandbox Code Playgroud)

我依稀记得有一种方法可以使用一行代码来做类似的事情。如何在一行中完成?

c++ fstream

-6
推荐指数
1
解决办法
411
查看次数