0

These commands are supposed to return bytes read or written. Currently, I am trying to read and write 100 characters at a time. When I use read(fd, buffer, 100) I get 99 characters read. When I use read(fd, buffer, 101) I get 100 characters read. What is the problem?

I am supposed to read 100 characters from a source code and write them to a destination1. Then, I am supposed to read 50 characters from same source into destination2. The reading and passing is inaccurate after the first few loops. Problems arise on the third loop. Please check it out:

  [Step 2] Prcs_P2.c: Copy the contents of source.txt into destination1.txt and 
  destination2.txt as per the following procedure.

  1. Read the next 100 characters from source.txt, and among characters read, 
  replace each character ’1’ with character ’A’ and all characters are then 
 written in destination1.txt
 2. Then the next 50 characters are read from source.txt, and among characters
 read, replace each character ’2’ with character ’B’ and all characters are
 then written in destination2.txt
 3. The previous steps are repeated until the end of file source.txt.
 The last read may not have 100 or 50 characters.
 -------------
 It's copying characters irregularly. sometimes more than 100 | 50 and sometimes less.

 int main() {

    //const int sizeBuff=100;

    char buffer[105];   //used to carry information in packets of 10
    int temp=0;     //temp variable to check for errors
    int charCount=0;


    int i=0;

//----------------------------------------
    //charCount=read(sourceFile, buffer , 101 );
    while( charCount=read(sourceFile, buffer , 100) >0){    //needed 101 as last arg instead of 100. DUnno why?

        i=0;
        while(i<charCount){
            if (buffer[i]=='1')
                buffer[i]='A';
            i++;
        }

        if(write( destinationFile, buffer,charCount)==-1)       //write(...) returns the number of bytes written to destinationFile
                                //-1 depicts error in the function and 0 is returned upon end of file
        {
            printf("\nWrite fail.\n");
            perror("Error");                //Prints error, if found while writing.
        }
        memset(buffer, 0, 105);     //CLEARS BUFFER

        i=0;
        charCount=read(sourceFile, buffer , 50 );   //reads 50 bytes at a time      //need flag for error
        while(i<charCount){
            if (buffer[i]=='2')
                buffer[i]='B';
            i++;
        }

        temp=write( destinationFile2, buffer, charCount);       //write(...) returns the number of bytes written to destinationFile
        if(temp==-1)                        //-1 depicts error in the function and 0 is returned upon end of file
        {
            printf("\nWrite fail.\n");
            perror("Error");                //Prints error, if found while writing.
        }

        memset(buffer, 0, 105);
    }//while loop ends

    close(destinationFile);
    close(destinationFile2);
    close(sourceFile);
    //------PART 1 ENDS-------------

    //------PART 2 STARTS------------



}
4

1 回答 1

4
charCount=read(sourceFile, buffer , 100) >0

This sets charCount to 0 or 1. You want

(charCount = read(sourceFile, buffer , 100)) > 0
于 2013-03-30T19:28:54.923 回答