Thread: Inserting values into Structures

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    9

    Inserting values into Structures

    Hi,
    If I had a .dat file containing a series of numbers as following:

    100.0 0.8
    200.2 3.9
    400.5 9.3

    I would like to put these into some structure:

    Code:
      typedef struct number{
        float no1,no2;
      }NUMBER;
    where, in the first line of the .dat file, 100.0 would go into no1 and 0.8 would go into no2. Now, I would have no problem if each number was on a seperate line, as I would just use the fgets command. The problem with this though is that it reads to the end of the line...is there some way of putting these two numbers, on the same line, with just one space seperating them, into two different arrays?
    Thanks very much for the help!

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    look at the fscanf function in stdio.h. It works similar to printf, only it reads input from a file rather than printing output to a buffer. The first argument is the file, the second argument is the string containing the %d or whatever. The third and fourth argument would be the variables you want to store the values in.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There are multiple ways of doing this.

    One is to read the entire line using fgets(), and then use sscanf() to read from the string that you've just read.

    Another solution would be to just use scanf() twice in a row for each line, although it's harder to recover from errors using this method.

    Yet another solution would be to read the line using fgets(), and then use strtod() to convert it. This is probably the most robust method.

    The last solution would be to manually convert the numbers yourself.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    OK...I've had a go using the fscanf function as it would seem at first to be the easiest. However if I run the following program and specify a .dat file which is simply one line with "1 2". The program appears to do nothing. Is there something rather obvious I've done wrong here? Also, I've stopped almost ever using scanf in favour of gets, due to scanf's limitations - will the same problems arise with fscanf? Thanks,



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    typedef struct number{
            float no1,no2;
    }NUMBER;
    
    int main()
    {
    
    NUMBER number1;
    FILE *fp;
    char filename[256];
    
    
      printf("\n\nEnter filename to read: ");
      gets(filename);
      fp = fopen(filename,"r");
    fscanf(fp,"&#37;f %f",number1.no1,number1.no2);
    
    printf("%f\n%f",number1.no1,number1.no2);
    }
    Last edited by aligeb; 05-01-2007 at 08:08 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. don't use gets - read FAQ about it and replace it with fgets
    2. scanf wants pointers to vars to place values in it. so add & before number1.no1 and number1.no2 in call to it (fscanf in your case)

    PS. And better fix your indentation alrady now, while your program is small
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    Great thanks! Everything is sorted now except for one small problem. I want to be able to have the end of the .dat file as either just the end or a line of text saying 'END', so the .dat might look like:

    1.0 2.0
    3.0 4.0
    5.0 6.0
    END

    When sorting, obviously I don't want to include this 'END' line so I use a bit of code to remove it, but I have a small problem detecting it. Any IF clause I use seems to just overwrite the last line with 'END' no matter what:

    Code:
      if (filedata[0] = 0x45)	// Checks for 'E'
      {
        if (filedata[1] = 0x4E) // Checks for 'N'
        {
    		if (filedata[2] = 0x44)  // Checks for 'D'
            {
    		  // <----blahblah whatever code in here
            }
        }
      }
    But if 'END' isn't included (ie a .dat file as in my first post) this just overwrites the first three characters with 'END'...and then as my program tells it to ignore a line marked 'END'...so I lose the last line. Surely this shouldn't be happening?
    Last edited by aligeb; 05-02-2007 at 07:12 AM.

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You need to change your "=" inside your if conditions into "==". And please don't use the hex values of the respective characters but rather the more portable 'E', 'N' and 'D' or immediately the strcmp(function).

    Code:
    if (strcmp(filedata, "END") == 0)
    {
    // ...
    }

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    Ah...knew it was something fairly obvious! Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  2. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  3. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM
  4. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM