Thread: structs, and reading from file.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    structs, and reading from file.

    I have made a simple program in c, where i use structs, and have to read data from a file.

    the program looks like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
      char ugedag[3]; 
      int dag; char streg[3]; int month;
      double klokke; 
      char hold1[3]; 
      char binde1[3]; 
      char hold2[3]; 
      int goal1; 
      char bined2[3]; 
      int goal2; 
      double watchers;
    }Kampe;
    
    
    int main(void) {
      Kampe k;
      FILE *kampefil;
      kampefil = fopen("kampe.txt", "r");
      char length[60];
      int line = 0;
    
      printf("Opgave 1\n");
      while(fgets(length, 60, kampefil)){
        fscanf(kampefil, "%s %d%s%d %lf %s %s %s %d %s %d %lf", k.ugedag, &k.dag, k.streg, &k.month, &k.klokke, k.hold1, k.binde1, k.hold2, &k.goal1, k.bined2, &k.goal2, &k.watchers);
        
        if(k.goal1+k.goal2 >=7){
          printf("%s %d%s%d %8.2lf %s %s %s %d %s %d %8.4lf \n", k.ugedag, k.dag, k.streg, k.month, k.klokke, k.hold1, k.binde1, k.hold2, k.goal1, k.bined2, k.goal2, k.watchers);
        }
      }
    
      fclose(kampefil);
    
      return 0;
    }
    and it should read a line in the text file that looks like this:
    Fre 19/07 18.30 CCC - DDD 2 - 2 9.364

    now the problem is that when i run the program it puts the 18 from 18.30 together with 07 from 10/07, maybe it is because i tried to first make an int that reads the 10 and then one string to read the / and at last another int to read 07, but i can't make it read it in any different way. so if you can help it would be much appriciated. And sometimes it will make an ekstra - right after AGF.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show what you want to store into each of your variables.
    Code:
    ugedag =
    dag =
    streg =
    month =
    etc.
    now the problem is that when i run the program it puts the 18 from 18.30 together with 07 from 10/07, maybe it is because i tried to first make an int that reads the 10 and then one string to read the / and at last another int to read 07
    Have you tried to read a single character for that '/' char instead of a string?

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    What i wanted to store in the variables was something like, and the thing that would be read from the text file is Fre 19/07 18.30 CCC - DDD 2 - 2 9.364
    Code:
      char ugedag[3]; //fre
    
      int dag; char streg[3]; int month; // 19/07
    
      double klokke; //18.30
    
      char hold1[3]; //CCC
    
      char binde1[3]; // -
    
      char hold2[3];  // DDD
    
      int goal1; //2
    
      char bined2[3]; //-
    
      int goal2; //2
    
      double watchers; //9.364
    most of them works fine, but it is the / in 19/07, that i'm not quite sure how to handle, and what happens is that the 18 from 18.30 gets put together with 07 from 19/07. And the - between CCC - DDD and 2 - 2 seems to work sometimes. And yes i have tried to read a single char for the '/' but didn't really change anything.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    int dag; char streg[3]; int month; // 19/07
    So what exactly do you want in each variable?

    Code:
    input == 19/07
    dag = 
    streg = 
    month =
    It appears to me that you want dag = 19, streg = '/', and month = 7. Is this correct?

    If so why is streg a member variable of that structure? I looks like it should really just be a piece of thrash that you throw away when reading the file.

    Code:
      // From your structure.
      char ugedag[4]; // Don't forget about the end of string character.
      int dag;
      int month;
      double klokke;
    
    
     char trash;
    
      char Input[] = "Fre 19/07 18.30"
    
      sscanf("%s%d%c%d%lf", ugedag, &dag, &trash, &month, &klokke);
    Jim

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Thank you very much jim, i got it working now by just throwing it away, don't know why i didn't think of that xD, but thank you again for the help.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Along with Jim's suggestion of using a trash variable, there are a few other ways.

    For starters, you really should read the documentation for scanf: scanf(3): input format conversion - Linux man page.

    One way is to use the assignment-suppression character, so you don't even need a trash variable. scanf will read a %c, %d or whatever, and simply not store it anywhere.

    Another way might be to specify the actual character you are trying to match (see the bit about ordinary character in the doc). You could put an actual '/' in the format string, but that only works if you accept / separated dates.

    If you want to accept any of several different separators like - or . you will need something else. That something else is the %[ format specifier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with reading in data to an array of structs
    By ljgerr93 in forum C Programming
    Replies: 6
    Last Post: 01-22-2012, 01:57 PM
  2. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  3. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM
  4. reading several structs
    By breed in forum C Programming
    Replies: 10
    Last Post: 01-09-2002, 02:19 AM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 10:52 AM

Tags for this Thread