Thread: Reading two values from a file as one value?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Reading two values from a file as one value?

    After about two or more hours of experimenting and going crazy trying to find an answer, I've decided to bring the problem here, where there are many people that know what they're doing.

    I'm writing a C program to open a file and read bytes on each line, formatting them along the way.

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *modeldata1, *modeldata2;
    char modeldata1choice[30],modeldata2choice[30];
    
    printf("Model to convert: ");
    gets(modeldata1choice);
    printf("Save converted file as: ");
    gets(modeldata2choice);
    
    }
    That is generally what I have so far (I'm not including the loops and some other things as to not take up too much space).

    > One loop operates on the file-size of "modeldata1." How could I get the total size of the file in hex bytes?

    > The other loop will need to read two bytes at a time into their corresponding variables.

    float xcoord,ycoord,zcoord;

    XX XX YY YY ZZ ZZ

    I've tried fgets and fgetc, but they don't seem to be working out. The fgets function will read two bytes for me, but it reads them in a string format that shows the wrong values when in hex.

    Hopefully I didn't make this very confusing.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up an example of input and the output that you want. Your description leaves questions.

    Focus on the data types, and not on how they're being represented.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    40
    Example:

    Input = File to get data from.

    Let's say that the file is only one line. The first six bytes would be grabbed, in sets of two.

    XX XX YY YY ZZ ZZ

    For example, 00 00 FF 7F 50 00. (00 00 are read as one value. FF 7F are read as one value. 50 00 are read as one value.)

    Output is formatted like so (basically, those values, but in decimal):
    v 0 65407 20480

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    int a[6];
    fscanf(fptr, "%x %x %x %x %x %x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    printf("v %d %d %d\n", (a[0] << 8) | a[1], (a[2] << 8) | a[3], (a[4] << 8) | a[5]);
    (not syntax checked)

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Advice: To do what you want, drop formatted input and directly grab each 4 bytes. Open your file in "rb" state and call "fwrite".
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  2. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM