c - 指针 - 在两个文件中读取、写入、复制 c 中的文本
问题描述
我有一个任务。我必须编写一个程序,从文件(source.txt)中获取文本并将它们放入数组中,然后将此数组写入另一个文件(result.txt),然后将数组复制到另一个数组并将此数组放入result.txt 然后我从键盘中取出字符并将它们放入数组和 txt 文件中。我必须在单独的文件中创建函数,并且必须在POINTERS上工作。我的程序编译没问题,但是在result.txt里面什么都没有,不知道为什么?这是我的主程序 eng.c
#include <stdio.h>
#include <stdlib.h>
#include "eng_fun.h"
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, "Read text from file source.txt and write into result.txt:\n");
readText(wz, s, 10000);
writeText(wc, s);
textStats(wc, s);
fprintf(wc, "\nCopy and paste array and write into result.txt:\n\n");
copyText(s, t, 10000);
writeText(wc, t);
textStats(wc, t);
fprintf(wc, "\nText from keyboard to array and then to file result.txt\n\n");
printf("Write some sentence and press CTRL+D. All text will write to result.txt\n");
while( (*r++ = getchar()) != EOF )
writeText(wc, r);
textStats(wc, r);
int fclose(FILE *wz);
int fclose(FILE *wc);
return 0;
}
这是我的功能 eng_fun.c 的程序
#include <stdio.h>
#include <stdlib.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
}
int copyText (char *s, char *t, int max)
{
if (_strlen(t) > max)
{
printf("This array is too big.");
}
else
{
while((*s++ = *t++));
}
return _strlen(t); //I must return array length in my task
}
int allSigns(char *s) // count all signs
{
int sum = 0;
while (*s++ != '\0')
{
sum++;
}
return sum;
}
int notWhiteSigns(char *s) //count notWhiteSigns
{
int sum = 0;
while (*s++ != '\0')
{
if ((*s != ' ') && (*s != '\n') && (*s != '\t') && (*s != '\v'))
sum++;
}
return sum;
}
int lines(char *s) // count lines
{
int sumLines = 1;
while (*s++ != '\0')
{
if (*s++ == '\n')
sumLines++;
}
return sumLines;
}
int words(char *s) // here I count words
{
int sum = 0;
*s++;
while (*s != '\0')
{
if (*s == ' ' && *(s-1) != ' ' && *s != ('\n' || '\t' || '\v') && (*(s-1) != ('\n' || '\t' || '\v')))
sum++;
}
if (*s-1 != ' ');
sum++;
return sum;
}
int textStats(FILE *wp, char *s) // (here I write stats)
{
fprintf(wp, "All signs: %i\n not white signs: %i\n words: %i\n lines: %i\n\n\n\n", allSigns(s), notWhiteSigns(s), words(s), lines(s));
return 0;
}
和 eng_fun.h 文件
#ifndef A_A
#define A_A
int _strlen (char *tab);
int writeText(FILE *wp, char *s);
int readText(FILE *wp, char *s, int max);
int copyText(char *s, char *t, int max);
int allSigns(char *s);
int notWhiteSigns(char *s);
int lines(char *s);
int words(char *s);
int textStats(FILE *wp, char *s);
#endif
我的源.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.
并且开头的文件result.txt是空的
我以这种方式编译我的文件
gcc -c eng.c
gcc -c fun_eng.c
gcc -o eng.x eng.o fun_eng.o
./eng.x source.txt result.txt
编译没问题,但是result.txt里什么都没有
解决方案
推荐阅读
- python - sqlite3 无法让数据库保持更新
- python - 在后台 windows 10 Powershell 中运行 Python HTTPServer
- c - 为我的堆栈分配 2 倍的内存(仅在需要时)C
- encoding - Informatica - 如果需要,是否可以通过使用通过命令任务调用的程序来确定和转换文件编码?
- python - 按单词和单词中的字符对单词列表进行排序
- java - Tomcat ClassPath 不适用于 Javac 编译
- python - python flask sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 尝试写一个只读数据库
- java - JavaFX - 通过 IntelliJ 运行应用程序时资源路径不工作,但在使用 JLink 构建它后工作
- php - 如何在 1 个域上提供两个 laravel 应用程序?
- sql - 无效的凭据邮递员