天道酬勤,学无止境

fread

C fread() magically reading dynamically allocated struct members, how?

This is a test program that I have written for a larger project that I am working on. It has to do with writing struct data to disk with fwrite() and then reading that data back with fread(). One member of the struct is dynamically allocated. First, here is my code #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRING_LEN 128 struct Person { int age; char *name; }; int main(int argc, const char *argv[]) { struct Person *person = calloc(1, sizeof(struct Person)); person->age = 22; person->name = calloc(STRING_LEN, sizeof(char)); char *name = "Name that is really, really

2021-06-14 13:56:16    分类:问答    c   struct   fwrite   fread

fread Function in C Programming

I have two questions about C's fread function: I have read that fread is used to read a binary file. However, when I read a binary file with fgets using read mode "r" and a text file with fread using "rb" mode, the results are the same as reading a text file with fgets and a binary file with fread. So, why are there different functions for reading binary and text files? I am using fread to read 10 bytes of a file in one call. How should I stop reading at the end of file – i.e. how is EOF specified in fread?

2021-06-14 12:58:21    分类:问答    c   fread

fread in R imports a large .csv file as a data frame with one row

I'm importing a large .csv file into R (about 0.5 million rows), so I've been trying to use fread() from the data.table package as a faster alternative to read.table() and read.csv(). However, fread() returns a data frame with all of the data from the rows inside one row, even though it has the correct number of columns. I found a bug report from 2013 showing this is related to the integer64 data class: http://r-forge.r-project.org/tracker/index.php?func=detail&aid=2786&group_id=240&atid=975 Are there any fixes or ways to get around this? The .csv file I'm trying to read is entirely integers

2021-06-14 07:12:32    分类:问答    r   import   data.table   fread

使用 fread() 导入大型 CSV (8 GB) 时“在字符串中嵌入 nul”('Embedded nul in string' when importing large CSV (8 GB) with fread())

问题 我有一个很大的 CSV 文件 (8.1 GB),我正试图将其转换为 R。我使用 Python 的 csvkit in2csv创建了 CSV,从 .txt 文件转换而来,但不知何故转换导致文件中出现空字符。 我现在在导入时收到此错误: Error in fread("file.csv", nrows = 100) : embedded nul in string: 'ÿþr\0e\0c\0d\0_\0z\0i\0p\0c\0' 虽然我可以使用read.csv导入小块,但这是因为它允许通过fileEncoding参数进行 UTF-16 编码。 test <- read.csv("file.csv", nrows=100, fileEncoding="UTF-16LE") 不过,我不敢尝试使用read.csv导入 8 GB 文件。 因此,我尝试了此处提供的解决方案,在该解决方案中,您使用sed s/\\0//g file.csv > file2.csv将空值拉出。 该命令执行得很好并填充了一个新的 8GB CSV 文件,但我收到了一个几乎相同的错误: Error in fread("file2.csv", nrows = 100) : embedded nul in string: 'ÿþr\0e\0c\0d\0_\0z\0i\0p\0c\0,\0p\0o\0s\0t\0_\0z

2021-06-12 21:26:05    分类:技术分享    r   csv   sed   data.table   fread

