【发布时间】:2020-04-14 13:52:17
【问题描述】:
我有一个任务。我必须编写一个从文件读取并写入另一个文件并复制数组的程序。我必须使用指针。这是我的程序
#include <stdio.h>
#include <stdlib.h>
#include "eng_fun.h"
int _strlen (char *array) // here I check array length
{
int i;
for (i = 0; array[i] != '\0'; ++i);
return i;
}
int writeText(FILE *wp, char *s)
{
int sum = 0;
while((*s++ = fputc(*s, wp)))
{
sum++;
}
return sum; //I must return array length in my task, so I choose sum to count how many signs I put
}
int readText(FILE *wp, char *s, int max)
{
int sum = 0;
while((*s++ = fgetc(wp)) != EOF)
{
sum++;
}
return sum; //I must return array length in my task, so I choose sum to count how many signs I get
}
int copyText (char *s, char *t, int max)
{
if (_strlen(t) > max)
{
printf("This array is too big.");
}
else
{
while((*t++ = *s++));
}
return _strlen(t); //I must return array length in my task
}
int main (int argc, char *argv[])
{
int c, i;
char *s, *t, *r;
FILE *wz, *wc;
char arr[10000];
s = arr;
char copyArr[10000];
t = copyArr;
char keyArr[10000];
r = keyArr;
if (argc != 3) {
printf("Wrong arguments number\n");
printf("I should run this way:\n");
printf("%s source result\n",argv[0]);
exit(1);
}
if( (wz= fopen(argv[1],"r")) == NULL) {
printf("Open error %s\n", argv[1]);
exit(1);
}
if( (wc= fopen(argv[2], "w")) == NULL) {
printf("Open error %s\n", argv[2]);
exit(2);
}
fprintf(wc, "Write text from file source.txt:\n");
readText(wz, s, 10000);
writeText(wc, s);
fprintf(wc, "\nCopy and paste array:\n\n");
copyText(t, s, 10000); //robie kopie tablicy
writeText(wc, t); //wypisuje ją
return 0;
}
这是我的输入文件
A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations
automatically via computer programming. Modern computers have the ability to follow generalized sets of
operations, called programs. These programs enable computers to perform an extremely wide range of tasks.
这是输出,错了
Write text from file source.txt:
A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations
automatically via computer programming. Modern computers have the ability to follow generalized sets of
operations, called programs. These programs enable computers to perform an extremely wide range of tasks.
\FF\00
Copy and paste array:
\00
预期的输出是
Write text from file source.txt:
A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations
automatically via computer programming. Modern computers have the ability to follow generalized sets of
operations, called programs. These programs enable computers to perform an extremely wide range of tasks.
Copy and paste array:
A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations
automatically via computer programming. Modern computers have the ability to follow generalized sets of
operations, called programs. These programs enable computers to perform an extremely wide range of tasks.
您以这种方式运行程序: ./program.x source.txt 结果.txt
编辑
现在我的脚本可以工作了,但我在复制数组时仍然遇到问题。我尝试了这个解决方案,但没有奏效。
int copyText (char *s, char *t, int max)
{
if (_strlen(t) > max)
{
printf("This array is too big.");
}
else
{
while( (*s++ = *t++) != '\0');
}
return _strlen(t); //I must return array length in my task
}
【问题讨论】: