reading from a file without actually writing into its hard disk copy

Discussion in 'C Programming' started by Jeevan, Oct 19, 2003.

  1. Jeevan

    Jeevan Guest

    Hi,

    I have an array of data (which I am getting from a socket connection).
    I am working on a program which acts on this data but the program is
    written to work on data from a file (not from an array). I cannot
    change anything in the program but can add some features by which I
    can convert this array of data into a file. The easiest thing would be
    to write the data into a file (in hard disk) and use it. But I will be
    working on thousands of such data, so this would be time consuming.

    One more constraint is that, the data has to be written into a file in
    binary format and read in ascii format (otherwise the data is not read
    properly). Is there anyway I can achieve this (writing in binary
    format and reading in ascii format) without actually writing the array
    into harddisk (by just using RAM).

    Thanks
    Jeevan.
     
    Jeevan, Oct 19, 2003
    #1
    1. Advertising

  2. Jeevan

    Malcolm Guest

    "Jeevan" <> wrote in message
    > The easiest thing would be to write the data into a file (in hard disk)
    > and use it. But I will be working on thousands of such data, so this
    > would be time consuming.
    >

    tmpfile() is your friend. This is almost always implemented as a RAM file.
    >
    > One more constraint is that, the data has to be written into a file in
    > binary format and read in ascii format (otherwise the data is not read
    > properly). Is there anyway I can achieve this (writing in binary
    > format and reading in ascii format) without actually writing the array
    > into harddisk (by just using RAM).
    >

    Unfortunately now you're in trouble, since you can't open a temporary file
    as binary for writing and text for reading. The real solution is to change
    your program design so that this isn't necessary.
     
    Malcolm, Oct 19, 2003
    #2
    1. Advertising

  3. Jeevan

    Scott Xiao Guest

    Well, if you're in Unix/Linux, maybe you can use function "mmap".
    The system call mmap() establishes a mapping between your process's
    address space and a file.
    You could map your file into RAM, then place your array on that memory
    address.
    But, maybe your data should be formated as ascii .....

    Scott


    "Jeevan" <> wrote in message
    news:...
    > Hi,
    >
    > I have an array of data (which I am getting from a socket connection).
    > I am working on a program which acts on this data but the program is
    > written to work on data from a file (not from an array). I cannot
    > change anything in the program but can add some features by which I
    > can convert this array of data into a file. The easiest thing would be
    > to write the data into a file (in hard disk) and use it. But I will be
    > working on thousands of such data, so this would be time consuming.
    >
    > One more constraint is that, the data has to be written into a file in
    > binary format and read in ascii format (otherwise the data is not read
    > properly). Is there anyway I can achieve this (writing in binary
    > format and reading in ascii format) without actually writing the array
    > into harddisk (by just using RAM).
    >
    > Thanks
    > Jeevan.
     
    Scott Xiao, Oct 20, 2003
    #3
  4. Jeevan

    Jeevan Guest

    Thanks for the suggestion. Someone suggested the following method. I
    am collecting the data into the file created by tmpfile() (I am using
    Windows so mmap does not work like that in unix). tmpfile() is created
    in binary mode. Now to read the file in ascii mode, I have to change
    the mode of the file. This is possible using the function _setmode()
    in vc++. I dont think this is available in unix.

    The reason why I am having to do this is that, webserver sends the
    data as it is in bytes. So, if the data contains new line character
    written by another c program in windows, it is actually the characters
    '\r' '\n'. If I write this in ascii mode, then the file contains \r \r
    \n (\n is replaced by \r \n). So I have to write it in binary mode. I
    have to read it in ascii mode because there are 2 characters for new
    line character.

    One more solution to avoid this problem is to change the data sent by
    webserver on the fly (array manipulations) which might take some extra
    time.

    Jeevan
     
    Jeevan, Oct 21, 2003
    #4
  5. "Jeevan" <> wrote in message
    news:...

    (snip)

    > The reason why I am having to do this is that, webserver sends the
    > data as it is in bytes. So, if the data contains new line character
    > written by another c program in windows, it is actually the characters
    > '\r' '\n'. If I write this in ascii mode, then the file contains \r \r
    > \n (\n is replaced by \r \n). So I have to write it in binary mode. I
    > have to read it in ascii mode because there are 2 characters for new
    > line character.
    >
    > One more solution to avoid this problem is to change the data sent by
    > webserver on the fly (array manipulations) which might take some extra
    > time.


    It might take more time, but it is exactly what the system I/O routines will
    do when reading ASCII mode files.

    -- glen
     
    Glen Herrmannsfeldt, Oct 21, 2003
    #5
    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. jon
    Replies:
    10
    Views:
    756
    Gordon Burditt
    Sep 8, 2006
  2. eastcoastguyz
    Replies:
    10
    Views:
    527
    Roland Pibinger
    Dec 14, 2006
  3. thunk
    Replies:
    1
    Views:
    366
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    541
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    665
    thunk
    Apr 3, 2010
Loading...

Share This Page