File troubles

This is a discussion on File troubles within the C Programming forums, part of the General Programming Boards category; I am working on a program to manipulate files. Data is read from input file and is to be transposed ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Unhappy File troubles

    I am working on a program to manipulate files. Data is read
    from input file and is to be transposed into the output file.


    Here is the description :

    Input File

    ^A^@^U^G^L^G^Q^G^F^h
    ^H^P^a^j^i^k^l^m^f^x

    and multiple lines like this. Specs. indicate each line can
    be upto 32MB!!!.

    I am printing the decimal equivalent of these characters in the
    output file. Output has to be like :

    /* I have copied input chars. to illustrate. In reality output file
    has decimal equivalents of input chars. */

    ^A^H
    ^@^P
    ^U^a
    ^G^j
    ^L^i
    ^G^k
    ^Q^l
    ^G^m
    ^F^f
    ^h^x

    and so on.

    What I have done is to read each line into a 'unsigned char buffer'. Find the length of the buffer, print each character
    of the buffer, add a newline and advance file pointer.

    fp1 = Output file pointer
    length = Length of input buffer(line)
    buf = unsigned char buffer

    /** Snippet of code **/
    for(index = 0 ; index < length ; index++)
    {
    pos = ftell(fp1) ;
    fprintf(fp1,"%d",buf[index]);
    fprintf(fp1,"\n");
    pos = ftell(fp1) ;
    fseek(fp1,pos+(length/2)-1,SEEK_SET);
    }
    iteration+= 1 ; /* Moves the fp1 to beginnig of 2nd col. */
    fseek(fp1,iteration,SEEK_SET);
    /********************/

    This does print the data as I want it. But it also adds extra
    characters to the output file(I believe newline). I do not want
    that. Is there is a way it can be accomplished without adding
    'extra/new' characters in the output file.


    PS : The input files can be very huge, as they are generated by
    a automatic chip tester. Each line can be upto 32MB !!!.So far
    I have worked on only simple file stuff.

    Any inputs are really appreciated.


    Thanks in advance.

  2. #2
    Unregistered
    Guest
    fopen("myfile.xyz", "rb")
    fopen("myfile.abc","wb")

    Binary is your friend.

    Quzah.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    File Troubles

    Salem,
    The code snippet is the actual code I have used to print the
    buffer contents. Buffer contains the data from the input file.

    I am opening files and reading the input file contents using
    the following statements.

    /* Opening input and output file */
    fp = fopen("input_file", "rb");
    fp1 = fopen("output_file,"wb");

    /* Reading from input file */
    fread(buf,sizeof(char), MAX_SIZE,fp) ;
    length = strlen(buf) ;


    Quzah ,

    I am using binary mode to open files.

    Thanks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > fread(buf,sizeof(char), MAX_SIZE,fp) ;
    > length = strlen(buf) ;
    So long as buf is
    &nbsp; char buf[MAX_SIZE];
    The killer is strlen - you can't use strlen on a binary string - it will stop at the first \0

    Do this
    &nbsp; length = fread(buf,sizeof(char), MAX_SIZE,fp) ;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21