Tag: scanf

如何从文件中读取特定格式的数据?

我应该从类似于这种格式的文件中读取输入和参数: Add id:324 name:”john” name2:”doe” num1:2009 num2:5 num2:20 问题是我不允许使用fgets。 我尝试使用fscanf,但不知道如何忽略“:”并分隔字符串’name:“john”’。

循环在第一次之后跳过scanf语句

这是main()的代码: int main (void) { float acres[20]; float bushels[20]; float cost = 0; float pricePerBushel = 0; float totalAcres = 0; char choice; int counter = 0; for(counter = 0; counter < 20; counter++) { printf("would you like to enter another farm? "); scanf("%c", &choice); if (choice == 'n') { printf("in break "); break; } […]

程序正在跳过fgets而不允许输入

基本上如标题所示..当我的程序从控制台运行时,它会询问你是否要加密或解密..当我输入e或E时,它会创建一个新的空行(直到我输入一些那种文字),然后一次显示“输入文字”和“输入密钥”行。 因此,在控制台中它看起来像: 你想(E)ncrypt还是(D)ecrypt? Ë asdf jkl; <—-随机用户输入以使程序继续.. 输入要加密的文本:输入用于加密的密钥:(用户输入) 然后程序退出.. //message to be encrypted char text[250]; //word to use as the key char key[50]; //stores the encrypted word char encrypted[250]; char answer; printf(“Would you like to (E)ncrypt or (D)ecrypt? “); scanf(” %c”, &answer); if(answer == ‘e’ || answer == ‘E’) { printf(“Enter the text you want […]

为什么scanf在按Enter键时将控制权返回给程序?

我写了以下程序。 void main() { int *piarrNumber1 = (int *) calloc(1, sizeof(int)); int iUserInput = 0; scanf(“%d”, &iUserInput); piarrNumber1[(sizeof piarrNumber1 / sizeof(int)) – 1] = iUserInput; printf(“\n%d\n”, piarrNumber1[0]); } 我从键盘输入“3”后跟一个TAB。 什么都没发生。 然后,我按Enter键。 我打印“3”,程序结束。 如果“TAB”[Horizanotal Tab]和“Enter”[Newline]都是空白字符,为什么他们的行为不同?

如何通过终端读取段落作为C中的单个字符串?

我正在编写一个C程序,应该在用户的文章中阅读。 论文分为多段。 我不知道文章会有多少行或字符,但我知道它以哈希符号( # )结尾。 我想只使用尽可能多的内存来保存文章。 这是我到目前为止所尝试的: #include #include #include main(){ int size; char *essay; printf(“\n how many characters?\n”); scanf(“%d”, &size); essay =(char *) malloc(size+1); printf(“Type the string\n”); scanf(“%s”,essay); printf(“%s”,essay ); } 正如我之前所说,我事先并不知道(也不想问)关于字符的数量。 如何动态分配内存以节省空间? (什么是动态内存分配?)是否存在另一种节省不依赖动态分配的内存的方法? 另外,我的代码现在一次只能读取一行。 如何读取多行并将它们存储为单个字符串? 这是另一个代码 #include #include int main () { char input; int count = 0; int n; char* characters= NULL; […]

为什么交错的scanf()+ printf()语句导致两个scanf()调用先执行,然后两个printf()调用?

您可以在以下代码中解释一件事: #include int main() { int n;char ch,ch1; scanf(“%d\n”,&n); printf(“d-%d \n”,n); scanf(“\n%c”,&ch); printf(“ch-%d \n”,ch); scanf(“\n%c”,&ch1); printf(“ch1-%d \n”,ch1); printf(“%d %d %d\n”,n,ch,ch1); return 0; } 为什么在输入n的值之后,它直接要求ch的值然后直接执行语句来打印它们各自的值,即语句: printf(“d-%d \n”,n); printf(“ch-%d \n”,ch);

在c中使用scanf读取浮点数

我有一个包含浮点变量的结构, struct MyStruct{ float p; }newMyStruct; 我正在使用scanf读取一个值 int main(){ scanf(“%f”,&(newMyStruct.p)); } 问题是当我使用printf(“%f”,newMyStruct.p)打印它时printf(“%f”,newMyStruct.p)它打印’0.000000’。 另外我得到一个警告,说格式是双重的,而格式期望它是浮点数(警告scanf(“%f”,&(newMyStruct.p));语句)。当我将scanf()语法更改为scanf(“%0f”,&(newMyStruct.p)); , printf(“%0f”,newMyStruct.p); 正确打印浮点值但编译器给出了另一个警告(与精度为0的相关内容)。 printf(“%2f”,newMyStruct.p)也以其他格式打印浮点数。 所以,我的问题是我如何摆脱所有这些警告,并阅读一个适当的浮动变量,也可以正确打印。 我无法访问我通常编码的笔记本电脑,因此我无法提供正确的警告。

使用带有%i的scanf捕获日期时的问题(mm dd yyyy)

我正在编写一个计算两个日期之间经过的天数的程序。 为此,我必须在如下定义的结构中将日期作为整数输入: struct date { int month; int day; int year; }; 现在,我已经想出了如何运行我的程序,但是当月或日是单个数字时,用户无法以(mm dd yyyy)格式输入日期,这真让我烦恼。 让我告诉你我的代码(请不要介意调试printf语句): printf(“Enter your start date (mm dd yyyy)\n”); scanf(“%i %i %i”, &startDate.month, &startDate.day, &startDate.year); // DEBUG printf(“Start date = %i/%i/%i\n”, startDate.month, startDate.day, startDate.year); // DEBUG printf(“start date month = %i”, startDate.month); printf(“Enter your end date (mm dd yyyy)\n”); scanf(“%i %i […]

sscanf函数更改另一个字符串的内容

我在使用sscanf读取字符串时遇到问题。 我已经愚弄了代码以专注于问题。 下面是整个代码中的一个函数,它应该打开一个文件并读取一些内容。 但是sscanf表现得很奇怪。 例如,我声明一个名为atm的字符串,其内容为’ATOM’ 。 在sscanf之前,它将此字符串作为ATOM打印,而在它之后为空。 可能是什么问题呢? 我认为它必须是分配问题,但我找不到它。 我尝试了其他主题的一些建议,比如用其他东西替换%s ,但它没有帮助。 void Get (struct protein p, int mode, int type) { FILE *fd; //input file char name[100]=”1CMA”; //array for input file name char string[600]; //the array where each line of the data file is stored when reading char atm[100]=”ATOM”; char begin[4]; int index1 =0; fd […]

当整数的scanf获得一个字符时如何处理exception

当输入是一个字符时,下面的简单程序会给出无限循环,尽管它意味着从数字中告诉一个字符。 如何使用scanf的返回值测试scanf是否应该是一个数字? #include int main() { int n; int return_value = 0; while (!return_value) { printf(“Input a digit:”); return_value = scanf(“%d”, &n); } printf(“Your input is %d\n”, n); return 0; }