fgets

fgets注意事项

时光怂恿深爱的人放手 提交于 2019-11-28 01:19:49
这是yjy的练习题,中途我在使用fgest时颇费了一点心思,特此记录一下。 #include <stdio.h> #include <string.h> #include <stdlib.h> int main (void) { char (*arry)[6]; int n,tmp,i,j; char *find; scanf("%d",&n); getchar();/* sancf 输入的时候会有换行符输入,该函数是为了吃掉\n,因为fegets遇到\n就结束*/ arry=(char (*)[6])malloc(n*sizeof(char)*6); for(i=0;i<n;i++) { /* fgets 读到count-1个字符或者遇到\n就终止,所以这里选择6 * 6-1=5 由于只会输入4个有用字符(例如为abcd),最后会有\n,此时字符数组中有 * 我输入的4个字符a b c d + \n \0 有\n是因为fgets要吸收输入时的\n,且fgets会 * 自动在最后加上\0。 * NOTE:使用fegts,输入第二个参数时,建议为输入的有效字符+2,例如我这里要输入4个 * 有效字符,为了容错\n和\0 */ if(fgets(arry[i],6,stdin)==NULL) { return -1; } if((find=strchr(arry[i],'\n'))!

Use of undefined constant STDIN - assumed 'STDIN' in C:\\wamp\\www\\study\\sayHello.php on line 5

孤人 提交于 2019-11-27 23:16:19
I want to Learn php & mySQL and I purchased a book (php&mySql: the missing manuals 2edition) I installed Wampserver2.4 on win8 64bit machine. Server Configuration Apache Version : 2.4.4 PHP Version : 5.4.12 in first lesson i got this error :( Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5 this is the php code on file "sayHello.php" <?php echo "Hello there. So I hear you're learning to be a PHP programmer!\n"; echo "Why don't you type in your name for me:\n"; $name = trim(fgets(STDIN)); echo "\nThanks, " . $name . ", it's really nice to

【PAT】A1040 Longest Symmetric String【最长回文子串】

a 夏天 提交于 2019-11-27 15:45:27
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. Input Specification: Each input file contains one test case which gives a non-empty string of length no more than 1000. Output Specification: For each test case, simply print the maximum length in a line. Sample Input: Is PAT&TAP symmetric? Sample Output: 11 题意 求最长回文串的长度。 思路 动态规划写法。具体总结待整理//TODO大法。 要注意fgets需要读取到换行符,所以fgets的最大长度需要比一行的字符数多1,否则读取不到换行符会出错。 代码 # include <cstdio> # include

Reading from file using fgets

允我心安 提交于 2019-11-27 15:05:20
I am reading from file of format 1 32 43 23 32 43 123 43 54 243 123 2222 2 Here is my code snippet. string[100]; while(!feof(fp)) fgets(string,100,fp) Now, when I put every string, in the last string I am getting repetition and some more ambiguities (like something else also gets printed say 123 or so). How to solve this problem? dasblinkenlight You need to check the return value of fgets . If a read has been successful, fgets returns the pointer to the buffer that you passed to it (i.e. string in your example). If the End-of-File is encountered and no characters have been read, fgets returns

How to fgets() a specific line from a file in C?

眉间皱痕 提交于 2019-11-27 09:32:11
So, I'm trying to find a way to fgets() a specific line in a text file in C, to copy the contents of the line into a more permanent buffer: Essentially, I was wondering if there was a way to do that without something similar to the following code: FILE *fp; fp = fopen(filename, "r"); char line[256]; char * buffer; int targetline = 10; while( targetline > 0) { fgets(line, 256, fp) } buffer =(char*)malloc(sizeof(char) * strlen(line)); strcpy(buffer, line); So basically I don't want to iterate through the file n-1 times just to get to the nth line... it just doesn't seem very efficient (and, this

how to prevent fgets blocks when file stream has no new data

岁酱吖の 提交于 2019-11-27 09:26:56
I have a popen() function which executes "tail -f sometextfile". Aslong as there is data in the filestream obviously i can get the data through fgets(). Now, if no new data comes from tail, fgets() hangs. I tried ferror() and feof() to no avail. How can i make sure fgets() doesn't try to read data when nothing new is in the file stream? One of the suggestion was select(). Since this is for Windows Platform select doesn't seem to work as anonymous pipes do not seem to work for it (see this post ). DGentry In Linux (or any Unix-y OS), you can mark the underlying file descriptor used by popen()

fgets() not waiting for input

99封情书 提交于 2019-11-27 08:42:23
问题 I wrote the following code: int N; scanf("%d", &N); int i; for (i = 0; i < N; i++) { char line[LINE_MAX]; if (fgets(line, LINE_MAX, stdin) != NULL) { // do stuff with line here printf("%c - %c\n", line[0], line[1]); } } I have an input file which has the number of lines it has, and then that number of lines followed which I want to process. So I read in the number of lines into N . After that, I use fgets to get the line to be able to process it. However, fgets does not seem to wait for a

Using fgets(char* c, int i, file* f) with printf() for C in Eclipse - CDT. The order of output is not correct.!

送分小仙女□ 提交于 2019-11-27 08:19:20
问题 #include <stdio.h> enum { max_string = 127 }; static char ch[max_string+1] = ""; int main(int argc, char ** argv){ printf("Type a String: \n"); fgets(ch, max_string, stdin); printf("the string is: %s", ch); return 0; } I have used this code and the output in the console was hello world Type a String: the string is: hello world 'hello world' is the input which I give. My question is why isn't the order not maintained in this case. As printf() should work before fgets(), but here it isn't that

Difference between scanf() and fgets()

牧云@^-^@ 提交于 2019-11-27 07:25:28
I want to know what is the difference between fgets() and scanf() . I am using C as my platform. There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads 'a line of text' from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a line of data and then use sscanf() to dissect it. daniel int scanf(const char * restrict format, ...); scanf(3) searches for certain pattern defined by the format argument on the given input

strstr not functioning

僤鯓⒐⒋嵵緔 提交于 2019-11-27 05:12:23
Why does this particular piece of code return false on the strstr() if I input "test"? char input[100]; int main() { fgets(input, 100, stdin); printf("%s", input); if(strstr("test message", input)) { printf("strstr true"); } } I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets. It's because fgets stores the newline character so when strstr does a comparison it fails. From the man page: fgets() reads in at most one less than size characters from stream