Jakub
Jakub

Reputation: 699

Problem with reading and writing array from a file in C in pointer program version

I have a task. I must write a program that reads from a file and write to another file and copy array. I must use pointers. This is my program

#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;
}    

This is my input file

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. 

This is the output, which is wrong

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

The expected output is

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. 

You run program in that way: ./program.x source.txt result.txt

Edit

Now my script works, but I still have a problem with copying an array. I tried this solution, but it didn't work.

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
}

Upvotes: 1

Views: 35

Answers (1)

CRM
CRM

Reputation: 4635

The issues with your code are the following:

  • In function readText() you are reading the character EOF from the file as well (due to precedence of operations, you are first assigning the value (i.e. (*s++ = fgetc(wp))) and comparing it afterwards with EOF. A simple way of avoiding this without changing your code is to replace the EOF value by the end of string character '\0';
  • In function writeText() you should stop writing the characters when the string ends in order to avoid writing any "garbage" that is in memory to the file.

In code:

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;
}

Upvotes: 1

Related Questions