标签: file-io

我怎样才能在没有 fstream 的情况下进行文件 i/o 来参加 google code jam 之类的比赛?

让我先说我不是一个非常有经验的程序员来回答这个问题。

对于像 google code jam 这样的比赛,我编写的代码如下:

#include <fstream>

using namespace std;

int main() {
    ifstream fin("file.in");
    ofstream oin("file.out");
    //Etc. I'll now write out my solution.
    //...
}
Run Code Online (Sandbox Code Playgroud)

但是,我注意到其他参与者的许多其他代码源根本不使用 fstream,而是使用 iostream。然后他们将使用 cout 和 cin,就像从控制台读写一样。

他们是怎么做的?如果我使用 g++ 和 ubuntu,我可以做同样的事情吗?

编辑:由于有人要求我发布一个示例来说明我的意思,这里是参与者 ryuuga 的代码,他解决了最近 '11 资格赛中的大型机器人信任问题 A。

他使用 cin 和 cout 但我不知道他是如何进行文件 i/o 的。

#include <iostream>
using namespace std;
#include <cstdio>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
typedef pair<int,int> pii;
#include <vector>
typedef vector<int> vi;
#include <queue>
#include <stack>
#define …
Run Code Online (Sandbox Code Playgroud)

c++ file-io iostream

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

Android中使用文件操作写入和读取配置数据

在我的应用程序中有一些配置数据,例如ip地址、端口号、标题等。我想将这些数据私下保存在手机中。我决定以下面的格式写入数据

IPAddress=127.0.0.1
Port=1234
Title=MyNewApplication
Run Code Online (Sandbox Code Playgroud)

我对文件流感到困惑。我还想在不使用临时文件的情况下更新值。请提供解决方案。

我尝试使用下面的代码

public class Mtx {

public static final String PREFS_NAME = "MyPrefsFile";

public static void ConfWrite(String type, String value) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(null);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("IPAddress", "127.0.0.1");
    editor.putInt("port", 1234);
    editor.putString("Title", "MyNewApplication");

    // Commit the edits!
    editor.commit();
}

public static void ConfRead( String type, String value ) {

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(null);
        String ipAddress = settings.getString("IPAddress", "");
        int port = settings.getInt("port", 0);
        String title = settings.getString("Title", "");

        Log.e("", title);
}
}
Run Code Online (Sandbox Code Playgroud)

file-io android

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

使用 Primefaces 3.5 文件上传时出错

在使用 PrimeFaces 3.3 的项目中,文件上传运行正常。我删除了 Primefaces 3.3 并添加了 3.5。从那时起,我就无法进行文件上传了。之前我添加了 commons-io 和 commons.fileupload,我不确定是否还需要它们,所以我也保留了这些 jar 文件。

错误

java.lang.NullPointerException
    at org.primefaces.component.fileupload.FileUploadRenderer.decodeSimple(FileUploadRenderer.java:56)
    at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:47)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:836)
    at javax.faces.component.UIInput.decode(UIInput.java:771)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1228)
    at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
    at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1223)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1223)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at …
Run Code Online (Sandbox Code Playgroud)

file-io jsf file-upload primefaces

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

Python 处理目录中一系列编号(日期)的文件

我正在尝试使用 python 2.7 在目录中查找一系列特定文件。我的目录中有许多文件,其名称类似于 AB_yyyyjjjhhmmss_001.txt,其中 y 是年份,j 是儒略日期,h 是小时等等。每个时间对应于获取某些数据的时间,而不一定是创建或操作文件的时间。我喜欢选择一个时间范围,比如从 2​​013305010000 到 2013306123000 并处理它们。

我有类似的东西,

import glob

def get_time (start_time = None, end_time = None):

    if start_time == None:
        start_time = input("start: ")
    if end_time == None:
        end_time = input("end: ")

    duration = str(start_time) + "-" + str(end_time)

    listing = glob.glob("*_[" + duration + "]_*")
Run Code Online (Sandbox Code Playgroud)

我了解到这[ ]只是为了匹配单个数字。所以我在这里完全偏离了轨道。我也尝试过{start_time..end_time}组合,但没有效果。

python file-io glob python-2.x

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

Ruby on Rails 是否有文件读取流?

rails 有没有办法实现像 Node js 这样的读取流来读取文件?

IE

fs.createReadStream(__dirname + '/data.txt');
Run Code Online (Sandbox Code Playgroud)

相对于

fs.readFile(__dirname + '/data.txt');
Run Code Online (Sandbox Code Playgroud)

我在哪里看到红宝石有

file = File.new("data.txt")
Run Code Online (Sandbox Code Playgroud)

我不确定 ruby​​/rails 中用于创建流的等​​效项,并且想知道这是否可行。我要求的原因是内存管理,因为流将作为与整个文件相关的一块一块地交付。

ruby file-io ruby-on-rails stream

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

如何有效地列出包括子目录在内的目录中的所有文件?

我正在开发一个画廊应用程序,该应用程序显示手机或笔式驱动器中的所有图像。我成功地列出了所有图像并将其显示在应用程序中。但我认为它很慢。我使用的Depth First Search技术内的AsyncTask。那么有没有其他方法可以在里面使用AsyncTask,速度要快得多。这里的 root 是一个由树 URI 组成的 DocumentFile。

