简体   繁体   English

使用 C 中的两个单独函数将字符数组从一个文件复制到另一个文件

[英]Copy char array from one file to another file by using two seperately functions in C

I have a task to get a char array from one text file and put this text in another file.我的任务是从一个文本文件中获取一个字符数组并将此文本放入另一个文件中。 I must use two functions.我必须使用两个函数。 They must look like this:它们必须如下所示:

int writeText(FILE *wp, char text[])
int readText(FILE *wp, char text[], int max)

I must use my own strlen function (_strlen).我必须使用我自己的 strlen function (_strlen)。

In the function readText they said that I must remember to put '\0' at the end and check that I don't put too many signs in the array (more than the array size).在 function readText 中,他们说我必须记住在最后放置 '\0' 并检查我没有在数组中放置太多符号(超过数组大小)。

This is my code这是我的代码

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

int _strlen (char *text)
{
    int i;
    for (i = 0; text[i] != '\0'; ++i);    
    return i;
}

int writeText(FILE *wp, char text[])
{
    int len = _strlen(text);
    for (int i = 0; i < len; i++) {
       fputc (text[i], wp);
    }
    return _strlen(text);
}

int readText(FILE *wp, char text[], int max)
{
    int length = _strlen(text);
    int c;
    int i = 0;
    max = 10000;
    while( (c = fgetc(wp)) != EOF && i <= max )
    {
        fputc (text[i], wp);
        i++;
    }
    if (i > max)
    {
        printf("This array is too big.");
    }
    text[i] = '\0';

    return _strlen(text);
}


int main (int argc, char *argv[])
{
    if (argc != 3) {
    printf("You should run program in this way:\n");
    printf("%s file_source file_result\n",argv[0]);
    exit(1);
    }

    int sum;
    const char source[] = "source.txt"; // plik wejściowy
    const char result[] = "result.txt";     // plik wyjściowy
    FILE *wz, *wc;                         // wskaźnik do pliku

    if( (wz= fopen(source,"r")) == NULL) {
        printf("Open error %s\n", source);
        exit(1);
    }
    if( (wc= fopen(result,"w")) == NULL) {
        printf("Open error %s\n",result);
        exit(2);
    }
    char tab1[1000];
    readText(wz, tab1, 1000);
    writeText(wc, tab1);


    int fclose(FILE *wp);
    int fclose(FILE *wc);

}

After I run this program I get something like that:在我运行这个程序后,我得到了类似的东西:

p\E2\B4&\FF

And I dont know why.我不知道为什么。

fputc writes a char to a specified stream. fputc将 char 写入指定的 stream。 You want to read a char and put that char in a char array.您想读取一个 char 并将该 char 放入一个 char 数组中。 Remove fputc and just assign the char from fgetc to the array at the specific index.删除 fputc 并将 fgetc 中的字符分配给特定索引处的数组。 There is no need to reassign the max variable.无需重新分配 max 变量。 As you had it, it would write over memory not assigned to the array if the input file was larger than your array size.正如您所拥有的,如果输入文件大于您的数组大小,它将覆盖未分配给数组的 memory。 You also want the loop to terminate at two less than max if you want to add a null terminating character at max - 1 or the last index in the allocated array.如果要在 max - 1 或分配数组中的最后一个索引处添加 null 终止字符,您还希望循环在小于 max 的两个处终止。

int readText(FILE *wp, char text[], int max)
{
    int c, i = 0;
    while((c = fgetc(wp)) != EOF && i < max - 1 )
    {
        text[i] = c;
        ++i;
    }

    text[i] = '\0';

    return _strlen(text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM