fseek

How can I read a known number of strings of unknown size from a .txt file and store each line in a line of a matrix (in C)?

有些话、适合烂在心里 提交于 2021-01-28 01:02:22
问题 Title is pretty self explanatory. I'm almost sure that the end result wouldn't be a matrix as each line would have a different number of columns, so it's more like a array of arrays of variable sizes. It would also be interesting to sort the fragments by size, biggest first. This is what I've tried so far: int main() { char str[MAXLEN], **fragmentsList; int number_of_strings, i, max, k; printf("Enter .txt file name: "); scanf("%s", str); printf("How many strings does the file has? "); scanf("

php使用file函数、fseek函数读取大文件效率分析

这一生的挚爱 提交于 2020-04-04 03:11:32
php 读取大文件可以使用 file 函数和 fseek函数 ,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下。 1. 直接采用file函数来操作 由于 file函数是一次性将所有内容读入内存,而PHP为了防止一些写的比较糟糕的程序占用太多的内存而导致系统内存不足,使服务器出现宕机,所以默认情况下限制只能最大使用内存16M,这是通过php.ini里的 memory_limit = 16M 来进行设置,这个值如果设置-1,则内存使用量不受限制。 下面是一段用file来取出这具文件最后一行的代码: <?php ini_set('memory_limit', '-1'); $file = 'access.log'; $data = file($file); $line = $data[count($data) - 1]; echo $line; ?> 整个代码执行完成耗时 116.9613 (s)。 我机器是2个G的内存,当按下F5运行时,系统直接变灰,差不多20分钟后才恢复过来,可见将这么大的文件全部直接读入内存,后果是多少严重,所以不在万 不得以,memory_limit这东西不能调得太高,否则只有打电话给机房,让reset机器了。 2.直接使用PHP的 fseek 来进行文件操作

fread和fseek的用法

随声附和 提交于 2020-04-04 00:05:47
原味:http://baike.baidu.com/view/656696.htm    http://baike.baidu.com/view/656689.htm fread   功 能: 从一个流中读数据   函数原型: size_t fread( void * buffer , size_t size , size_t count , FILE * stream );     参 数:   1.用于接收数据的地址(指针)( buffer )   2.单个元素的大小(size) :单位是字节而不是位,例如读取一个整型数就是2个字节   3.元素个数( count )   4.提供数据的文件指针(stream)   返回值:成功读取的元素个数 程序例 #include <stdio.h> int main(void) { FILE *stream; char msg[] = "this is a test"; char buf[20]; if ((stream = fopen("DUMMY.FIL","w+")) == NULL ) { fprintf(stderr,"Cannot open output file.\n"); return 1; } fwrite(msg,strlen(msg)+1,1,stream); fseek(stream,0,SEEK_SET);

fseek函数

泪湿孤枕 提交于 2020-03-15 18:32:25
  int fseek( FILE *stream, long offset, int origin );   第一个参数stream为文件指针   第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移   第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET   SEEK_SET: 文件开头   SEEK_CUR: 当前位置   SEEK_END: 文件结尾   其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.   简言之:   fseek(fp,100L,0);把fp指针移动到离文件开头100字节处;   fseek(fp,100L,1);把fp指针移动到离文件当前位置100字节处;   fseek(fp,100L,2);把fp指针退回到离文件结尾100字节处。 来源: https://www.cnblogs.com/hongzg1982/archive/2010/04/14/1711852.html

fseek go back to the end of file when writing a value

[亡魂溺海] 提交于 2020-02-25 03:04:14
问题 I am trying to override 4 bytes at position 4 in a file, but fseek seems not to be working. My code: int r = fseek(cacheStream, 4, SEEK_SET); std::cout << "fseek returns " << r << std::endl; std::cout << "ftell " << ftell(cacheStream) << std::endl; r = fwrite(&chunckSize, sizeof(uint32_t), 1, cacheStream); std::cout << "fwrite returns " << r << std::endl; std::cout << "ftell " << ftell(cacheStream) << std::endl; cacheStream was open with "ab". The output is: fseek returns 0 ftell 4 fwrite

C语言fseek()函数和ftell()函数

时间秒杀一切 提交于 2020-02-06 04:25:35
C语言fseek()函数和ftell()函数 方便用的时候看, FILE * fp ; int ch ; long count = 0l , tell ; * * * 前移: * * * fseek ( fp , 0l , SEEK_END ) ; tell = ftell ( fp ) ; count = 1l ; //为了count成为-1 while ( count <= tell ) { fseel ( fp , - count , SEEK_END ) ; //从文件末尾向前移 ch = getc ( fp ) ; fprintf ( stdout , "%c" , ch ) ; count ++ ; } * * * 后移: * * * fseek ( fp , 0l , SEEK_END ) ; //定位到文件末尾,如果不写这个函数,下面的ftell()函数返回值为0; tell = ftell ( fp ) ; //从文件起始位置到文件末尾的字节数 printf ( "tell==%ld\n" , tell ) ; while ( count < tell ) { fseek ( fp , count , SEEK_SET ) ; //从文件的起始位置开始 ch = getc ( fp ) ; fprintf ( stdout , "%c" , ch ) ;

Windows physical drive access fopen and fseek

时光怂恿深爱的人放手 提交于 2020-01-11 12:29:31
问题 I'm currently trying to access a physical hard disk as a stream of binary data in C. I've mounted an image (.img), and it's readable from the OS (Win 7). My C program simply attempts to open the physical drive in read-only binary mode, then read some data from the drive. However, if I simply read data from the stream without seeking anywhere, all is well, I get back the data that is stored in the drive, and as I'm at offset 0 in the stream, I'm able to read the MBR on the disk. However, if I

Do we need mutex to perform multithreading file IO

时光怂恿深爱的人放手 提交于 2020-01-06 07:27:12
问题 I'm trying to do random write (Benchmark test) to a file using multiple threads (pthread). Looks like if I comment out mutex lock the created file size is less than actual as if Some writes are getting lost (always in some multiple of chunk size). But if I keep the mutex it's always exact size. Is my code have a problem in other place and mutex is not really required (as suggested by @evan ) or mutex is necessary here void *DiskWorker(void *threadarg) { FILE *theFile = fopen(fileToWrite, "a+"

OggVorbis ov_open() Throws Access Violation Exception

落花浮王杯 提交于 2020-01-05 05:39:07
问题 I'm trying to open an OggVorbis file using the Vorbis SDK/DLLs. ov_open() throws an Access Violation exception 0x00000014 . I have checked the file exists: I've opened it with fopen and printed the contents to the console just to check - all went fine! Might not make a difference, but the DLLs have been compiled as DEBUG Win32 , and my project is compiling under the same configuration. I'm using an absolute path to the file, for reassurance, and the file does exist. As stated before, I can

C语言 FileStreaming fseek

a 夏天 提交于 2020-01-02 09:23:30
方法 描述 fgetpos Get current position in stream (function ) fsetpos Set position indicator of stream (function ) ftell Get current position in stream (function ) fseek Reposition stream position indicator (function ) rewind Set position of stream to the beginning (function ) ftell() 和 fseek() 用长整型表示文件内的偏移 (位置) fgetpos() 和 fsetpos() 函数使用 了一个特殊的类型定义 fpos_t 来表示偏移量 参考: http://blog.sina.com.cn/s/blog_ade902fe0101l4yd.html http://www.cplusplus.com/reference/cstdio/ http://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams 来源: CSDN 作者: Claroja 链接: https://blog.csdn.net