标签: file-io

从HD读取文件而不遵循计算机字节对齐时的性能问题

我有一个使用50字节数据结构的文件格式(.STL,立体光刻,结构是标准的,不能更改.不要与标准模板库混淆).从HD读取直接导致读取错误的数据,因为50个字节不是4的倍数.

在整个50字节结构中,我只需要36个字节.我现在用来提取数据的方法是手动将文件的读取位置偏移到下一组数据开始的位置,将36个字节读入临时变量,然后将数据转储到临时变量中进入它在阵列上的正确位置.

这是代码块:

threePoints* output = new threePoints [numTriangles]; // create an array to hold the entire file
threePoints* temp = new threePoints [1]; // temp variable to pull data out of each "cell"

// extract each triangle individualy
for (int i = 0; i < numTriangles; i++)
{
    stlFile.seekg (96 + i * 50, ios::beg);  //read vertex data and put them into tempoary array
                                            // offset = 80 header + 4 #triangles + 12 normal vector
    stlFile.read(reinterpret_cast<char*>(temp), (36)); // …
Run Code Online (Sandbox Code Playgroud)

c++ performance file-io

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

在中间读取并复制带有EOF指示的文件

我使用下面的代码从一个二进制文件复制到另一个,但第一个文件包含一些EOF指示符(0xFF)作为其一部分,因此复制功能实际上复制文件直到它的第一个EOF指示符.

例如:如果我的文件{0x01, 0x02, 0x03, 0xFF, 0x01, 0x02, 0xFF, 0xFF}只是{0x01, 0x02, 0x03}将被复制到新文件.任何想法如何解决它(或者我可能在那里遗漏了一些......)

码:

int Util_Copy_File(char* source, char* dest)
{
    FILE *fs,*ft;  
    char ch;
    char infile[100];
    sprintf(infile, "%s", dest);
    fs = fopen(infile,"r");  
    if(fs==NULL)  
    {
        return -1;  
    }
    ft = fopen(dest,"w");  
    if(ft==NULL)  
    {  
    fclose(fs);  
    return STATUS_FAIL;
    }  

    while(1)  
    {  
    ch = getc(fs);  
    if(ch==EOF)  
    {  
        break;  
    }  
    else  
        putc(ch,ft);  
    } 
    fclose(fs);  
    fclose(ft);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,Binyamin

c c++ file-io eof

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

将.txt文件的内容收集为字符串C++

我目前在这里有一个小程序,它会将.txt文件的内容重写为字符串.

但是我想将文件的所有内容收集为一个字符串,我该如何解决这个问题呢?

#include <iostream>
#include <fstream>
#include <string>


using namespace std;


int main () {
    string file_name ; 


    while (1 == 1){
        cout << "Input the directory or name of the file you would like to alter:" << endl;
        cin >>  file_name ;


        ofstream myfile ( file_name.c_str() );
        if (myfile.is_open())
        {
        myfile << "123abc";

        myfile.close();
        }
        else cout << "Unable to open file" << endl;


    }


}
Run Code Online (Sandbox Code Playgroud)

c++ string file-io text-files file-read

0
推荐指数
2
解决办法
2598
查看次数

使用File创建包含句点的目录

File testDir = new File("C:\temp\test");
testDir.createNewFile();
Run Code Online (Sandbox Code Playgroud)

据我了解,上述将创建一个目录的目录c名为test:\ TEMP

File testDir = new File("C:\temp\test.dir");
testDir.createNewFile();
Run Code Online (Sandbox Code Playgroud)

据我了解,上面将在目录c:\ temp中创建一个名为test.dir 的文件

如果我希望test.dir实际上是一个目录,我应该对上面的代码做什么?

java file-io

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

C#:是否正在使用和尝试使用StreamReader配置错误的选项?

想一想:

StreamReader reader = null;

try
{
    reader = new StreamReader(_fileName);
}
catch
{
    //show error that file not found
    reader.Dispose();
    return false;
}

try
{
    //code to read file
}
catch
{
   //show error badly formed file
}
finally
{
    reader.Dispose();
}
//return 
Run Code Online (Sandbox Code Playgroud)

当无法打开文件时,上面的代码不起作用,因为它调用Dispose for null导致异常.

我不想使用因为我想分开打开文件和阅读它的问题.这可以通过百万种不同的渔获量实现,但我不想这样做.另外如果使用与try-finally相同,那么"隐藏Dispose"会抛出不需要的异常吗?当我需要的只是捕获异常打开它并读取异常时,这将是最好的方法吗?

谢谢和BR - 马蒂

file-io dispose idisposable using c#-2.0

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

我无法打开变量文件名

有什么理由Dev C++不让我这么做file.open(file_name_variable)吗?我不明白为什么它不会让我打开任何东西,而是一个硬编码的名字,比如file.open("abc.txt")如何解决这个问题?不要使用Dev C++?

这基本上就是我所拥有的:

int open_file(string file_name){
    ifstream file;
    file.open(file_name);
    if (!file.is_open()){
        return 0;       
    }
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

c++ file-io file

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

从C中的文件中读取下一个非空白字符的最佳方法

从C程序中的文件读取下一个非空白(不是空格/换行符/制表符)字符的最佳(最短)方法是什么?

我意识到我可能会使用get跟随strtok,但似乎必须有更简洁的东西.

如果是这样,请告诉我; 如果没有,请告诉我.

谢谢.

c file-io

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

文件未找到?

我有一个赋值,我们有几个类给出,其中一个是文件读取器类,它有一个读取文件的方法,并使用包含文件路径的参数(String)调用,现在我有几个. txt文件,它们与.java文件在同一个文件夹中,所以我想我可以将file.txt作为文件路径传递(比如在php中,相对而言),但总是返回一个未找到的文件异常!

看到给定的类应该正常工作的事实,并且我确认这些类实际上与.java文件位于相同的文件夹workspace/src中我必须对文件路径字符串做错了什么,但是什么?

这是我的代码:

private static final String fileF = "File.txt";
private static final ArrayList<String[]> instructionsF =
CreatureReader.readInstructions(fileF);
Run Code Online (Sandbox Code Playgroud)

java file-io

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

在java中读取文件

我有一个包含以下文本的文件:

  8.8   0.0   0.00015      0.43      10      51       10      44        2      55  0.79
  10.9   0.0   3.5e-05       0.1     214     247       57      95       40     111  0.74
  10.5   0.0   4.7e-05      0.14     316     361      113     160      104     161  0.90
 -1.9   0.0      0.27     8e+02      62     109      385     432      372     465  0.76
Run Code Online (Sandbox Code Playgroud)

如您所见,每两个相邻数字之间的空格字符数可能会有所不同.

我想处理每一行,并将每个数字保存在变量中.

这怎么可能 ?

谢谢你的帮助

java file-io

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

C中文件I/O的问题

我很抱歉我的英语不好,但我会试着解释我的问题.使用C,我想输出一个IP地址列表.

但是,当我打开.txt文件时,它看起来是空白的.我也尝试在stderr上打印,但我根本没有输出.有什么问题?似乎for循环没有被执行.

这是代码.(将打印地址从192.168.51.1打印到192.168.51.254)

#include <stdio.h>
#define B1_S 192
#define B1_E 192
#define B2_S 168
#define B2_E 168
#define B3_S 51
#define B3_E 51
#define B4_S 1
#define B4_E 254
#define FNAME "ip.txt"
#define MIN_RANGE 0
#define MAX_RANGE 255

void to_next(int *x);

main()
{
  FILE *fp;
  int i, j, k, l;
  if(fp = fopen(FNAME, "w"))
  {
    for(i=B1_S; i<=B1_E; to_next(&i))
      for(j=B2_S; j<=B2_E; to_next(&j))
        for(k=B3_S; j<=B3_E; to_next(&k))
          for(l=B4_S; l<=B4_E; to_next(&l))
            fprintf(fp, "%d.%d.%d.%d\n", i,j,k,l);
    fclose(fp);
  }
  else
    printf("Error opening file.\n");
}

void …
Run Code Online (Sandbox Code Playgroud)

c file-io printf

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

标签 统计

file-io ×10

c++ ×4

c ×3

java ×3

c#-2.0 ×1

dispose ×1

eof ×1

file ×1

file-read ×1

idisposable ×1

performance ×1

printf ×1

string ×1

text-files ×1

using ×1