Thread: File troubles

  1. #1
    Registered User
    Join Date
    Oct 2001

    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


    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. */


    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) ;
    pos = ftell(fp1) ;
    iteration+= 1 ; /* Moves the fp1 to beginnig of 2nd col. */

    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
    fopen("", "rb")

    Binary is your friend.


  3. #3
    Registered User
    Join Date
    Oct 2001

    File Troubles

    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.


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

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
    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. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM