FAQ Database Discussion Community


Skip some lines with fread

r,fread
I am interested to skip some lines of my data frame before the header names . How can i do it by skiping all the lines before ID_REF or if ID_REF is not present, check for the pattern ILMN_ and deleting all the lines keeping immediate first if not containing...

How can I most efficiently read the first n lines of a large text file in php?

php,fopen,fgets,fread,feof
So far I'm able to return the entire file, or an empty string. But not the first n lines of a file as expected. echo head('/path/to/my_file.txt',100); // returns '', or in other versions `1\n1\n...` function head($filepath, $lines = 1, $adaptive = true) { // Open file $f = @fopen($filepath, "rb");...

Error when reading a random string from a file

c++,c,fread
I tried to read 1 random item from this file: //"file.bin" structure: Offset Length 0 4 Total items (=n) 4 4 Item 1 Position = 4 + n * 5 = offset1 8 1 Item 1 Length = l1 9 4 Item 2 Position = offset2 = offset1 + l1...

In the following case, which one is better ? fread() or mmap()?

c,linux,memory,mmap,fread
I want to read two files via a process, the first file is about 2G, the second file is about 20M. They look like this: 1 1217907 1 1217908 1 1517737 1 2 1 3 1 4 1 5 Now I plan to read the whole file into memory, then...

Garbage after reading a large text file using (FILE*)

c,file,fread
I have a sample program written in C/C++, which reads a text file into memory. While trying to parse this file (not part of this sample), I came across lots of garbage near end of file. Investigating this I found there is some problem while reading large files into memory;...

fwrite() will do the job of fseek(f,1,SEEK_CUR)?

c,file,fwrite,fread,fseek
fwrite() will do the job of fseek(f,1,SEEK_CUR)? my code is : while (fread(&tmp,sizeof(compt),1,fc)) { if(tmp.num == m.crd ){ tmp.Solde-=m.mon; fseek(fc,-sizeof(compt),SEEK_CUR); fwrite(&tmp,sizeof(compt),1,fc); } if(tmp.num == m.deb){ tmp.Solde+=m.mon; fseek(fc,-sizeof(compt),SEEK_CUR); fwrite(&tmp,sizeof(compt),1,fc); fseek(fc,1,SEEK_CUR); } } the file is a group of two struct i put them before. the file is opened in r+. My...

Fread - large amount of integers

c,optimization,fread
i need to read large amount of integers from stdin. Speed is very important here, for example getchar_unlocked is too slow for me(hundredths of a second really matters) My code works with getchar_unlocked, but now I'm trying to read wholle line of unknown number of integers to buffer with fread....

fread() - Want to read an integer but it is returning garbage

c,file,fwrite,fread
I have a dynamic .txt file called library.txt, which has only one line of data. It is exactly this one: 63Book Title1|Book Author 1|Book Editor 1|2014|English|255|99.989998| I'm trying to read the first integer on the file (in this case, the number "63") using the void readInteger() function that I created:...

Error: Downloading broken file by fread in php

php,http-headers,fread,downloading
I tried different codes to download files from php. fileread() give issues so, that is not even an option now. I am trying to use fread() now, But the problem it gives is that when you download a file, that file never works. I download broken file. For example if...

How do I read back an 8 byte number after writing it to a file?

c,binary,fwrite,fread
I am able to write an 8 byte representation of a number to a file. However when I go to read it back, I do not get the number I am expecting. In my code below, I am attempting to write and read back the number 5000 to testfile.txt. #include...

fread from c++ binary file returning wrong value

c++,file-io,binaryfiles,fread,stdio
I am trying to read and write some boolean grids to a file using stdio.h. The user inputs a number nx (from 1 to 10, generally) and the program generates a list of nx by ceil(nx / 2) boolean grids (ceil(nx / 2) is ny). The grids themselves are stored...

behaviour of fread for empty (0kb) files

c,file,fread
Here is sample code which gives segmentation fault while reading empty file. #include <stdio.h> main() { FILE *fp; int i = 0; char buffer[20]; printf("1\n"); fp = fopen("/home/amadhab/aa", "r+"); printf("2\n"); i = fread(buffer, 1, 1, fp); printf("3\n"); printf("i = %d\n", i); } Gives o/p as 1 2 Segmentation fault Does...

Binary file read - write not working

c,pointers,binaryfiles,fwrite,fread
I've been trying to write a binary file with some information in a program but I can't make it work. I write it and try to read it to see if it worked. This is the struct I'm trying to write inside the file: typedef struct{ int puntuacio; int posicio_x;...

PHP: read part of file with fread() from http resource

php,fread
I have the following PHP code: $fp=fopen("http://myurl.com/very_big_file.txt",'r'); $feed=fread($fp,16); It works correctly but I have some doubts. Does it really reads only first 16 bytes without downloading the whole file?...

Infinite loop with fread

