Thread: How do you open up a file using C and have it output to the window?

  1. #1
    Unregistered
    Guest

    How do you open up a file using C and have it output to the window?

    I'm trying to take an external input file and perform operations onto it. The file is a bunch of HEX code that I want to sum up. Using the following function (declared in the program previously):

    /************************
    * open the input file *
    ************************/

    void file(void)
    {
    hex= fopen("EProm0109-0502.HEX", "r");
    if (hex == NULL)
    {
    printf("Error, file is not found. \n");
    exit(1);
    }
    }

    in my main program, i try and call this by using the following command:

    while(fscanf(hex, "%d", hexstr) != EOF)
    {
    printf("%d\n", hexstr);
    }

    What am I doing wrong? Also, how can i get the numbers to read out as hex numbers, not decimal as %d would seem to indicate or a string as %s would imply?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fscanf(hex, "%d", hexstr)
    If hexstr is not an integer then this is wrong anyway, otherwise you need to do something more like this:

    fscanf( hex, "%x", &hexstr )

    >printf("%d\n", hexstr);
    If you want the variable to be printed as a hexadecimal value, use %#x:

    printf ( "%#x\n", hexstr );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    that cleaned it up a little, but I don't get the proper output. My first line in my input file should be a string of hex values:
    0E00000040C0068450B0040188B00701BCC0A7

    but my program outputs:

    12ff24

    I don't know what number represents.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    How is the file formatted? It's very possible that you are reading several values as one and causing general mayhem to your variable.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    i think it's simply one entry per line... with a return placed at the end of every line. I'm not sure if the program is reading it this way though. Maybe it is reading the entries without a carriage return. If this is the case, how do i read an entry in line by line from the original source?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An example using an array as it's input.... (simulating a file, well kind of!).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char a[] = "0E00000040C0068450B0040188B00701BCC0A7";
    	char *p = a;
    	int i;
    	
        while (sscanf(p, "%1x", &i) == 1)
        {
        	printf ("0x%02x\n", i);
        	p++;
        }
    
        return 0;
    }
    Last edited by Hammer; 07-10-2002 at 10:17 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Maybe it is reading the entries without a carriage return.
    fscanf is delimited by whitespace, I simulated your program with a file of my own and it worked perfectly. Perhaps you could post a small chunk of the file that we can work with?

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    :0E00000040C0068450B0040188B00701BCC0A7
    :080010008687050000B802001C
    :080020000000F244F54305C3A2
    :0C003000F744FA43F64AF84FF94147C084
    :0400400086850500AC
    :06005000B701036A5DC068
    :10006000B7010C2400000A978684B76A0500F9617D
    :0E007000F86FF764F66AFA63809205000200EA
    :1000800020B00E010D2900B00AA40AAF01C0078BF1
    :0C009000068C03B86161FB9801C351C2EB

  9. #9
    Unregistered
    Guest
    there is some of my input file. Also, Hammer, when I try to implement your code, I get the following error:

    warning C4047: 'initializing' : 'char *' differs in levels of indirection from 'char (*)[39]'

    And I don't know what that means, if I remove the & sign, the error goes away but I still can't run the program.

    P.S. Thanks for the help guys

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Oops, stupid typo on my part.

    Change
    >char *p = &a;
    to
    >char *p = a;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Unregistered
    Guest
    anybody able to make anything out of that input file sample i posted?

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    anybody able to make anything out of that input file sample i posted?
    Yep, but it's not pretty though.
    Code:
              
              
                                      :0E00
                                   000040C0
                                 068450B00401
                             88B00701BCC0A7:0
                         80010008687050000B80
                    2001C:080020000000F2
                 44F54305C3A2:0C00300
                  0F744FA43F64AF8
                  4FF94147C0
                    84:04
                      0
                      0
                      4
                      0
               0086850500AC:06
               005000B701036A5
               DC068:10006000B
               7010C2400000A97
               8684B76A0500F96
               17D:0E007000F86
         FF764F66AFA63809205000200EA 
         1000800020B00E010D2900B00AA
         40AAF01C0078BF1:0C009000068
           C03B86161FB9801C351C2EB
    Sorry, couldn't resist.

    Seriously, can you show me some sample output (what you want it to look like), and I'll help out.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly is it you want? Do you want the hex value of the characters in the file, or do you want to display the information in the file as is? (IE: Is the stuff in your file supposed to be hex already and you're just trying to display it?)

    Those are two entirely different things.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    More stuff to throw into the mix whilst we figure out what is required.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    // format of line is as follows
    // :llaaaaaaddddddddss
    // :0400400086850500AC
    // : is a colon
    // ll is a byte indicating the number of dd data bytes
    // aaaaaa is a 3 byte address in LSB order
    // dd are the data bytes
    // ss is the checksum of all the bytes which precede it
    // ( ll+aa+dd+ss ) % 256 = 0
    void decode_line ( char *line ) {
        int i;
        int sum = 0;
    
        printf( "Decoding %s", line );
    
        // skip the :, and the \n at the end of the line
        for ( i = 1 ; i < strlen(line)-1; i += 2 ) {
            int byte;
            sscanf( &line[i], "%2x", &byte );
            printf( "byte=%02x\n", byte );
            sum += byte;
        }
    
        printf( "Sum=%x\n", sum );
        printf( "Sum modulo 256=%x\n", sum % 256 );
    }
    
    int main ( ) {
        // example line, as you would get using fgets
        decode_line( ":0400400086850500AC\n" );
        return 0;
    }

  15. #15
    Unregistered
    Guest
    the characters have now been identified as hex values. From here, I want to get the decimal equivalent of each hex value and then sum up the rows. What I really want is the summation value of each row. So, let's take the first row for instance: the sum of the decimal equivalents of each hexidecimal number turns out to be 156.
    (E+4+C+6+8+4+5+B+4+1+8+8+B+7+1+B+C+C+A+7)(base 16)=157(base 10)

    Now, the code I have so far works great for the single line entry. When I want to read the entire file, some 100+ Hex values, i get nothing. I want to be able to add up line by line the decimal sums and display the totals individually. So far I can do it for one line, when manually entered into my code, now I want to do it so I can specify the file name, and the code will do the rest for me.

    Now did I confuse everybody else as much as confused myself with that explanation? :-D

Popular pages Recent additions subscribe to a feed