how to rewind to file beginning immediately after writing...

Discussion in 'C Programming' started by Rajorshi Biswas, Jul 20, 2006.

  1. Hi all,
    I'm aware that this might not be a "C-specific" question, if so,
    please let me know which group is the most appropriate for this kind of
    question.

    This is a question on unix filehandling in C, all I have to do is
    rewind to the beginning of the file immediately after "write"ing some
    bytes. My code looks something like this -


    /* create a file and write something into it */
    int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
    char b[100] = "this is some stupid text!\n";
    write(fd, b, sizeof(b));

    p = lseek(fd, 0, SEEK_SET);
    printf("lseek returned = %d\n",p); /* returns 0 */

    /* char block[BUFSIZ]; */
    /* char tmp[] = "/tmp/fileXXXXXX"; */

    newfd = mkstemp(tmp);

    //copy contents of fd to newfd
    while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
    HERE <<< */
    write(newfd, block, nread);



    I am clueless as to why after a write to a new file, and an lseek to
    position zero, a subsequent read fails. Even if I do an fsync() after
    the write, it fails. I'm on RHEL3.


    Thanks in advance.
    Raj
     
    Rajorshi Biswas, Jul 20, 2006
    #1
    1. Advertising

  2. Sorry folks, fixed it - a simple case of changing O_WRONLY to O_RDWR ..
    duh :(
    Rajorshi Biswas wrote:
    > Hi all,
    > I'm aware that this might not be a "C-specific" question, if so,
    > please let me know which group is the most appropriate for this kind of
    > question.
    >
    > This is a question on unix filehandling in C, all I have to do is
    > rewind to the beginning of the file immediately after "write"ing some
    > bytes. My code looks something like this -
    >
    >
    > /* create a file and write something into it */
    > int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
    > char b[100] = "this is some stupid text!\n";
    > write(fd, b, sizeof(b));
    >
    > p = lseek(fd, 0, SEEK_SET);
    > printf("lseek returned = %d\n",p); /* returns 0 */
    >
    > /* char block[BUFSIZ]; */
    > /* char tmp[] = "/tmp/fileXXXXXX"; */
    >
    > newfd = mkstemp(tmp);
    >
    > //copy contents of fd to newfd
    > while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
    > HERE <<< */
    > write(newfd, block, nread);
    >
    >
    >
    > I am clueless as to why after a write to a new file, and an lseek to
    > position zero, a subsequent read fails. Even if I do an fsync() after
    > the write, it fails. I'm on RHEL3.
    >
    >
    > Thanks in advance.
    > Raj
     
    Rajorshi Biswas, Jul 20, 2006
    #2
    1. Advertising

  3. Rajorshi  Biswas

    Flash Gordon Guest

    Rajorshi Biswas wrote:
    > Hi all,
    > I'm aware that this might not be a "C-specific" question, if so,
    > please let me know which group is the most appropriate for this kind of
    > question.
    >
    > This is a question on unix filehandling in C, all I have to do is
    > rewind to the beginning of the file immediately after "write"ing some
    > bytes. My code looks something like this -
    >
    > /* create a file and write something into it */
    > int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);


    open and friends are not part of standard C so you would do better
    asking on comp.unix.programmer, however I would point out that if
    O_WRONLY has the obvious meaning of opening the file for writing only
    you will not be able to read from it.

    <snip>

    > while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
    > HERE <<< */


    <snip>

    If you want further information on using open and friends, please ask in
    comp.unix.programmer
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jul 20, 2006
    #3
  4. Thanks for pointing me to the appropriate group, Gordon!

    ~Rajorshi

    Flash Gordon wrote:
    > Rajorshi Biswas wrote:
    > > Hi all,
    > > I'm aware that this might not be a "C-specific" question, if so,
    > > please let me know which group is the most appropriate for this kind of
    > > question.
    > >
    > > This is a question on unix filehandling in C, all I have to do is
    > > rewind to the beginning of the file immediately after "write"ing some
    > > bytes. My code looks something like this -
    > >
    > > /* create a file and write something into it */
    > > int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);

    >
    > open and friends are not part of standard C so you would do better
    > asking on comp.unix.programmer, however I would point out that if
    > O_WRONLY has the obvious meaning of opening the file for writing only
    > you will not be able to read from it.
    >
    > <snip>
    >
    > > while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
    > > HERE <<< */

    >
    > <snip>
    >
    > If you want further information on using open and friends, please ask in
    > comp.unix.programmer
    > --
    > Flash Gordon, living in interesting times.
    > Web site - http://home.flash-gordon.me.uk/
    > comp.lang.c posting guidelines and intro:
    > http://clc-wiki.net/wiki/Intro_to_clc
     
    Rajorshi Biswas, Jul 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    7
    Views:
    1,201
    Emma Gumbdough
    Jun 30, 2004
  2. Replies:
    2
    Views:
    2,446
  3. Lynn McGuire

    rewind writing to a text file

    Lynn McGuire, Dec 4, 2010, in forum: C++
    Replies:
    7
    Views:
    588
    Default User
    Dec 7, 2010
  4. Alan Davies
    Replies:
    7
    Views:
    239
    Alan Davies
    Nov 27, 2003
  5. Jesse B.
    Replies:
    9
    Views:
    262
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page