【问题标题】:Problem with reading and writing array from a file in C in pointer program version在指针程序版本中从 C 中的文件读取和写入数组的问题
【发布时间】: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
}

【问题讨论】:

    标签: c arrays pointers io


    【解决方案1】:

    您的代码存在以下问题:

    • 在函数 readText() 中,您也从文件中读取字符 EOF(由于操作的优先级,您首先分配值(即 (*s++ = fgetc(wp))),然后将其与 EOF 进行比较。一个简单的方法在不更改代码的情况下避免这种情况的方法是用字符串字符 '\0' 的结尾替换 EOF 值;
    • 在函数writeText() 中,您应该在字符串结束时停止写入字符,以避免将内存中的任何“垃圾”写入文件。

    在代码中:

    int readText(FILE *wp, char *s, int max)
    {
        int sum = 0;
    
        while((*s++ = fgetc(wp)) != EOF)
        {
            sum++;
        }
        *(s-1)='\0'; // replace EOF by empty character
        return sum;
    }
    

    ...

    int writeText(FILE *wp, char *s)
    {
        int sum = 0;
        while((*s++ = fputc(*s, wp)))
        {
            sum++;
    
            if(*s == '\0')
                break;
    
        }
        return sum;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      相关资源
      最近更新 更多