标签: file-io

生成具有任意二进制数据的 1GB 文件的最快现代 C++ 方法

所以,我尝试过这个:

uint64_t size = 1 * 1000 * 1000 * 1000;
std::vector<char> data(size, 'a');

std::ofstream myfile;
myfile.open("test.dat");
for (auto it = data.begin(); it != data.end(); ++it)
{
    myfile << *it;
}

myfile.close();
Run Code Online (Sandbox Code Playgroud)

它可以工作,但需要 15 分钟以上的时间才能发布。我运行的是 1TB NVME SSD 和 64GB RAM,我觉得我应该能够在几秒钟或不到一秒内完成此操作。

我确实可以访问 C++20。我更喜欢现代的东西,因为它具有可读性,只要它具有合理的性能。我觉得超过10秒就太慢了。

需要明确的是,将所有数据推回数组几乎是即时的。但写下来只需要几分钟的时间。

c++ performance file-io

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

如何指定完整文件名而不是短文件名作为 ifstream 或 ofstream 的参数

我正在编写一个程序来处理最佳公共交通路线。因此,我在文件夹中加载了一些文件。我可能需要阅读它们。例如,我有一个路径为 timetables_0006\weekday.txt 的文件。我想阅读它并使用文件中的数据。但是,我只遇到过短文件名的 ifstream,例如

ifstream input ("weekday.txt")
Run Code Online (Sandbox Code Playgroud)

我试过

ifstream input ("timetables\_0006\weekday.txt") 
Run Code Online (Sandbox Code Playgroud)

并失败了。是否可以输入完整的文件名,或者还有其他选择吗?谢谢。

c++ file-io

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

使用C++读取文件名

有没有办法使用纯C(或C++)从文件夹中读取文件名?这意味着不包括windows.h(没有FindFirstFile()等...).

它看起来不像fstream具有此功能.我知道文件名依赖于操作系统,但我希望有一些库可以在Windows中使用它.

c++ file-io

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

使用java重命名相同的文件

我想将文件重命名140103122005+0530_Penguins.jpgPenguins.jpg同一目录.

我试过renameTo()但它不工作,它需要目标文件,我没有目标文件.

任何人都可以告诉我如何使用Java从同一目录重命名相同的文件.

java file-io file

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

Java NewLine"\n"无法保存到文本文件

这是我的代码:

public String display() {
    return "\n......................\nFixed Employee:\n" + 
           "Name: " + super.fullName() + 
           "\nSalary: " + salary() + 
           " tk\n......................";
}
Run Code Online (Sandbox Code Playgroud)

但是当我从主类调用这个方法时,"\n"newLine无效.只显示一行输出.你会帮忙解决问题吗?

谢谢

java file-io

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

如何将数据添加到文本文件中,而不是覆盖我在Java中的内容

到目前为止这是我的代码,但它已经覆盖了我在文本文件中的内容.我想要的是将它添加到文本文件中的新行.

import java.io.*;
import java.util.Scanner;

