fread

C fread not getting entire file

白昼怎懂夜的黑 提交于 2019-12-02 01:42:39
问题 I am trying to read binary files and storing the content into a char array. This function is working great for text files, but for non-text files (a PNG file for example) it does not work as expected. Below is the code followed by the results. What is wrong? Code: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> unsigned int readFile(const char *fileName, char **contents); int main(int argc, char *argv[]) { char *contents = 0; unsigned int length = readFile(argv

C fread not getting entire file

本秂侑毒 提交于 2019-12-02 01:09:17
I am trying to read binary files and storing the content into a char array. This function is working great for text files, but for non-text files (a PNG file for example) it does not work as expected. Below is the code followed by the results. What is wrong? Code: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> unsigned int readFile(const char *fileName, char **contents); int main(int argc, char *argv[]) { char *contents = 0; unsigned int length = readFile(argv[1], &contents); fprintf(stderr, "File length: %d\r\n", length); fprintf(stderr, "File contents:\r\n%s

Compare two files Byte by Byte

非 Y 不嫁゛ 提交于 2019-12-01 20:56:31
I have two binary files and I want to compare them Byte by Byte. I came up with the following code to do so: int CompareFiles(char *pFname1, char *pFname2) { FILE *pFile1,*pFile2; long lSize1, lSize2; // file length int i=0; char tmp1, tmp2; pFile1 = fopen(pFname1,"r"); pFile2 = fopen(pFname2,"r"); // obtain file size: fseek (pFile1 , 0 , SEEK_END); lSize1 = ftell (pFile1); rewind (pFile1); // obtain file size: fseek (pFile2 , 0 , SEEK_END); lSize2 = ftell (pFile2); rewind (pFile2); if (lSize1 != lSize2) { printf("File sizes differ, %d vs. %d\n",lSize1,lSize2); return ( ERROR ); } for (i=0;i

fread dropping carriage returns in C?

女生的网名这么多〃 提交于 2019-12-01 20:11:35
I want to be able to read in a Windows text file, modify it in memory, and then overwrite the old file with the modified data. However, fread doesn't seem to store the carriage returns present in my Windows text file, which is throwing things off when I write over the old data. I can't find anyone else who seems to have had this issue. Here is some example code that demonstrates the problem: #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

fread error “unexpected character ending field”

懵懂的女人 提交于 2019-12-01 20:11:20
could you please help me? I am trying to load large TSV file (4 mln rows), and using for that 'fread' (enormous speed :) Problem is that when reaching certain line all program crashes. Last message from verbose is " Bumping column 12 from INT64 to REAL on data row 2220004, field contains '0.54'" I tried to copy only till that row with 'skip' option - it worked fine, but after when I was trying to copy last rows it immediately thrown another error: Unexpected character ("Ам) ending field 5 of line 2220005 After I tried to disable header, to drop the 12th column, to input column classes -

how to read contents of a structure using fread() in c

一世执手 提交于 2019-12-01 14:42:59
I have the following code where I am trying to read the content of a file and display it and also write to another file. My problem is the content i get on the screen is completely different from the content of the file. I have put parts of the contents of the file and parts of the result displayed #include<iostream> #include <stdint.h> #include <stdio.h> struct test { uint64_t start; uint16_t length; struct test *next; }; void main() { char frd[32]; std::cout<<"\nEnter name of file to read?\n"; std::cin>>frd; FILE *rd=fopen(frd,"r+b"); FILE *wrt=fopen("abc2.doc","w+"); struct test test_st;

Why does my program crash when using fread in the constructor?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 14:08:45
I have a small program, written in C++ that contains a class with a large array. The class looks like this: class Test { public: Test(); ... private: int myarray[45000000]; }; Now, this array is read in from a file. I would like to do this with the constructor directly, instead of bothering to call any extra functions. The array only needs to be read in once, and afterwards will not change anymore. It has the exact size specified. My constructor looks like this: Test() { memset(myarray, 0, sizeof(myarray)); FILE* fstr = fopen("myfile.dat", "rb"); size_t success= fread(myarray, sizeof(myarray),

how to read contents of a structure using fread() in c

牧云@^-^@ 提交于 2019-12-01 12:42:34
问题 I have the following code where I am trying to read the content of a file and display it and also write to another file. My problem is the content i get on the screen is completely different from the content of the file. I have put parts of the contents of the file and parts of the result displayed #include<iostream> #include <stdint.h> #include <stdio.h> struct test { uint64_t start; uint16_t length; struct test *next; }; void main() { char frd[32]; std::cout<<"\nEnter name of file to read?

fread EOF instead of separator

天涯浪子 提交于 2019-12-01 12:05:06
I'm trying to read a huge file with fread, but i guess something is messed with the layout of the file. If i try to read the file with data = fread(input = "../data.txt", sep = "\t") on this file (i just took the line with the error and few before and after): ID imdbID Title Year Rating Runtime Genre Released Director Writer Cast Metacritic imdbRating imdbVotes Poster Plot FullPlot Language Country Awards lastUpdated Type 683 tt0000683 The Fatal Hour 1908 14 min Short, Crime 1908-08-18 D.W. Griffith D.W. Griffith George Gebhardt, Harry Solter, Linda Arvidson, Florence Auer 5.9 26 Pong Lee, a

Importing binary LabVIEW files with header information into MATLAB?

对着背影说爱祢 提交于 2019-12-01 11:43:47
I have large .bin files (10GB 60GB) that I want to import to MATLAB; each binary file represents the output of two sensors, thus there are too columns of data. Here is a more manageable sized example of my data . You will notice that there is a .txt version of the data; I need to upload the .bin files directly to MATLAB, I can't use the .txt version because it takes hours to convert with larger files. The problem I have is that the .bin file has header information that I can't seem to interpret properly, and thus I cannot extract the data in MATLAB every time I try I seem to get gibberish