这是我使用过的代码。

public class ImageBackgroundTask extends AsyncTask<Object, Object, ArrayList<DocumentFile>> {
DocumentFile root;
ArrayList<DocumentFile> result;
ProgressDialog pg;
Context context;
private AsyncTaskCompleteListener<ArrayList<DocumentFile> > callback;

ImageBackgroundTask(DocumentFile root, Context context, AsyncTaskCompleteListener<ArrayList<DocumentFile>> cb){
    this.context=context;
    this.root=root;
    this.callback = cb;

}
@Override
protected ArrayList<DocumentFile> doInBackground(Object... voids) {
    Queue<DocumentFile> stack=new ArrayDeque<>();
    ArrayList<DocumentFile> list=new ArrayList<>();
    for(DocumentFile f:root.listFiles()){
        stack.add(f);
    }
    while(!stack.isEmpty()){
        DocumentFile child=stack.remove();
        if(child.isDirectory()){
            for(DocumentFile file:child.listFiles()){
                stack.add(file);
            }
        }
        else if(child.isFile()){
            String name=child.getName();
            if(name.endsWith(".jpg")
                    || name.endsWith(".png")
                    || …
Run Code Online (Sandbox Code Playgroud)

file-io android gallery image-gallery android-asynctask

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

f.close(), f.close 和 f.closed 的区别

with open('new.txt', 'r+') as f:
    print(f.readline())


if f.close:
    print('It is closed')
else:
    print('It is open.')
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,输出'It is closed'. 但是,如果我将 if 语句从f.closeto更改为 ,则会f.closed()得到输出'It is open'。那么我的文件是关闭还是打开?为什么我得到不同的输出?

python file-io file with-statement python-3.x

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

没有运算符“&lt;&lt;”匹配但“&gt;&gt;”有效

我在 C++ 中遇到了“没有运算符“<<”匹配这些操作数”错误(在 fout << dog 处)的问题。这是我的代码的样子:

int FileRepository::addDog(const Dog& dog)
{
    if (this->findDog(dog.getName()) != -1)
        return -1; 
    std::ofstream fout;
    fout.open(this->fileName.c_str(), std::ios_base::app);
    fout << dog;
    fout.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
ostream& operator<<(ostream& outputStream, Dog& dog)
{
    outputStream << dog.name << ", " << dog.breed << ", " << dog.birthDate << ", " << dog.numberOfShots << ", " << dog.photo << ", " << '\n';
    return outputStream;
}
Run Code Online (Sandbox Code Playgroud)

我还导入了特定的头文件和库,并且“>>”运算符有效。

在这里它有效:

void FileRepository::writeVectorToFile(std::vector<Dog> vectorOfDogs)
{
    ofstream fout(this->fileName.c_str());
    for (Dog dog : vectorOfDogs)
        fout …
Run Code Online (Sandbox Code Playgroud)

c++ file-io stream

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

CreateFile 函数执行没有错误但不创建任何文件

我的方法中有这样的实现

std::string name2 = "D:\\Buffer\\Temp\\t_libpng" + std::to_string(i) + ".png";
Run Code Online (Sandbox Code Playgroud)
bool _write_png_file_cv2_(char const *filename,
    int width, 
    int height,
    int widthStep,
    png_byte color_type, 
    png_byte bit_depth, 
    png_bytep *row_pointers)
{
    // ...

    hFile = CreateFile((LPCWSTR)filename,      // Open Two.txt.
                       GENERIC_WRITE,          // Open for writing
                       0,                      // Do not share
                       NULL,                   // No security
                       OPEN_ALWAYS,            // Open or create
                       FILE_ATTRIBUTE_NORMAL,  // Normal file
                       NULL);                  // No template file

    if (hFile == INVALID_HANDLE_VALUE)
    {
        printf("ERROR:: Could not open file");
        CloseHandle(hFile);            // Close the first file.
        return false; …
Run Code Online (Sandbox Code Playgroud)

c++ file-io createfile

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

C++:从(到)文件读取(写入)如果提供,否则回退到 std::cin (std::cout)

我有这个需要。如果提供,则从/向文件读取或写入回退到好朋友std::cin/ std::cout,如下所示:

// pseudo code for problem statement.

int main(int argc, char* argv[]) 
{
  istream reader;
  ostrea writer;
  if (argc > 1) 
  {
    // we have path to our file in argv[] 
    reader = ifstream(infile);
    writer = ofstream(outfile);
  }
  else
  {
    reader = cin;
    writer = cout;
  }
 // rest of code
}

Run Code Online (Sandbox Code Playgroud)

从本质上讲,即使我的想法很复杂,有没有办法实现类似的目标。谢谢!

使用的环境:C++ 11、14、17。

编辑#1:详细说明if条件,如果程序被称为

$: driver "input_file.txt" "output_file.txt"
Run Code Online (Sandbox Code Playgroud)

它应该从这些文件中执行 IO,相反,如果这样调用,

$: driver < "input_file.txt" > "output_file.txt" 
Run Code Online (Sandbox Code Playgroud)

它应该使用std::cinstd::cout …

c++ file-io

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