public class Login{
  public static void main(String[] args) throws IOException {
    Scanner s1,s2;
    s1 = new Scanner(new FileInputStream("login.txt"));
    s2 = new Scanner(System.in);
    boolean loggedIn = false;
    String name,pword,n,p;
    System.out.println("Are you a new user? (Type y for yes or n for no)");
    String nU = s2.next();

    if (nU.equals("n"))
    {
    System.out.println("Enter username:");
    n=s2.next();
    System.out.println("Enter password:");
    p=s2.next();
    while(s1.hasNext()){
      name=s1.next();
      pword=s1.next();
      if(n.equals(name) && p.equals(pword)){
        System.out.println("You are logged in.");
        loggedIn = true;
        break;
      }
    }
    if(!loggedIn)
      System.out.println("Incorrect password or username."); …
Run Code Online (Sandbox Code Playgroud)

java file-io file

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

写入文件时fprintf不像printf吗?

我查看了文档:

在这里说:

成功打开文件后,您可以使用fscanf()从中读取文件或使用fprintf()写入文件.这些函数就像scanf()和printf()一样工作,除了它们需要一个额外的第一个参数,一个FILE*用于读/写文件.

所以,我这样编写了我的代码,并确保包含一个条件语句以确保文件打开:

# include<stdio.h>
# include<stdlib.h>

void from_user(int*b){

    b = malloc(10);
    printf("please give me an integer");
    scanf("%d",&b);

}

void main(){

    FILE *fp;

    int*ch = NULL;
    from_user(ch);
    fp = fopen("bfile.txt","w");

    if (fp == NULL){
        printf("the file did not open");
    }
    else {
        printf("this is what you entered %d",*ch);
        fprintf(fp,"%d",*ch);  
        fclose(fp);
        free(ch);   
    }
}
Run Code Online (Sandbox Code Playgroud)

我错了还是文档没有正确解释?谢谢.

c c++ linux file-io printf

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

C - fwrite - 无法打开文件,因为它包含无效字符

我正在使用C的fwrite函数将3个整数的数组写入文件,但是当使用gedit(使用Unicode UTF-8)打开输出文件时,我收到以下错误:

There was a problem opening the file.  The file you opened has invalid characters.  If you continue editing this file, you could corrupt the document.
Run Code Online (Sandbox Code Playgroud)

这是相关的代码段:

char* imageFile = "image.txt";
FILE* imageFilePtr = fopen(imageFile, "w");

int scores[3] = {69, 70, 71};
fwrite(scores, sizeof(int), sizeof(scores), imageFilePtr);
Run Code Online (Sandbox Code Playgroud)

当我使用十六进制读取器,如"xxd"时,我在终端中得到以下内容:

0000000: 4500 0000 4600 0000 4700 0000 7031 7108  E...F...G...p1q.
0000010: 0830 7108 2987 0408 2087 0408 0460 cebf  .0q.)... ....`..
0000020: 0100 0000 0000 0000 0000 0000 0000 0000  ................
Run Code Online (Sandbox Code Playgroud)

请记住,在我的环境中,sizeof(int)是4个字节.因此,我可以看到十进制中的69,70和71如何以xxd显示的十六进制中的45,46和47打印到文件中.但是,"4700 …

c file-io hex fwrite xxd

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

java do while循环无法访问变量

我在java中编写一个简单的文件I/O程序,我的程序允许用户输入他们想要编辑的文件(这将是我使用我的FileReader读取的文件).但是,我想继续提示用户输入文件名,直到他们输入有效文件(已存在的文件).出于某种原因,我的编译器(Eclipse)告诉我在任何地方都没有使用"布尔变量loopFlag".

当我运行程序时,如果用户输入错误的输入(不存在的文件),程序将正确运行并再次提示用户输入文件名.但是,如果用户输入了一个好的输入(已存在的文件),程序将无限循环并仍然提示用户输入文件名.这是我的代码:

String fileName;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // read in user input
boolean loopFlag; // compiler says this is not used anywhere, but I use it in my do-while loop?!?!

do
{
     loopFlag = false;
     System.out.println("Please enter the file name you wish to edit (including extension):");
     fileName = br.readLine(); // set fileName equal to user input
     // this try catch block is to make sure that the file exists
     try
     {
          FileReader fr = new FileReader(ManipulateText.readFileName);
          fr.close(); …
Run Code Online (Sandbox Code Playgroud)

java variables file-io filereader do-while

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

如果在 C 中不检查 feof 会发生什么?

在读/写操作期间是否绝对有必要检查feof()

我问的原因是因为我有一个执行读/写一次的程序,下面是代码:

while (1) {

      data = read_file_data(file);
      write_file_data(data, filename);

      if (feof(file)) 
          print(read error)
}
Run Code Online (Sandbox Code Playgroud)

这只是伪代码,但是是否有必要检查feof()像这样一次读取的情况?目前,我认为只有当您在上面的阅读之后再阅读一遍时才有必要,如下所示:

while (1) {

      data = read_file_data(file);
      write_file_data(data, filename);

      if (feof(file))       // feof error occurred oops
          print(read error)

      data = read_file_data(file); // reading after error    
}
Run Code Online (Sandbox Code Playgroud)

EOF reached最后,即使发生错误(读过去) ,阅读会产生什么后果EOF

c file-io pointers file feof

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

标签 统计

file-io ×10

c++ ×4

java ×4

c ×3

file ×3

do-while ×1

feof ×1

filereader ×1

fwrite ×1

hex ×1

linux ×1

performance ×1

pointers ×1

printf ×1

variables ×1

xxd ×1