fgets

Is it possible to distinguish the error returned by fgets

不打扰是莪最后的温柔 提交于 2020-08-19 11:24:42
问题 Upon looking at the ISO C11 standard for fgets §7.21.7.2, 3, the return value is stated regarding the synopsis code: #include <stdio.h> char *fgets(char* restrict s, int n, FILE* restrict stream); The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer

How to convert string to unsigned long in C++? [duplicate]

。_饼干妹妹 提交于 2020-03-03 13:06:34
问题 This question already has answers here : C++ convert hex string to signed integer (8 answers) Closed 6 years ago . I need help in programming. The program has to accept a string that will eventually be turned to unsigned long. But here's the catch, there must be an error catcher that when you enter combination of hex and symbols like a!!!!!! will produce an error and the unsigned long variable must be able to accept and store the input greater that 4294967295 which is FFFFFFFF . I've tried

C语言 fgets

穿精又带淫゛_ 提交于 2020-02-27 19:25:50
C语言 fgets #include <stdio.h> char *fgets(char *s, int size, FILE *stream); 功能:从stream指定的文件内读入字符,保存到s所指定的内存空间,直到出现换行字符、读到文件结尾或是已读了size - 1个字符为止,最后会自动加上字符 '\0' 作为字符串结束。 参数: s:字符串 size:指定最大读取字符串的长度(size - 1) stream:文件指针,如果读键盘输入的字符串,固定写为stdin 返回值: 成功:成功读取的字符串 读到文件尾或出错: NULL fgets()在读取一个用户通过键盘输入的字符串的时候,同时把用户输入的回车也做为字符串的一部分。 通过scanf和gets输入一个字符串的时候,不包含结尾的“\n”,但通过fgets结尾多了“\n”。 fgets()函数是安全的,不存在缓冲区溢出的问题。 案例 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(void) { // fgets() // 可以接受空格 // 将用户输入的回车也会算作为字符串的一部分 //

fseek() by line, not bytes?

烂漫一生 提交于 2020-01-31 06:27:19
问题 I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed. Is this really the best / only way to seek to a specific line in a file? ( fseek() is not usable in my case.) <?php for ($i = 0; $i < 100000; $i++) fgets($fp); // just discard this I don't have a problem using this, it is fast enough - it just feels a bit dirty. From what I know about the underlying code, I don't imagine there is a better way

fseek() by line, not bytes?

跟風遠走 提交于 2020-01-31 06:26:16
问题 I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed. Is this really the best / only way to seek to a specific line in a file? ( fseek() is not usable in my case.) <?php for ($i = 0; $i < 100000; $i++) fgets($fp); // just discard this I don't have a problem using this, it is fast enough - it just feels a bit dirty. From what I know about the underlying code, I don't imagine there is a better way

fseek() by line, not bytes?

自闭症网瘾萝莉.ら 提交于 2020-01-31 06:25:11
问题 I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed. Is this really the best / only way to seek to a specific line in a file? ( fseek() is not usable in my case.) <?php for ($i = 0; $i < 100000; $i++) fgets($fp); // just discard this I don't have a problem using this, it is fast enough - it just feels a bit dirty. From what I know about the underlying code, I don't imagine there is a better way

pwn学习日记Day6 基础知识积累

人走茶凉 提交于 2020-01-26 05:47:25
知识杂项 ELF:在计算机科学中,是一种用于二进制文件、可执行文件、目标代码、共享库和核心转储格式文件。 char fgets(char buf, int bufsize, FILE stream); buf: 字符型指针,指向用来存储所得数据的地址。 bufsize: 整型数据,指明存储数据的大小。 *stream: 文件结构体指针,将要读取的文件流。 从文件结构体指针stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中,每次最多读取bufsize-1个字符(第bufsize个字符赋'\0'),如果文件中的该行,不足bufsize-1个字符,则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行,但是,缓冲区总是以NULL字符结尾,对fgets的下一次调用会继续读该行。函数成功将返回buf,失败或读到文件结尾返回NULL。因此我们不能直接通过fgets的返回值来判断函数是否是出错而终止的,应该借助feof函数或者ferror函数来判断。 FILE * fopen(const char * path, const char * mode); 参数 path字符串包含欲打开的文件路径及文件名,参数 mode 字符串则代表着流形态。 mode 有下列几种形态字符串: r 以只读方式打开文件,该文件必须存在

Reading multiple lines with different data types in C

白昼怎懂夜的黑 提交于 2020-01-25 19:59:06
问题 I have a very strange problem, I'm trying to read a .txt file with C, and the data is structured like this: %s %s %d %d Since I have to read the strings all the way to \n I'm reading it like this: while(!feof(file)){ fgets(s[i].title,MAX_TITLE,file); fgets(s[i].artist,MAX_ARTIST,file); char a[10]; fgets(a,10,file); sscanf(a,"%d %d",&s[i].time.min,&s[i++].time.sec); } However, the very first integer I read in s.time.min shows a random big number. I'm using the sscanf right now since a few

fgets not reading whole line

余生长醉 提交于 2020-01-17 03:58:04
问题 I have a simple function, which is supposed to read line from standard input and put it into an char array, and I call this function in a loop till EOF is inputed. The problem is, that for extremely long lines (more than 10k characters) the fgets reads only a number of characters and stops, although it has not encountered any \n and the buffer has sufficient space, therefore next invoking of this function reads the rest of the line. Is there a reason for this behaviour (wrongly written code,