Thread: new line character being added while using write system call

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    new line character being added while using write system call

    Hi ,

    I have two files . One source and other destination.

    Below are the contents

    1302 [12:09] [/cprogs/files] -> cat temp
    jimmy
    1302 [12:09] [xrakant/cprogs/files] -> cat test
    jhony jhony yes papa


    I am just copining them using the below code.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <curses.h>
    
    int main(int argc,char *argv[])
    {
         int sfd,dfd,n = 0;
        char buf[5];
    
        memset(buf,0,sizeof(buf));
    
        printf("The file name to opened %s \n",argv[1]);
        printf("The file name to copied %s \n",argv[2]);
    
    
    
        if (argc == 4 && !strcmp(argv[1],"-f"))
        {
            printf("need to override the file\n");
        }
    
    
        if (argc == 4 && !strcmp(argv[1],"-f"))
        {
             printf("need to override the file  1:%s 2:%s 3:%s\n",argv[1],argv[2],argv[3]);
    
             sfd = open(argv[2],O_RDONLY);
             dfd = open(argv[3],O_WRONLY | O_CREAT | O_TRUNC);
             printf("The open sfd: %d \n",sfd);
             printf("The open sfd: %d \n",dfd);
        }
        else
        {
             sfd = open(argv[1],O_RDONLY);
             dfd = open(argv[2],O_WRONLY | O_CREAT);
             printf("The open sfd: %d \n",sfd);
             printf("The open sfd: %d \n",dfd);
        }
    
        while( (n = read(sfd,buf,sizeof(buf))) && (dfd > 0) && strlen(buf) > 0)
        {
            printf("The content: %s number of bytest :%d \n",buf,n);
            write(dfd,buf,n);
            memset(buf,0,sizeof(buf));
        }
    
        close(sfd);
        close(dfd);
    
        return 0;
    }
    Output:

    eselnts1302 [12:08] [xrakant/cprogs/files] -> ./sysprog1_1 temp test
    The file name to opened temp
    The file name to copied test
    The open sfd: 3
    The open sfd: 4
    The content: jimmy number of bytest :5
    The content:
    number of bytest :1
    eselnts1302 [12:08] [xrakant/cprogs/files] -> cat test
    jimmy
    jhony yes papa


    I am not sure how the new line character is being added while doing write to destination file.





    Thanks
    vlrk

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The source file contains "jimmy\n".
    Your read is in a loop.
    The first pass reads "jimmy".
    The second pass reads "\n".
    Both are written to the destination file.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    I am doing echo "jimmy" > temp to fill the source file.

    I am not sure here how \n is being inserted .

    Is there any editor where I can see all the special character like \n and other. So that I can be sure that \n is coming from source file.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    echo adds a newline.
    echo -n supresses it.
    cat -E shows end-of-line character as $.
    Clearly there's a newline at the end of the file since when you cat the file your prompt appears on the next line. If there weren't a newline your prompt would appear on the same line.
    (This is all assuming linux.)

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Thanks .. It did worked.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while( (n = read(sfd,buf,sizeof(buf))) && (dfd > 0) && strlen(buf) > 0)
    1. You should compare dfd (and sfd for that matter) with -1 (what open would have set it to on failure, and what you should have checked for before getting to this point).
    0 is actually a valid fd, so the > test is itself wrong as well.

    2. strlen(buf) is undefined, since it must necessarily step off the end of the array if you happened to completely fill the buffer with the read call.
    If you want to always ensure there is room for a \0, then you need sizeof(buf)-1 to allow space for it.

    You ordinarily only need something like this
    while ( (n = read(sfd,buf,sizeof(buf))) > 0 )
    Then you use n inside the loop to process the valid characters.

    > printf("The content: %s number of bytest :%d \n",buf,n);
    This is undefined behaviour for the same reason as strlen(buf) is.

    If you want to print a string without a \0, but you know it's length, then use
    printf("The content: %.*s number of bytest :%d \n",n,buf,n);

    If you want \0 terminated strings inside the loop, then write this
    Code:
    while ( (n = read(sfd,buf,sizeof(buf)-1)) > 0 ) {
      buf[n] = '\0';
      // now strlen and simple printf will do what you want safely
    }

    All the memset calls are a waste of effort. You don't need any of them if the code is correct in other places.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write function of system call failing
    By learnunix in forum Linux Programming
    Replies: 5
    Last Post: 10-17-2014, 08:20 AM
  2. printf and system call write
    By blob84 in forum C Programming
    Replies: 7
    Last Post: 07-06-2011, 06:20 AM
  3. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  4. Extra line break added during copying
    By Tigers! in forum C Programming
    Replies: 14
    Last Post: 10-21-2009, 04:49 AM
  5. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM

Tags for this Thread