fscanf相关的有关问题
发布时间:2011-06-28 13:57:59 文章来源:www.iduyao.cn 采编人员:星星草
fscanf相关的问题求助
写了一段代码,是从一个文件中读取一段表达式并计算,读取的很简单,就是:12+23
在linux下的GCC编译运行成功的,但是结果不对,不是35,是很大的数
代码如下:
#include <stdio.h>
int main(void) {
char* input = "input";
char* output = "output";
int input1 = 0;
int input2 = 1;
char s;
int result;
FILE *fp1,*fp2;
fp1 = fopen(input, "r");
if(fscanf( fp1, "%d %s %d", &input1, &s, &input2 )) //这里的问题,&input2输入不对,是一个很大的数,不 是23
switch(s) {
case '+': result = input1 + input2; break;
case '-': result = input1 - input2; break;
case '*': result = input1 * input2; break;
case '/': if(input2) result = input1/input2;
else exit(1);
break;
case '%': if(input2) result = input1%input2;
else exit(2);
break;
default: exit(3);
}
else {
printf("error of fscanf\n");
exit(4);
}
if(fp2 = fopen(output, "w"))
fprintf( fp2, "result is %d", result );
else {
printf("error of fprintf\n");
exit(5);
}
fclose(fp1);
fclose(fp2);
return 0;
}
经过调试发现时fscanf函数中,前两个读取都正确,是12和+号,第三个数读取错误,不知道什么原因,请大家帮助,不胜感激。
------解决方案--------------------
要加空格
12 + 23
------解决方案--------------------
fscanf( fp1, "%d %s %d", &input1, &s, &input2 )
"%d %s %d"这个格式跟输入要一致
------解决方案--------------------
fscanf( fp1, "%d%s%d", &input1, &s, &input2
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
【★C/C++奖励基金-30期★】更新获奖书籍,请选择~ - C/C++ / 非技术区
【★C/C++奖励基金-30期★】
C/C++ 2011-12专家榜
名次...
-
C/c++ 如何按位拷贝呢?
我有一个结构体
strut st_header
{
unsigned short ver:2
unsigned short ping:1
unsgne...
-
uboot上的一点代码,没看明白!
struct in_str {
const char *p;
#ifndef __U_BOOT__
char peek_buf[2];
#endif
int __promptme;
...
-
Debug时为什么变量的地址不变?
是巧合还是某种必然. 是不是因为C中生成的可执行程序存储的地址是相对地址而不是绝对的物理地址.
...
-
关于switch语句。我不知道哪里错了,大侠帮忙bug一下
#include <stdio.h>
int main()
{
int a;
char b;
do{
printf("1.Chines...
-
#pragma section 的 $ 语法
在 ARX 头文件中间过如下三行,放在一起:
#pragma section("ARXCOMMAND$__a")
#pragma section("ARXCOMM...
-
read 问题再现
#define N 205
signed short x[N];
for(i=0;i<N;i++)
{
printf("hello boy!!\n");
...
-
征求一个C语言输入函数?
不知道大家有没有学习过Java? 现在需要一个类似Java的Scanner的函数集, 要求如下:
1. 三个函数: int rea...
-
求一道算法。一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
/*我写了一点,但是有错误,汗*/
/*
...
-
3n+1问题
求救,不知道错在哪里
#include<stdio.h>
int count(int a,int b)
{
int max=0,len=0,a1;
while(a<=b)
{
...