scanf

Reading input from getchar() giving unexpected results

限于喜欢 提交于 2020-05-31 11:43:16
问题 I have a function which reads PIN from terminal and stores PIN and PIN length in the variables passed. On the first call to the function I get expected PIN and PIN length entered. However, during the second call to this function the first character is omitted. /* * Function : read_pin(char *pin,int *pin_len) * Description : Read entered PIN and stores PIN in pin and pin length in pin_len */ int read_pin(unsigned char *pin,unsigned int *pin_len) { int err = EXIT_SUCCESS; char ch; fflush(stdout

What does scanf(“%*[\n] %[^\n]”, input_string); do?

淺唱寂寞╮ 提交于 2020-05-23 10:53:07
问题 I am not able to understand the difference. I use %[^\n]s , for taking phrases input from the user. But this was not working when I needed to add two phrases. But the above one did. Please help me understanding me the difference. 回答1: The %[\n] directive tells scanf() to match newline characters, and the * flag signals that no assignment should be made, so %*[\n] skips over any leading newline characters (assuming there is at least one leading \n character: more on this in a moment). There is

Reading line by line using fscanf

家住魔仙堡 提交于 2020-05-16 04:58:07
问题 I want to read a file with 3 lines: The first one with strings, second with a number and the third with strings again. Example: Line 1: bird toy book computer water Line 2: 2 Line 3: toy water I have this code, that reads a file, word by word storing them in the word array, and then putting the word into the words 2d array. char words [5][50]; char word [50]; int i,j; j = 0; while( (fscanf(file, "%s", word))!=EOF ){ for(i = 0; i<50; i++){ if(word[i] != NULL){ words[j][i] = word[i]; } else{

C infinite loop when char input instead of int

北城余情 提交于 2020-05-15 05:42:15
问题 I have a C program which is supposed to validate that the input from the user is an int between 1 and 8. It works if an integer is entered, but when characters are entered the validation loop repeats forever. Can you tell what I am doing wrong? #include <stdio.h> int main(void) { int i; int input; domainEntry *myDomains = buildDomainDB(); printf("You have the choice between the" " following top domains: 1-EDU, 2-COM" ", 3-ORG, 4-GOV, 5-MIL, 6-CN, 7-COM.CN, 8.CAN\n"); printf("Which one do you

Is scanf(“%4s%4s”, b, b); well-defined? [duplicate]

旧时模样 提交于 2020-05-14 18:42:05
问题 This question already has answers here : Is scanf(“%d%d”, &x, &x) well defined? (3 answers) Closed 7 days ago . #include <stdio.h> int main(void) { char b[5]; scanf("%4s%4s", b, b); //My input: "qwer<Enter>sgsh<Enter>" printf("%s", b); //Output: sgsh } C99: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. In this case, I am modifying the value of b twice . Isn't it undefined behavior ? 回答1: From this

c sscanf with character delimiter

痴心易碎 提交于 2020-05-08 15:41:47
问题 I have an input string of the form char *s = "one.two three" and I want to separate it into 3 string variables. I'm doing sscanf(s, "%s.%s %s", one, two, three); but it's reading in "one.two" as the string for variable one. How do I handle the "." and the whitespace with sscanf? 回答1: The %s specifier only stops for a space. Try something like: sscanf(s, "%[^.].%s %s", one, two, three); Interestingly, it's likely impossible to produce an acceptable input for "%s.%s %s" since %s only stops for

C - Get random words from text a file

邮差的信 提交于 2020-05-08 15:30:23
问题 I have a text file which contains a list of words in a precise order. I'm trying to create a function that return an array of words from this file. I managed to retrieve words in the same order as the file like this: char *readDict(char *fileName) { int i; char * lines[100]; FILE *pf = fopen ("francais.txt", "r"); if (pf == NULL) { printf("Unable to open the file"); } else { for (i = 0; i < 100; i++) { lines[i] = malloc(128); fscanf(pf, "%s", lines[i]); printf("%d: %s\n", i, lines[i]); }

scanf and newlines in c [duplicate]

我的梦境 提交于 2020-04-11 04:24:09
问题 This question already has answers here : What is the effect of trailing white space in a scanf() format string? (4 answers) Closed 3 years ago . I just had a test in my C class today and I have reason to believe that answer might be incorrect. scanf("%d\n", &x); // Evaluate the expression for the string "54321\n" The idea is pretty simplistic. Find an integer and place the scanned number at the memory location corresponding with integer variable x . However, I don't believe that this call to

Bellman-Ford&&spfa(判负环)

主宰稳场 提交于 2020-04-08 04:35:23
题目链接 题意 : 有n个农场,m条双向路径u,v,t表示从u农场到v农场要花t时间,w个虫洞u,v,t,表示从u穿越到v时间倒流t。 问从任意一点出发,再回到出发点,能否在出发前时间到达出发点(时间倒流)。 解法 : Bellman-Ford算法(O(VE)) 算法核心 : 对所有边进行V-1次松弛操作,每一次松弛操作最少确定一点到源点的最短路径,所以最多v-1次可求出所有点到源点的最短路径。 最少一次就可以确定所有点的最短路径,即一条链依次更新。 判负环:如果存在负权环,则不存在最短路径。可以一直进行松弛操作。所以第V次对所有边进行松弛时,可以判断是否存在负权。 学习博客 //#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> #include <stdlib.h> using namespace std; typedef

车站售票系统

妖精的绣舞 提交于 2020-04-07 19:38:36
#include <windows.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define EmFile "C:\\zm.txt" //密码帐号文件名 #define N 3 #define TEMP 2 #define TRUE 1 #define FALSE 0 int shoudsave=0 ; int count1=0,count2=0,mark=0,mark1=0 ; struct train { char num[10];//列车号 char city[10];//目的城市 char takeoffTime[10];//发车时间 char receiveTime[10];//到达时间 int price;//票价 int bookNum ;//票数 int leftbookNum; }; //订票人的信息 struct man { char num[10];//ID char name[10];//姓名 int bookNum ;//需求的票数 char password[7];//密码 int tag; }; //定义火车信息链表的结点结构 typedef struct node { struct train data ; struct node