2012-07-18 87 views
1

我已经编写了下面的程序,它在从IDE运行时运行良好。但是,当我想通过从inp.txt文件输入并输出到out.txt文件来测试它时,它不会这样做。为什么我的代码不输出到文本文件


#include <stdio.h> 
#include <stdlib.h> 

struct node 
{ 
    int data; 
    struct node *next; 
}*start; 

void insertatend(int d) 
{ 
    struct node *n; 
    n=(struct node *)malloc(sizeof(struct node)); 
    n->data=d; 
    n->next=NULL; 

    if(start==NULL) 
    { 
    start=n; 
    } 

    else 
    { 
    struct node *tmp; 
    for(tmp=start;tmp->next!=NULL;tmp=tmp->next); 
    tmp->next=n; 
    } 
} 

int max(int a,int b) 
{ 
    int c=(a>b)?a:b; 
    return c; 
} 

int maxCoins(int n) 
{ 
    int arr[n+1],i; 
    arr[0]=0; 
    arr[1]=1; 
    arr[2]=2; 
    arr[3]=3; 

    if(n>2) 
    { 


    for(i=3;i<=n;i++) 
    { 
     int k= arr[(int)(i/2)]+arr[(int)(i/3)]+arr[(int)(i/4)]; 
     arr[i]=max(i,k); 
    } 
    } 

    return arr[n]; 
} 

int main(void) 
{ 
    int coins,i; 
    start=NULL; 
    struct node*p; 

    while(scanf("%d",&coins)>0) 
    { 
    insertatend(coins); 
    } 

    for(p=start;p!=NULL;p=p->next) 
    { 
    printf("%d\n",maxCoins(p->data)); 
    } 

    getchar(); 

    return 0; 
} 

我试着做我的命令提示符下ByteTest.exe<inp.txt>out.txt以下,但不更改out.txt文件进行。

我通过输入CTRL+Z终止输入到我的程序。这与这有什么关系?


inp.txt and out.txt可以,例如含有


inp.txt  out.txt 

12    13 
24    27 
26    27 
+1

你为什么期望你的程序输出到一个文件?您的程序中没有使用任何文件访问/写入功能?你是否期望它能像shell命令行'ls -a> file_list.txt'那样通过管道输出工作? – mathematician1975 2012-07-18 20:48:01

+0

是的。我正在尝试这样的事情 – OneMoreError 2012-07-18 20:51:50

+0

我没有看到你如何做事情的任何问题。 – pb2q 2012-07-18 20:56:07

回答

2

你的问题可能是:

while(scanf("%d",&coins)>0) 

这将返回字符的数量。您不在此处检查硬币的值,而是检查输入字符串的长度。

相关问题