标签: file-io

读取文本文件显示到控制台然后附加文本文件

我有一个名字的文本文件.我想将文本文件读入流中,将其显示到控制台.完成后,它将提示用户输入他们的名字.然后它应该将它添加到文件中.

我可以让它分开做这两件事而不是一起做.这是我的代码.

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

using namespace std;
using namespace System;

int main(array<System::String ^> ^args)
{
 fstream myfile;
 string line;
 string name;
    myfile.open("Names.txt",ios::out | ios::in | ios_base::app);
    if (myfile.is_open())
    { 
      while( getline(myfile, line) )
      {
          cout << line << endl;
      }
     cout << "Enter your name!\n";
     getline (cin, name);
     myfile << name;
     myfile.close();
 }
 else
 {
     cout << "file was not opened\n";
 }

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

如果我将while循环留在那里,它会将所有名称写入控制台,但不会将用户输入的名称附加到列表中.如果我取出while循环,我可以为文件添加一个名称,但当然我没有得到该文件中已有的名称列表.

我最好的猜测是,我认为这可能与以下事实有关:在使用getline循环浏览文件后,位置位于我的流的末尾,所以当我尝试为其添加名称时,没有'在流中留下的任何房间?

c++ file-io iostream

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

源代码可以在运行时操纵其他或自身吗?

例如,两个源代码的编写方式是,它们都返回一个数字并打印出一个随机字符串(例如:"我已经死了数字次数"),这个数字次.这里重要的一点是,从一个程序返回的数字是添加到另一个程序的字符串的数字.还应该考虑的一件事是两个程序假设干预其他源代码然后永远运行它.这意味着一个程序的返回值实际上是操纵另一个程序的主要因素.

问题:它是否可行(例如在java中)如果程序可以改变自己的源代码?

java file-io

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

以编程方式告诉c ++中的文件类型

有什么方法可以以编程方式查看文件并告诉它是什么类型的文件,例如mp3,txt,binary,photoshop等?我不认为STL中会有任何内容(除了尝试打开文本文件并将其读入).

如何尝试告诉您真正处理的文件类型?我的意思是我可以将.mp3扩展名放在一个真正不是mp3文件的文件上.

c++ file-io binaryfiles file

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

即使子目录不存在,如何创建文件

即使某些子目录不存在,如何创建文件?我知道这个问题及其答案表明我Directory.CreateDirectory(path)用来创建我的文件及其目录.

但是当我尝试使用Directory.CreateDirectory(path)它创建文件时,会将文件创建为目录/文件夹而不是文件?

在此输入图像描述

Directory.CreateDirectory (@"C:\Users\me\AppData\LocalLow\company\product\database.db");
Run Code Online (Sandbox Code Playgroud)

为什么是database.db文件夹而不是文件?难道我做错了什么?即使某些子目录不存在,如何创建文件?

c# file-io

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

Python文本文件而不是字典

我正在开展一个项目,在这个项目中,我将大量数据抓取并重新组织到结果文本文件中.以前我使用字典来存储临时数据,但随着数据量的增加,进程因内存使用速度变慢而字典变得无用.

由于处理速度在我的情况下并不那么重要,我正在尝试将字典替换为文件,但我不确定如何轻松地将文件指针移动到适当的位置并读取所需的数据.在字典中,我可以轻松地参考任何数据.我想在文件中实现相同的目标.

我正在考虑使用mmap并编写我自己的函数来将文件指针移动到我想要的位置.Python是否有内置或第三方模块用于此类操作?

欢迎任何其他理论方法.

python file-io dictionary

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

为什么在python中调用file.read会用垃圾填充我的文件?

运行这个:

import os

if __name__ == '__main__':
    exclude = os.path.join(
        r"C:\Dropbox\eclipse_workspaces\python\sync\.git", "info", "exclude")
    with open(exclude, 'w+') as excl:  # 'w' will truncate
        # print excl.read() # empty
        # excl.readall() # AttributeError: 'file' object has no attribute
        # 'readall' -- this also I do not understand
        excl.write('This will be written as expected if I comment the
         line below')
        print "Garbage\n\n", excl.read()
    # if I do not comment the line however, the file contains all the garbage
    # excl.read() just printed (edit: …
Run Code Online (Sandbox Code Playgroud)

python io file-io python-2.7

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

php文件上传错误警告move_uploaded_file无法打开流权限被拒绝

这是两个错误

警告:move_uploaded_file(/uploads53e866b24977d1.48375376.pdf):无法打开流:权限在第28行的C:\ xampp \ htdocs \ file_upload \ upload.php中被拒绝

警告:move_uploaded_file():无法在第28行的C:\ xampp \ htdocs \ file_upload \ upload.php中将“ C:\ xampp \ tmp \ php7D69.tmp”移动到“ /uploads53e866b24977d1.48375376.pdf”

这是我的HTML

<form method="POST" action="upload.php" enctype="multipart/form-data">
        <label for="">Upload Your Cv</label><input type="file" name="file">
        <input type="submit" value="upload">
</form>
Run Code Online (Sandbox Code Playgroud)

这是我的PHP

if (isset($_FILES['file'])) {
        $file   = $_FILES['file'];
        // print_r($file);  just checking File properties

        // File Properties
        $file_name  = $file['name'];
        $file_tmp   = $file['tmp_name'];
        $fiel_size  = $file['size'];
        $file_error = $file['error'];

        // Working With File Extension
        $file_ext   = explode('.', $file_name);
        $file_fname = explode('.', $file_name); …
Run Code Online (Sandbox Code Playgroud)

php xampp file-io file-upload

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

从文件输入C++定义全局变量

我必须在我的C++代码中定义一个全局数组,其大小必须从文件中读取.我使用以下代码

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

string inputfile = "input.txt";
ifstream infile(inputfile.c_str());
infile>>N; // N = size of Array
int array[N];

// ------some code here-----

int main(){
int N;
cout << N<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

但如果我放置3条线

string inputfile = "input.txt";
ifstream infile(inputfile.c_str());
infile>>N; // N = size of Array
Run Code Online (Sandbox Code Playgroud)

在主循环内部,此代码有效.不幸的是我不能把它放在任何函数中,因为我需要从变量N初始化一个全局数组.

我问了很多人并搜索了不同的地方,但我似乎无法弄清楚这一点.谢谢你的帮助.

c++ file-io initialization

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

我可以在后台写一个文件吗?

在执行我的脚本期间,我需要将一个大文件写入磁盘以存储部分计算.这个文件很大,其余的脚本不需要它.目前,我需要等待写入完成才能继续执行脚本.

有没有办法在后台编写文件?有点像启动一个单独的写入过程,而其余的脚本继续不受干扰?

ruby file-io background-process

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

读取1.5行的Bash脚本

我有一个像文本文件

a
b
c
d
e
f
Run Code Online (Sandbox Code Playgroud)

我想写一个bash脚本,它读取文件并返回

ab
bc
cd
de
ef
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的尝试是一个脚本,它读取2行2行,并尝试保存第二个变量,以便在下一轮中重复使用它.但到目前为止我的尝试是:

#!/bin/bash

while read LINE
do let "COUNT++";
        if [[ $COUNT == 2 ]]; then
                echo $PREV $LINE
                COUNT=0
                LINE=$PREV2
        else
                PREV="$LINE"
        fi
done
Run Code Online (Sandbox Code Playgroud)

我能管理的最好的是像这样的列表

ab
cd
de
f
Run Code Online (Sandbox Code Playgroud)

bash file-io

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