2013-03-30 2 views
0

이 명령은 읽거나 쓸 바이트를 반환합니다. 현재 한 번에 100자를 읽고 쓰려고합니다. read (fd, buffer, 100)를 사용할 때 99 문자를 읽습니다. read (fd, buffer, 101)를 사용할 때 100 문자를 읽습니다. 문제가 무엇입니까?read (...) write (...)를 사용하여 C로 파일 읽기 및 쓰기

소스 코드에서 100자를 읽고 대상 1에 씁니다. 그런 다음 동일한 소스에서 destination2로 50자를 읽어야합니다. 첫 번째 몇 개의 루프가 끝나면 읽기 및 패스가 정확하지 않습니다. 세 번째 루프에서 문제가 발생합니다. 을 확인하시기 바랍니다 : 내가 믿는 :

[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------------ 



} 
+0

원하는 charCount 0에 또는 1 세트 100 번째 문자가 null 플러그이기 때문에 100으로 설정할 때만 99를 얻을 수 있습니다. –

+3

@StevenMorad :'read'와'write'는 특별히'\ 0'을 다루지 않으므로 그렇게 생각하지 않습니다. – nneonneo

+0

파일을 여는 코드 (SSCCE 또는 [짧은 자체 포함, 올바른 예] (http://sscce.org/))를 보여 주면 현명합니다. 어떤 종류의 짧은 쓰기를 감지하려면'if (write (destinationFile, buffer, charCount)! = charCount)'로 쓰기를 검사해야합니다. 'memset()'명령은 실제로 필요하지 않습니다. –

답변

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

이 당신이 1 개 이상에 의한 해제에 관한 당신의 질문의 첫 번째 부분에 대한

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

맞습니다,하지만 문제는 제가 파일 전체에서 보이지 않는 개행 문자를 고려하지 않고 있다는 것입니다. – knowKnothing