c,arrays,loops,malloc,fread
I'm trying to allocate an array 64 bytes in size and then loop over the array indexes to put a read a byte each from the inputfile. but when I don't malloc() the array indexes, the loop stays in index0 (so each time it loops it replaces the content in...

Significance of two arguments in fread?

c,fread
When reading the documentation for fread here, it explains that the two arguments after the void *ptr are multiplied together to determine the amount of bytes being read / written in the file. Below is the function header for fread given from the link: size_t fread(void *ptr, size_t size, size_t...

read and write in verilog when having negative numbers and array

io,verilog,fwrite,fread,negative-number
I am now doing reading input data from txt file and write the results into txt file. However the results runs in the simulation works well but it fail to link back the the conv module which is o = a+b; the system able to read the values in x.txt...

How to search for specific structure from Structures of data array and read using fread?

c,struct,fread,fseek,strstr
My text file looks like this, Person.txt John { sex = "Male"; age = 23; }; Sara { sex = "Female"; age = 23; }; stephan { sex = "Male"; age = 25; }; I want to fetch the specific person's data based on the request. For Instance, I got...

The Error “ Reached total allocation…” when using “fread” function

r,data.table,fread
I am trying to read 4Gb .txt file using "fread" function: mydata<-fread("myfile.txt") But after reading I get the following error: Error: cannot allocate vector of size 193.8 Mb In addition: Warning messages: 1: In lapply(globals, function(name) { : Reached total allocation of 4095Mb: see help(memory.size) 2: In lapply(globals, function(name) {...

Converting binary serialization to human-readable serialization

c,printf,fopen,fread,fscanf
I'm converting a program that used to do a binary dump of a struct to file and read that binary struct back in using fread. I want to convert it to creating and reading human readable data files, which of course means I need to format the data and such....

Opening a Text File using PHP - Fixing the Format

php,format,wamp,fread
Basically, my text file contains this info: WITH PARALLEL AND SERIAL ----- [System Info] ----------------------------------------------------------- Property Value Machine Type AT/AT COMPATIBLE Infrared (IR) Supported No DMI System UUID 809EC223-DAD7DD11-A2F33085-A993FFAC UUID 23C29E80-D7DA-11DD-A2F3-3085A993FFAC Disk Space Disk C: 89 GB Available, 97 GB Total, 89 GB Free Disk Space Disk D: 355 GB...

How would I read a text file in C?

c,file,fread
I have file.txt with 123456 2,00 beer 234567 2,50 milk 345678 3,30 ice cream I want to put this info in my dynamic two-dimensional array: char **dataBase; dataBase = (char**)malloc(NUM_OF_PROD * sizeof(char*)); for(i = 0; i < NUM_OF_PROD; i++){ dataBase[i] = (char*)malloc(MAX_BUFFER* sizeof(char)); } But I don't know how. We...

Read mpq_t from a binary file

c,fread,gmp
I have written mpq_t to a binary file called "data", and now I am trying to read mpq_t from the file one by one, but I kept having segfault at the line: gmp_printf("%Qd\n", buf). I've been debugging for a while but couldn't figure out where went wrong. int main(){ FILE...

Segmentation fault in comparing the second byte of two files

c++,fread,fseek
I was testing the commands fread() and fseek, and I made a programme to compare the second byte of both the programme. The programme gave segmentation fault at the line indicated (the last fread()). int main(int argc, char** argv) { FILE *file1, *file2; long size1, size2; long size1a, size2a; char...

How to use fread() with “https” url scheme?

r,https,data.table,fread
In R Documentation for fread() it's said that "input" argument may be a URL starting http:// or file://. However in this "Introduction to data.table" vignette fread() is used with https: flights <- fread("https://raw.githubusercontent.com/wiki/arunsrinivasan/flights/NYCflights14/flights14.csv") That results in "Error in download.file(input, tt, mode = "wb") : unsupported URL scheme" console message. I...

Why do I get Segmentation fault (core dumped)?

c,segmentation-fault,binaryfiles,fwrite,fread
I am a C beginner. After going through many tutorials I am not sure why am I getting the segmentation fault. I get this error on both my functions write_new_file and search_by_firstname. I think the problem lies within the argument type PERSON *inrecord in my function write_new_file. I have already...

Reading bytes from a file and overwriting the same bytes

c++,c,file,fread
I'm using fread to read the contents of a file and I want to over-write the bytes I've just read. So let's say: fread(buffer, buffersize, 1, FilePointer); I want to overwrite the exact same bytes that I've just read. The size of buffer varies, but I want to overwrite the...

ANSI C What am I doing wrong with fwrite and the fread?

c,printf,fwrite,fread
I have the following code in which I attempt to write some data to a file on first execution, and then on second execution reads the file and prints the data to the screen. In the first execution it writes the data to the screen exactly as it is in...

fwrite corrupts my text file

c,fwrite,fread
I have been tinkering around for days with a program for a class of mine, and I cannot get fwrite to cooperate. I've tried looking at the online resources for fread and fwrite, looked at my professor's examples, and tried time and time again to fix this, but nothing has...

Read a line in C and split it

c,fread,sscanf
I got a problem trying to split 'str' in 2 int vars and 1 char var. Here is a code: FILE *fp; int i,j; char str[8]; char valor; if(!(fp = fopen("miniflota_g.dat","rb"))){ printf("Error opening file."); getch(); return; } while(fread(str,sizeof(str),1,fp) == 1){ sscanf(str,"%d %d %c",i,j,valor); printf("%d %d %c",i,j,valor); } fclose(fp); And this...

How to input a huge txt using fread() and put it into a stringstream?

c++,string,stream,fread,stringstream
I am trying to input a huge txt file (approximately 5 MB) into a stringstream, but I face a problem to define fread(). I know, here, I have to use a string to put that string created by fread(). Here is my code, I don't know where is my mistake....

Fwrite() & Fread() vs. Mysql Database [closed]

php,mysql,fwrite,fread
I'm no expert in this world so I can only imagine the ignorance in this question. I'm looking to build a very simple form tool for a work project. That said, all content needs to remain locally on a mapped share drive. Rather than connecting a mysql database to my...