check return value fread and fwrite

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ if(argc != 3){ printf("Usage: ./copy filename newfile\n"); exit(1); } int bytes; long file_size, file_copied_size; FILE *file_to_copy, *new_file; if((file_to_copy = fopen(argv[1], "rb")) == NULL){ printf("File cannot be opened - read\n"); exit(1); } if((new_file = fopen(argv[2], "wb")) == NULL){ printf("File cannot be opened - write\n"); exit(1); } fseek(file_to_copy, 0, SEEK_END); file_size = ftell(file_to_copy); rewind(file_to_copy); char *buffer = malloc(1024 * 1024); /* Imposto un buffer di 1MB per maggiore efficienza

2021-06-12 00:16:10    分类:问答    c   return-value   fwrite   fread

fread 在 C 中丢弃回车符?(fread dropping carriage returns in C?)

问题 我希望能够读取 Windows 文本文件,在内存中修改它,然后用修改后的数据覆盖旧文件。 但是, fread 似乎没有存储我的 Windows 文本文件中存在的回车,当我覆盖旧数据时,这会导致问题。 我找不到其他似乎遇到过这个问题的人。 下面是一些演示问题的示例代码: #include <stdio.h> #include <stdlib.h> int main() { FILE* textFile; long fileSize; char fileCharacterBuffer[100]; int i; for(i = 0; i < 100; i++) { fileCharacterBuffer[i] = '\0'; } textFile = fopen("./Test.txt", "r+"); fseek(textFile, 0L, SEEK_END); fileSize = ftell(textFile); fseek(textFile, 0L, SEEK_SET); fread(fileCharacterBuffer, 1, fileSize, textFile); } 测试文件: 3 112 REd 110 green #5/09/2014 5087 - 5/6/2014 它的十六进制转储,显示它的 EOL 是 \r\n: Offset(h) 00 01 02 03

2021-06-11 23:43:34    分类:技术分享    c   fread   carriage-return

使用 fwrite() 和 fread() 进行二进制输入/输出(using fwrite() & fread() for binary in/output)

问题 我正在使用大量的浮点数。 经过大量摆弄后,我设法将其写入二进制文件。 稍后打开该文件时,读取过程仅读取少量浮点数(根据 fread() 的返回值,并且所有值都是 0.0f)。 读数应该将浮点数放入(原始)数组中,并且它不包含原始值。 我正在使用 Code::Blocks 和 MinGW 在 64 位 pc 上的 32 位领域中执行程序..而且我对 c/c++ 和指针不是很精通。 #include<...> const int mSIZE = 6000 ; static float data[mSIZE*mSIZE] ; void myUseFunc(){ const char *chN = "J:/path/flt_632_55.bin" ; FILE *outFile=NULL ; # .. sucessfully filling and using data[] .. size_t st = mSIZE*mSIZE ; outFile = fopen( chN , "w" ) ; if(!outFile){ printf("error opening file %s \n", chN); exit(0);} else{ size_t indt; indt = fwrite( data , sizeof(float), st , outFile ); std::cout <

2021-06-11 10:25:32    分类:技术分享    c++   binary   fwrite   fread

在多个站点上找到的关于 inf fopen 的 Javascript 示例对我不起作用(Javascript examples found on severial sites regardinf fopen is not working for me)

问题 我正在尝试使用 javascript 读取与我的 html 文件位于同一目录中的文本文件,以便我可以将文本文件的内容包含在我的 html 文件中。 这是我必须测试 fopen 和 fread 函数的代码 <html> <head> </head> <body> <script> fh = fopen('my.txt', 0); // Open the file for reading. if(fh!=-1) // Check if the file has been successfully opened. { length = flength(fh); // Get the length of the file. str = fread(fh, length); // Read in the entire file. fclose(fh); // Close the file. // Display the contents of the file. write(str); } </script> </body> </html> 我试过用 document.write 替换“write”,但仍然没有。 以下是一些以此代码为例的网站: http://answers.yahoo.com/question/index?qid=20130519190823AA2lQ1W http

2021-06-10 21:46:33    分类:技术分享    javascript   file   fopen   fread

为什么在构造函数中使用 fread 时我的程序会崩溃?(Why does my program crash when using fread in the constructor?)

问题 我有一个用 C++ 编写的小程序,其中包含一个带有大数组的类。 这个类看起来像这样: class Test { public: Test(); ... private: int myarray[45000000]; }; 现在,这个数组是从文件中读入的。 我想直接用构造函数来做到这一点,而不是费心调用任何额外的函数。 数组只需要读入一次,之后就不会再改变了。 它具有指定的确切尺寸。 我的构造函数如下所示: Test() { memset(myarray, 0, sizeof(myarray)); FILE* fstr = fopen("myfile.dat", "rb"); size_t success= fread(myarray, sizeof(myarray), 1, fstr); fclose(fstr); } 使用 Visual Studio 2012 Ultimate:当尝试启动一个使用这个类的程序时,它会在创建类后立即崩溃并显示“APPPCRASH”,并在尝试调试它时(我几乎不知道),告诉我认为错误是堆栈溢出。 这一切的奥秘在于,在我之前的版本中,myarray 是一个静态变量,我不得不调用一个静态函数来设置它,一切都很好。 但是试图将其转换为构造函数,尽我所能,我所有的尝试都失败了。 那么我在这里做错了什么? 回答1 所以你大概是在你的主要(或其他任何地方)

2021-06-09 19:47:50    分类:技术分享    c++   arrays   crash   fread

Does fread fail for large files?

I have to analyze a 16 GB file. I am reading through the file sequentially using fread() and fseek(). Is it feasible? Will fread() work for such a large file?

2021-06-09 13:39:09    分类:问答    c   large-files   fread