fgets

C++: Store read binary file into buffer

微笑、不失礼 提交于 2019-12-03 14:26:39
I'm trying to read a binary file and store it in a buffer. The problem is, that in the binary file are multiple null-terminated characters, but they are not at the end, instead they are before other binary text, so if I store the text after the '\0' it just deletes it in the buffer. Example: char * a = "this is a\0 test"; cout << a; This will just output: this is a here's my real code: this function reads one character bool CStream::Read (int * _OutChar) { if (!bInitialized) return false; int iReturn = 0; *_OutChar = fgetc (pFile); if (*_OutChar == EOF) return false; return true; } And this is

Difference between read() and fgets() in C

南笙酒味 提交于 2019-12-03 09:40:56
问题 I want to read from a stdin stream. Is there any difference in using read() or fgets() to read from the stdin stream. I am attaching the following two pieces of code with fgets and read. With fgets I can use a java program to write and read from the c program easily. With read and write my java program hangs waiting for the output from C program which does not come. I am just reading a line keeping it in buf and appending A to it. Java program is able to talk to the following program which

Does fgets() always terminate the char buffer with \0?

笑着哭i 提交于 2019-12-03 08:51:04
问题 Does fgets() always terminate the char buffer with \0 even if EOF is already reached? It looks like it does (it certainly does in the implementation presented in the ANSI K&R book), but I thought I would ask to be sure. I guess this question applies to other similar functions such as gets(). EDIT: I know that \0 is appended during "normal" circumstances, my question is targeted at EOF or error conditions. For example: FILE *fp; char b[128]; /* ... */ if (feof(fp)) { /* is \0 appended after

Serial comm with PHP on Windows

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am looking for a way to communicate with RS232 serial COM port on windows. I have found 2 solutions on the net, one which is not totally free (introduces deliberate delays on the function) and another with limited capability on Windows. The latter can only write to a COM port on Windows, not read. I can't look at the code of the first solution since it is compiled into a .dll (makes sense, otherwise people can just edit the delay and not purchase it...) and the second one seems only to use fopen() to open the port and later fwrite() to it

How to read non-ASCII characters from CLI standard input

别说谁变了你拦得住时间么 提交于 2019-12-03 07:11:43
问题 If I type å in CMD, fgets stop waiting for more input and the loop runs until I press ctrl-c . If I type a "normal" characters like a-z0-9!?() it works as expected. I run the code in CMD under Windows 7 with UTF-8 as charset ( chcp 65001 ), the file is saved as UTF-8 without bom. I use PHP 5.3.5 (cli). <?php echo "ÅÄÖåäö work here.\n"; while(1) { echo '> '. fgets(STDIN); } ?> If I change charset to chcp 1252 the loop doesn't break when I type å and it print "> å" but the "ÅÄÖåäö work here"

How to use fgets if you don't know the number of characters to be read?

99封情书 提交于 2019-12-03 06:57:38
I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets() , or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); Don't forget that fgets() reads a line at a time, subject to having enough space. Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I usually use: char buffer[4096]; while (fgets(buffer, sizeof(buffer), fp)) { ...process line... } If you are

How to read line by line after i read a text into a buffer?

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: First , I read a text into a buffer by calling fread, and then I want to read it line by line, how to do it? I try to use a sscanf , but it seems not to work. char textbuf[4096]; char line[256]; FILE *fp; fp = fopen(argv[1],"r"); memset(textbuf, 0, 4096); fread(textbuf, 1, 4096, fp); I know using fgets is a good way. I just want to know weather this method can do the same thing. 回答1: Try this: fgets(textbuf, sizeof(textbuf), fp); For read line by line you can use: fgets(line, 128, fp) or getline(&line, &size, fp); EDIT If you want to read it

How to use fgets if you don't know the number of characters to be read?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets() , or is there a better alternative? Note: char * fgets ( char * str , size_t num , FILE * stream ); 回答1: Don't forget that fgets() reads a line at a time, subject to having enough space. Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I usually use: char buffer [ 4096 ];

Using fgets to copy lines into string array?

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a C program that I'm trying to write which intends to reverse the lines of a file. I am still very inept at C (though I come from a Java background), so I am very likely making mistakes with pointers and such, but I've tried consulting the manual at every turn. This is somewhat of an assignment. The point of the program is to reverse a file's contents, up to MAX_LINES in number, each line being no longer than MAX_CHARS. The method I wanted to try is the following: use fgets to read from the file, 80 chars or until EOL, store that

strcmp will not correctly evaluate in if statements [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: strcmp on a line read with fgets 6 answers #include <stdio.h> #include <math.h> #include <string.h> #define size 7 int computeN(char s1[]) { int n=-1; if(strcmp(s1, "black") == 0) { n = 0; } else if (strcmp(s1, "brown") == 0) { n = 10; } else if (strcmp(s1, "red") == 0) { n = 20; } else if (strcmp(s1, "orange") == 0) { n = 30; } else if (strcmp(s1, "yellow") == 0) { n = 40; } else if (strcmp(s1, "green") == 0) { n = 50; } else if (strcmp(s1, "blue") == 0) { n = 60; } else if (strcmp(s1, "violet") ==