首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

这个程序虽然给出异常提示,小弟我依然不会改,请帮忙

2013-07-01 
这个程序虽然给出错误提示,我依然不会改,请帮忙!下面这个程序,是想把文件str2的内容追加到str1中去,str3作

这个程序虽然给出错误提示,我依然不会改,请帮忙!
    下面这个程序,是想把文件str2的内容追加到str1中去,str3作为中间传送的字符串数组。count来统计追加的文件的个数。用setvbuf创建一个1024字节的缓冲区。
    运行时错误提示为'fread' : cannot convert parameter 4 from 'char [1000]' to 'struct _iobuf *'和'fwrite' : cannot convert parameter 4 from 'char [1000]' to 'struct _iobuf *'。请问,怎样改动?

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define SIZE 1000

int main(void)
{
char str1[SIZE];
char str2[SIZE];
char str3[10];
FILE *in, *out; 
int count = 0;

printf("Please input the source file's name:");
gets(str1);
in = fopen(str1, "a+");
if((setvbuf(in, NULL, _IOFBF, 1024)) != 0)
{
fprintf(stderr, "Can not create buffer of &s", str1);
exit(2);
}

printf("Please input the destination file's name:");
while(gets(str2) != NULL && str2[0] != '\0')
{
if(strcmp(str1, str2) == 0)
{
fputs("Can not copy itself.\n", stderr);
exit(3);
}
else
{
if((setvbuf(out, NULL, _IOFBF, 1024)) != 0)
{
fputs("Failing in creating the buffer for the file.\n", stderr);
continue;
}
out = fopen(str2, "r");
if(out == NULL)
{
fputs("Failing in opening the file.\n", stderr);
}
fread(str3, sizeof(char), 10, str2);
fwrite(str3, sizeof(char), 10, str1);
}
count++;
}
fclose(in);
fclose(out);
return 0;
}

[解决办法]
     fread(str3, sizeof(char), 10, str2);
            fwrite(str3, sizeof(char), 10, str1);

最后一个参数,应该是文件指针 in,out
[解决办法]

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
 
#define SIZE 1000
 
int main(void)
{
    char str1[SIZE];
    char str2[SIZE];
    char str3[10];
    FILE *in, *out; 
    int count = 0;
 
    printf("Please input the source file's name:");
    fgets(str1, SIZE, stdin);
    in = fopen(str1, "a+");
    if((setvbuf(in, NULL, _IOFBF, 1024)) != 0)
    {
        fprintf(stderr, "Can not create buffer of &s", str1);


        exit(2);
    }
 
    printf("Please input the destination file's name:");
    while(fgets(str2, SIZE, stdin) != NULL && str2[0] != '\0')
    {
        if(strcmp(str1, str2) == 0)
        {
            fputs("Can not copy itself.\n", stderr);
            exit(3);
        }
        else
        {
            if((setvbuf(out, NULL, _IOFBF, 1024)) != 0)
            {
                fputs("Failing in creating the buffer for the file.\n", stderr);
                continue;
            }
            out = fopen(str2, "r");
            if(out == NULL)
            {
                fputs("Failing in opening the file.\n", stderr);
            }    
            fread(str3, sizeof(char), 10, in);
            fwrite(str3, sizeof(char), 10, out);
        }
        count++;
    }
    fclose(in);
    fclose(out);
    return 0;
}


[解决办法]
还有把gets改为fgets,因为gets是不安全的函数。

引用:
确实如楼上所说,参数弄错了。

热点排行