Thread: write function of system call failing

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    write function of system call failing

    Hello Guys,

    My problem here is that I have a C code that is suppose to write data to a file (preferably a .txt file). But after I run the code it creates the file but it does not write anything inside. PLEASE HELP..I'm a newbie (and I don't want to give up now please) The code is below.

    -----------------------
    Code:
    #include <sys/file.h>
    #include <stdio.h>
    struct record{
    int uid;
    char login[8];
    };
    
    char * logins[] = { "user1", "user2", "user3", "user4", "user5" };
    
    int main (int argc, char * argv[])
    {
    
    //struct record * user[5];  At first I tried to use an array of pointer but Hmmm had enough trouble to try with a simple structure. lol
    struct record user[5];
    int i,fp;
    if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
    {
    perror(argv[1]);
    exit(1);
    }
    
    for(i=0 ; i<=4 ; i++)
    {
    user[i].uid = i;
    strcpy(user[i].login , logins[i]);
    
    }
    for(i=4 ; i<=0 ; i--)
    {
    lseek(fp, (long) i*sizeof(struct record), L_SET);
    write (fp, &user, sizeof(struct record));
    }
    
    close(fp);
    exit(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i=4 ; i<=0 ; i--)
    When do you expect this to be true?

    Perhaps you meant >=

    Also, you're going to write the same record every time.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    NIce. You where right Salem. BUT the output is not on the .txt file I create. It is on the terminal an I get a strange output (unresolved characters). Some character would not work on the forum here so I've copy/paste it on a .txt file an added it as image write function of system call failing-screenshot-2014-10-16-21-40-53-jpgattachement (it's a screenshot ). Below is the new code.

    Code:
    #include <sys/file.h>
    #include <stdio.h>
    struct record{
    int uid;
    char login[8];
    };
    
    char * logins[] = { "user1", "user2", "user3", "user4", "user5" };
    
    int main (int argc, char * argv[])
    {
    
    //struct record * user[5];  At first I tried to use an array of pointer but Hmmm had enough trouble to try with a simple structure. lol
    struct record user[5];
    int i,fp;
    if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
    {
    perror(argv[1]);
    exit(1);
    }
    
    for(i=0 ; i<=4 ; i++)
    {
    user[i].uid = i;
    strcpy(user[i].login , logins[i]);
    
    }
    for(i=4 ; i>=0 ; i--)
    {
    lseek(fp, (long) i*sizeof(struct record), L_SET);
    write (fp, &user[i], sizeof(struct record));
    }
    
    close(fp);
    exit(0);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > BUT the output is not on the .txt file I create. It is on the terminal an I get a strange output
    That's because you're writing binary data.

    If you want to see text, then you need to format your data as a string (say sprintf), then write the whole string to the file.

    But in doing so, you lose the ability to easily seek to specific records.

    Try this command
    Code:
    od -Ax -t x1z mydatafile
    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.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by learnunix View Post
    Code:
    if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
    The above line is equivalent to
    Code:
    fp = open(argv[1], O_WRONLY | O_CREAT, 0644) < 0;
    if (fp)
    You can fix your line by adding parentheses, but I personally prefer to do the assignment first, and then check the result -- I find it easier to read.

    Because fp ends up becoming logical false, value 0, and that happens to be what STDIN_FILENO macro evaluates to, and you are running this in a terminal, you get the output into the terminal instead of to the file. (Normally, standard input is not writable, but in case of Unix/POSIX terminals and pseudoterminals, it usually is writable too; just a copy of standard output and/or standard error file descriptors.)

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    Writring binary data...that makes sense.. I mean at least I should get 1s and 0s in my .txt file.
    Still I'm having issues
    1st : I tried the code you gave me....The result is 000000 in my terminal. And still nothing in my .txt file
    2nd : In the code below, you'll see I tried using sprintf. The result is I get what I want in the terminal (all character resolved) and still nothing in my .txt file

    Code:
    #include <sys/file.h>
    #include <stdio.h>
    struct record{
    int uid;
    char login[8];
    };
    
    char * logins[] = { "user1", "user2", "user3", "user4", "user5" };
    
    int main (int argc, char * argv[])
    {
    
    
    struct record user[5];
    int i,fp;
    char mystr[8];
    
    if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
    {
    perror(argv[1]);
    exit(1);
    }
    
    strcpy(user[1].login,logins[1]);
    sprintf(mystr,"%s", user[1].login);
    write (fp, mystr, 8);
    }
    
    close(fp);
    exit(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf and system call write
    By blob84 in forum C Programming
    Replies: 7
    Last Post: 07-06-2011, 06:20 AM
  2. System function Call - Nowait possible - linux environment?
    By mickle026 in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2009, 01:55 PM
  3. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  4. What system call use the CreateThread Function of borland?
    By amfm0777 in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2007, 05:41 AM
  5. Function call failing with bind2nd
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2007, 09:01 AM