Thread: File I/O convert

  1. #1
    Unregistered
    Guest

    Red face File I/O convert

    Going mad (Visitor) Oct 3, 2001
    I have Text file as below on the left.
    6.00 Peter 6.00 Peter
    10.30 Joa 10.30 Joa
    12.45 Mac 12.44 Mac
    1.00 Jack 13.00 Jack
    5.00 J9 17.00 J9
    5.45 Liz ..
    10.55 Mark ..
    11.45 Peter 23.45 Peter
    12.05 Mac 00.00 Mac
    1.25 Joa 1.25 Joa
    5.55 J9 5.55 J9

    I am tring to read the above file from the start to end and convert the time to 24 hour time as shown on the right and save the text file with the converted time.
    Heres what i have coded so far.

    // Program to open .txt file covert and save to new name.

    #include <stdio.h>

    void main( void )
    {
    FILE *fp; // file pointer

    float *time; // store original time
    int nTimeFlag=0; // time flag
    float nTmpTime=0.00; // ref time.


    char buffer[900]; // store in read data
    int i, ch;
    /* Open file for reading (fail if data does not exist) */
    if( (fp = fopen( "test.txt", "rt" )) == NULL )
    printf( "error opening file \n" );
    else
    printf( "file opened \n" );
    /* Read in characters and place them in "buffer": */
    ch = fgetc( fp);
    for( i=0; (i < 899 ) && ( feof(fp ) == 0 ); i++ )
    {
    // store original time in array time

    buffer = (char)ch;
    *time = buffer;
    if (*time < nTmpTime)
    {
    if (!nTimeFlag)nTimeFlag=1;
    else nTimeFlag =0;
    }
    buffer=*time;
    if (nTimeFlag)*time +=12.00;
    buffer =*time;
    ch = fgetc( fp );
    }

    printf( "%s\n",buffer);

    fclose(fp);

    }

    Can some one pls help me, or put me in the right direction, i can not get the time cinversion side working.

    Thanks!!.

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    You implemented pretty weird functionality .
    I cannot descipher, how are you obtaining the data needed for conversion...

    You should scan each new line until first space, or whatever your delimiter char is, and convert the so obtained data.
    I assume from following line:
    >> 11.45 Peter 23.45 Peter <<

    that the string format is
    log_in_time/delimiter_char/ User_Name/delimiter_char/logout_time

    It' would be much more professional if you'd use <fprintf> for writing to, and <fscanf> for reading from the file!

    For a concrete example see fscanf example in the MSDN library (I can paste it here, if you don't have it).

    Have fun!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM