fread

fread()模板

耗尽温柔 提交于 2019-12-03 14:43:23
char buf[1<<20],*p1,*p2; #define GC (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++)//提交时使用 //#define GC getchar()//getchar()用于调试,要慢很多 inline ll read_64(){ll X=0,w=0; char ch=0;while(!isdigit(ch)) {w|=ch=='-';ch=GC;}while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=GC;return w?-X:X;}//read_64读long long inline int read_32(){int X=0,w=0; char ch=0;while(!isdigit(ch)) {w|=ch=='-';ch=GC;}while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=GC;return w?-X:X;}//read_32读int 来源: https://www.cnblogs.com/pepparan/p/11800768.html

C++fread/fwrite的基础用法

孤街醉人 提交于 2019-12-03 12:03:54
前言 fread是吼东西 应某人要求(大概)科普一下 fread #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #define fo(a,b,c) for (a=b; a<=c; a++) #define fd(a,b,c) for (a=b; a>=c; a--) using namespace std; char st[233]; char *Ch=st; int main() { fread(st,1,233,stdin); cout<<st<<endl; cout<<*Ch<<endl; } 可以用文件输入,也可以直接输并在最后加Ctrl+Z (下面的空行是因为读入了一个换行符) fread基本格式: fread(字符串,1,字符串大小,stdin); *Ch一开始指向的是st[0],之后可以不断*++Ch来往后跳 快速读入 #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #define fo(a,b,c) for (a=b; a<=c; a++) #define fd(a,b,c) for (a=b; a>=c; a--) using namespace std;

How to use fread and fwrite functions to read and write Binary files?

浪尽此生 提交于 2019-12-03 11:29:50
问题 Hi in my project I've to read a .bin file which has sensor data in the form of short(16 bit values) . I'm doing this using fread function into a buffer, but I feel that the reading-in is not happening correctly. I mean there is no consistence between what I write and what I read in. Can you guys suggest what is going wrong here? This is not my code from my project... I'm only trying to verify the fread and fwrite functions here. #include<stdio.h> void main() { FILE *fp = NULL; short x[10] =

R data.table: using fread on all .csv files in folder skipping the last line of each

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have hundreds of .csv files I need to read in using fread and save as one data table. The basic structure is the same for each .csv. There is header info that needs to be skipped (easy using skip = ). I am having difficulty with skipping the last line of each .csv file. Each .csv file has a different number of rows. If I have only one file in the Test folder, this script perfectly skips the first rows (using skip = ) and the last row (using nrows = ): file <- list.files("Q:/Test/", full.names=TRUE) all <- fread(file, skip = 7, select = c(1

fread函数详解

房东的猫 提交于 2019-12-03 09:52:22
fread函数详解 函数原型: size_t fread( void *buffer, size_t size, size_t count, FILE *stream ) buffer 是读取的数据存放的内存的指针(可以是数组,也可以是新开辟的空间,buffer就是一个索引) size 是每次读取的字节数 count 是读取次数 strean 是要读取的文件的指针 例如 从文件fp里读取100个字节 可用以下语句 fread(buffer,100,1,fp) fread(buffer,50,2,fp) fread(buffer,1,100,fp) ************************************************************************************** 对读出的二进制流是不能用strlen()或者sizeof()求其长度和大小的。 ************************************************************************************** fread可以读二进制文件,有时用字符方式去读文件不能读完整个文件,但是二进制方式就可以 。 这就是因为字符方式用特定的标记结尾的,读取时只要碰到该标记就自动结束 函数fread()读取[ num ]个对象

Write and read php object in a text file?

家住魔仙堡 提交于 2019-12-03 09:38:48
问题 I want to write a php object in a text file. The php object is like that $obj = new stdClass(); $obj->name = "My Name"; $obj->birthdate = "YYYY-MM-DD"; $obj->position = "My position"; I want to write this $obj in a text file. The text file is located in this path $filePath = getcwd().DIRECTORY_SEPARATOR."note".DIRECTORY_SEPARATOR."notice.txt" I want a simple way to write this object into the text file and want to read the file to get the properties as I defined. Please help me. Thanks in

Matlab, fread, speed up reading file with multiple data types and multiple sample rates

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Using Matlab R2015a. I'm using fread to read sensor data from a binary file. The data contains multiple precisions. One signal is sampled at 5 times the rate of the other signals. The file is structured as a sequence of batches, where each batch has e.g. 1 sample of signal_A, but 5 samples of signal_B. I have googled for examples on how to load and format the data fast, but the solutions I have seen only represent cases where there is a single sampling rate, making the solution simpler, as far as I can tell. What I would like to avoid is the

How to import last 100 rows using read.csv() in R

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Hi I've a huge file and i want to import only the last 100 rows from that file. How can we do that using read.csv() or any alternative? 回答1: The package R.utils has a function called countLines(). You could do: l2keep <- 10 nL <- countLines("your.csv") df <- read.csv("your.csv", header=FALSE, skip=nL-l2keep) 回答2: If you are on a *nix system, you are better off using the tail -n 100 command to take the last 100 rows. Anything implemented in R would be slower and potentially much slower is your file is truly huge. If you are using Windows, you

How can I read in a RAW image in MATLAB?

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to open and read a .raw image in MATLAB. My file can be downloaded here . I have tried the following three code snippets, but neither gives the expected results. Code Snippet #1 row=576; col=768; fin=fopen('m-001-1.raw','r'); I=fread(fin,row*col,'uint8=>uint8'); Z=reshape(I,row,col); Z=Z'; k=imshow(Z); It shows this picture: Code Snippet #2 f=fopen('m-001-1.raw'); a=fread(f); input_img = reshape(a,768, 576, 3); input_img = imrotate(input_img, -90); imwrite(input_img, 'm-001-1.jpg'); This saves a blank (just white) image in .jpg format

Any way to force fread() of data.table not to stop on empty lines?

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: (very simple question - but I couldn't find an answer in fread help)... So, I have a table with some empty lines in the middle. When I try to open it with fread , it stops, saying Stopped reading at empty line 10006, but text exists afterwards (discarded) . Is there any way to avoid this without changing the data file? 回答1: Version 1.9.8 of data.table , released 25-NOV-2016, has a new blank.lines.skip option to skip blank lines. text 回答2: You can use the Windows findstr command to get rid of empty lines. Example file "Data.txt". 1,a 2,b 3,c