fgets

Ignoring whitepace with fscanf or fgets?

谁说胖子不能爱 提交于 2019-12-01 20:52:53
I was wondering if there is any way to ignore whitespace with either the fscanf or fgets function. My text file has two chars on each line that may or may not be separated by a space. I need to read the two chars and place each one in a different array. file = fopen(argv[1], "r"); if ((file = fopen(argv[1], "r")) == NULL) { printf("\nError opening file"); } while (fscanf(file, "%s", str) != EOF) { printf("\n%s", str); vertexArray[i].label = str[0]; dirc[i] = str[1]; i += 1; } Using a space ( " " ) in the fscanf format causes it to read and discard whitespace on the input until it finds a non

Issue in C language using 'fgets' after 'printf' as 'fgets' runs before 'printf' [duplicate]

喜欢而已 提交于 2019-12-01 20:48:03
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why does printf not flush after the call unless a newline is in the format string? (in C) I am getting a problem using printf and fgets as in my code printf is written earlier then fget but it does not run, it runs after fgets runs. enum { max_string = 127 }; static char string[max_string+1] = ""; int main( int argc, char ** argv ) { printf("Type a String: "); fgets(string, max_string, stdin); printf("The String

Fgets skipping inputs [duplicate]

眉间皱痕 提交于 2019-12-01 19:55:36
This question already has an answer here: fgets doesn't work after scanf 7 answers I've tried looking around and I can't seem to find where the error lies. I know it must have something to do with the way I used fgets but I can't figure out for the life of me what it is. I've read that mixing fgets and scanf can produce errors so I've even changed my second scanf to fgets and it still skips the rest of my inputs and only prints the first. int addstudents = 1; char name[20]; char morestudents[4]; for (students = 0; students<addstudents; students++) { printf("Please input student name\n"); fgets

how to read a string from a \\n delimited file

点点圈 提交于 2019-12-01 17:36:39
I'm trying to read a return delimited file. full of phrases. I'm trying to put each phrase into a string. The problem is that when I try to read the file with fscanf(file,"%50s\n",string); the string only contains one word. when it bumps with a space it stops reading the string fscanf can be modified to read past spaces. The details are a bit complicated. Here is what the man page says about %[...] Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the

C之输入输出函数(2) -- gets()

帅比萌擦擦* 提交于 2019-12-01 16:09:00
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/gets.htm#gets #include <stdio.h> char *gets(char *buffer); 从标准输入设备读入一行字符,直至读到换行符或者文件结束符为止,但不会把换行符或者文件结束符读入相应内存。 当读到换行符时,在相应内存处添加’\0’封印字符串。 警告:按照美国国土安全局的建议,永远不要使用gets()函数,因为它不会控制读入的字符数量。 https://www.us-cert.gov/bsi/articles/knowledge/coding-practices/fgets-and-gets_s 使用fgets()替代gets()。 char *fgets(char *string, int n, FILE *stream); 对于标准输入输出设备,这样使用即可: fgets(char *string, int n, stdin); 来源: https://www.cnblogs.com/freshair_cnblog/p/11692037.html

C之输入输出函数(1) -- fgets()

依然范特西╮ 提交于 2019-12-01 16:03:37
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/fgets.htm #include <stdio.h> char *fgets(char *string, int n, FILE *stream); -- string: 指向放置输入数据的内存 -- n: 指示最多读取多少数据 -- stream: 指向要读的文件 如果操作成功,fgets()返回指向存储数据区的指针;如果操作失败或者读到文件结束符,返回NULL。用feof()或ferror()判断是读取失败还是读到文件结束符。 fgets()的特别之处在于: (1) 按照行读; (2) 会把换行符‘\n’读进来; (3) 会在结束前加字符’\0’; (4) 函数操作结束于: ---- 读到换行符; 或者 ---- 读完指定数量的字符; 因为最后一定要添加一个’\0’字符,因此读到的最多字符数量是(n-1)。 来源: https://www.cnblogs.com/freshair_cnblog/p/11691779.html

Read file lines backwards (fgets) with php

霸气de小男生 提交于 2019-12-01 15:33:00
问题 I have a txt file that I want to read backwards, currently I'm using this: $fh = fopen('myfile.txt','r'); while ($line = fgets($fh)) { echo $line."<br />"; } This outputs all the lines in my file. I want to read the lines from bottom to top. Is there a way to do it? 回答1: First way: $file = file("test.txt"); $file = array_reverse($file); foreach($file as $f){ echo $f."<br />"; } Second Way (a): To completely reverse a file: $fl = fopen("\some_file.txt", "r"); for($x_pos = 0, $output = '';

C, how to use fgets and fscanf together

倖福魔咒の 提交于 2019-12-01 13:20:39
i hav a homework form univ. that is use file IO. there is like this TXT file: Brian s213551 50 70 70 50 Alex Fernandes s210011 70 81 50 89 Young Lee s211213 60 80 60 90 ... and more I have to read this file and save to var in struct. and prof. said to me. I have to use fgets and fscanf together. if I use only fscanf its not working nicely because "Alex Fernandes" has space in it. but even though I use fgets and fscanf together, its not working.. so i need help u. this is my source: #include <stdio.h> #include <stdlib.h> typedef struct sMember{ char name[10]; char id[10]; int score[4]; double

strcmp will not correctly evaluate in if statements [duplicate]

早过忘川 提交于 2019-12-01 12:51:39
问题 This question already has answers here : strcmp on a line read with fgets (6 answers) Closed 4 years ago . #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

fgets and dealing with CTRL+D input

删除回忆录丶 提交于 2019-12-01 09:03:49
I am grabbing some standard input from the user and if the user presses CTRL+D , I want to display an error and terminate the program. I think perhaps my issue may be related to being stuck in a while loop; int readInput(){ char buff[10]; int count = 0; int counter; printf("Enter random number: "); fgets(buff, 10, stdin); if ((int) strtol(buff, NULL, 10) == 0){ printf("Error reading number. \n"); return 0; //This will get hit if the user presses CTRL+D at this input. } counter = atol(buff); while (count < counter){ printf("Enter a label: "); fgets(buff, 10, stdin); if ((int) strtol(buff, NULL,