0

これらのコマンドは、読み書きされたバイトを返すはずです。現在、一度に 100 文字の読み書きを試みています。read(fd, buffer, 100) を使用すると、99 文字が読み取られます。read(fd, buffer, 101) を使用すると、100 文字が読み取られます。何が問題ですか?

ソース コードから 100 文字を読み取り、それらを destination1 に書き込むことになっています。次に、同じソースから宛先 2 に 50 文字を読み取ることになっています。最初の数回のループの後、読み取りと受け渡しが不正確になります。3 番目のループで問題が発生します。チェックアウトしてください:

  [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

これはcharCount0 または 1 に設定されます。

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