3 Replies - 1081 Views - Last Post: 12 October 2012 - 12:54 AM Rate Topic: -----

#1 HelpSeeker_007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

using "fopen", "struct" and array to display the data

Posted 08 October 2012 - 12:17 AM

Here is the code that I've tried so far. I would like to know if this is correct, or there is something I should improved in it. And I would also like to know if there is any other way of coding this aside from the one that I have used.

# include<stdio.h>
# include<conio.h>
struct Dtr {
int hours;
int minutes;
};

main()
struct Dtr Login,Logout;
printf ("enter a Login time\n");
scanf ("%:%",& Login.hours, & Login.minutes);
printf ("enter a Logout time\n");
scanf ("%:%", & Logout.hours, & Login.minutes);

getch ();
return 0;
}

Attached File(s)


This post has been edited by Salem_c: 08 October 2012 - 12:33 AM
Reason for edit:: added [code][/code] tags - learn to use them yourself


Is This A Good Question/Topic? 0
  • +

Replies To: using "fopen", "struct" and array to display the data

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: using "fopen", "struct" and array to display the data

Posted 08 October 2012 - 12:39 AM

Quote

Doctor_Santiago,A02-0001,1
Peter_Palabrica,A02-0003,3
Johny_Asis,A02-0002,3
Emmie_Tabada,A02-0005,2
Anthony_Cruz,A02-0004,1

So what does this have to do with login and logout times?

> 11 scanf ("%:%",& Login.hours, & Login.minutes);
Each % needs to be followed by d,s (see here for a list).

Normally, it goes something like this
FILE *fp = fopen("employee.txt","r");
if ( fp != NULL ) {
  char buff[BUFSIZ];
  while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
    // now parse buff
    // sscanf is one way, but you may also consider things
    // like strtok, strstr, strchr, strtol, strtod, 
    // or simply a for loop examining each buff[i]
  }
  fclose(fp);
}

Was This Post Helpful? 0
  • +
  • -

#3 HelpSeeker_007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: using "fopen", "struct" and array to display the data

Posted 12 October 2012 - 12:44 AM

I don't get exactly exactly what you mean. Do you mean I should use Looping or Conditional statement? How can I? Can you please explain furtherly?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: using "fopen", "struct" and array to display the data

Posted 12 October 2012 - 12:54 AM

Step 1 is prove the input file is being read correctly.
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
    printf("%s",buff);
}



Step 2 is extract some data
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
    char employee[100];
    // do something to extract employee name from buff, and store in employee
    // see my previous post for ideas of functions to use
    // read their respective manual pages
    printf("Found employee=%s",employee);
}



When you've got each employee name being reliably extracted, then you can add code to extract the "A02-0001,1" fields as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1