Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用读(…)写(…)在C中读写文件_C - Fatal编程技术网

使用读(…)写(…)在C中读写文件

使用读(…)写(…)在C中读写文件,c,C,这些命令应该返回读取或写入的字节。 目前,我正在尝试一次读写100个字符。 当我使用read(fd,buffer,100)时,我可以读取99个字符。当我使用read(fd,buffer,101)时,我可以读取100个字符。有什么问题 我应该从源代码中读取100个字符,然后将它们写入destination1。然后,我应该把同一来源的50个字符读入destination2。 前几个循环后的读数和传递不准确。第三个回路出现问题。 请查看: [Step 2] Prcs_P2.c: Copy the

这些命令应该返回读取或写入的字节。 目前,我正在尝试一次读写100个字符。 当我使用read(fd,buffer,100)时,我可以读取99个字符。当我使用read(fd,buffer,101)时,我可以读取100个字符。有什么问题

我应该从源代码中读取100个字符,然后将它们写入destination1。然后,我应该把同一来源的50个字符读入destination2。 前几个循环后的读数和传递不准确。第三个回路出现问题。 请查看:

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



}
[步骤2]Prcs_P2.c:将source.txt的内容复制到destination1.txt中,然后
destination2.txt,请按照以下步骤操作。
1.从source.txt中读取接下来的100个字符,在已读取的字符中,
将每个字符“1”替换为字符“A”,然后删除所有字符
用destination1.txt编写
2.然后从source.txt读取接下来的50个字符,并在字符之间读取
读取,将每个字符“2”替换为字符“B”,所有字符均为
然后写入destination2.txt
3.重复上述步骤,直到文件source.txt结束。
最后一次读取不能有100或50个字符。
-------------
它不规则地复制字符。有时超过100 | 50,有时更少。
int main(){
//常数int sizeBuff=100;
字符缓冲区[105];//用于以10个数据包的形式携带信息
int temp=0;//检查错误的临时变量
int charCount=0;
int i=0;
//----------------------------------------
//charCount=read(源文件,缓冲区,101);
而(charCount=read(sourceFile,buffer,100)>0){//需要101作为最后一个参数,而不是100。不知道为什么?
i=0;
而
这将
charCount
设置为0或1

(charCount = read(sourceFile, buffer , 100)) > 0
这将
charCount
设置为0或1

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

对于你问题的第一部分,关于off by 1错误:我认为当你将其设置为100时,你只能得到99是因为第100个字符是空插头。@StevenMorad:
读取和
写入
不要特别对待
\0
,所以我认为这不是原因。如果你显示co,这是明智的打开文件的de(SSCCE或)。您应该使用
if(write(destinationFile,buffer,charCount)!=charCount)
检查写操作,以检测任何类型的短写操作。
memset()
命令其实不是必需的。对于问题的第一部分,关于off by 1错误:我认为当设置为100时,只能得到99是因为第100个字符是空插头。@StevenMorad:
不要特别对待
\0
,所以我认为这不是原因。如果您显示打开文件的代码(SSCCE或)。您应该使用
if(write(destinationFile,buffer,charCount)!=charCount)
检查写操作,以检测任何类型的短写操作。
memset()
命令并非真正必要。你是对的,但问题是我没有考虑整个文件中不可见的换行符。你是对的,但问题是我没有考虑整个文件中不可见的换行符。