homework help scanning info from disk? 
Author Message
 homework help scanning info from disk?

HI...I hope someone could help me with this lab, i'm having a hard time
getting started.

What I need to do is retrieve and manipulate a company's payroll
database using structs which
is found in A:payfile.dat(disk).  Here's what the file looks like(first
name, initial,
last name,address, age, sex, tenure, salary----

ADA            A AGUSTA    33 BABBAGE ROAD  LOVELACE      GB 19569 28 F
2 350.50
ISSAC        A ASIMOV      99 FICTION WAY        AMHERST         MA
63948 58 M 3 420.00
HUMPHRY R BOGART    71 SAM STREET        HOLLYWOOD   CA 48482 56 M 5
366.00
ALBERT     G EINSTEIN  94  ENERGY WAY       PRINCETON      NJ 47474 67 M
8 780.00
..........
..........

Right now I'm just not sure how to scan all the info in, here's what I
have so
far(I know it's pretty much all messed up but I didn't know how else to
do
it)(Also I deleted the address for now, because I didn't know how to
deal with
the spaces inbetween some of the street names and states--

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

#define NUM_EMPLOYEE 20

typedef struct
    {
     char first[8];
     char initial;
     char last[10];
     int age;
     char sex;
     int tenure;
     float salary;
    }PAYROLL;

//Lab 6 Payroll Database

void main(void)
{

     int i=0;

 PAYROLL Ary[NUM_EMPLOYEE];

    FILE *fp;
    fp=fopen("A:payfile2.dat","r");
    if(fp==NULL)
  {
     printf("File not found.\n");
     exit(1);
  }
     while(!feof(fp))
  {

fscanf(fp,"%s%c%s%d%c%c%f\n",Ary[i].first,Ary[i].initial,Ary[i].last,Ary[i].age

      ,Ary[i].sex,Ary[i].tenure,Ary[i].salary);
     i++;
  }

Quote:
}

Any help would be much appreciated


Fri, 03 Nov 2000 03:00:00 GMT  
 homework help scanning info from disk?

Quote:

> What I need to do is retrieve and manipulate a company's payroll
> database using structs which
> is found in A:payfile.dat(disk).  Here's what the file looks like(first
> name, initial,
> last name,address, age, sex, tenure, salary----

> ADA            A AGUSTA    33 BABBAGE ROAD  LOVELACE      GB 19569 28 F
> 2 350.50
> ISSAC        A ASIMOV      99 FICTION WAY        AMHERST         MA
> 63948 58 M 3 420.00

This looks like it is a fixed column format. If this is the case,
decoding it should not be very difficult. What 's needed is a detailed
file format description. If this says something like: first name from
column 1 to columns 25, second name from column 26 to columns 30, etc.
you can extract those parts very easily using simple "strncpy()" calls:

  char *currentPosition;
  char firstName[FIRST_NAME_SIZE + 1];
  char secondName[SECOND_NAME_SIZE + 1];

   /* Check line size. */
  expectedLength = FIRST_NAME_SIZE+SECOND_NAME_SIZE;
  if ( strlen(lineToDecode) != expectedLength )
  {
    /* Handle error: line is not long enough
  }
  currentPosition = lineToDecode;

    /* Extract first name. */
  strncpy( firstName, currentPosition, FIRST_NAME_SIZE );
  firstName[FIRST_NAME_SIZE] = '\0';
  currentPosition += FIRST_NAME_SIZE;

    /* Extract second name. */
  strncpy( secondName, currentPosition, SECOND_NAME_SIZE );
  secondtName[SECOND_NAME_SIZE] = '\0';
  currentPosition += SECOND_NAME_SIZE;

The above example is base on all elements having a fixed size. If this
is not the case, you need a more sophisticated extraction mechanism.
The next best thing would be to have a single unique separation
character (or maybe several of them) that do not appear inside of
the entries. An example would be to separate number, that do not contain
space with spaces: 42 666 4711 -12.345

If this is the case you can use "strchr()" to search for the separation
character, or use a loop that examines every character separately. You
could also use "strtok()", but be careful. This function is more
complex and not easy to understand for a beginner.

Quote:
> #include<stdio.h>
> #include<stdlib.h>
> #include<conio.h>

You should try not to use <conio.h>. It is rarely needed to solve a
given problem and it is *not* part of the standard C language. Instead
it is a compiler specific extension.

Quote:
> #include<string.h>

> #define NUM_EMPLOYEE 20

> typedef struct
>     {
>      char first[8];
>      char initial;
>      char last[10];
>      int age;
>      char sex;
>      int tenure;
>      float salary;
>     }PAYROLL;

Having a data structure is a good aproach.

Quote:
> //Lab 6 Payroll Database

The "//" comment is part of C++ and a syntax error on a lot of C
compilers.

Quote:
> void main(void)

The only correct return type for "main" is "int" ! "void" is tolerated
by some compilers, but is actually wrong. Using "int" is as easy as
using "void", but it's so much more portable and correct.

Quote:
> {
>      int i=0;

>  PAYROLL Ary[NUM_EMPLOYEE];

>     FILE *fp;
>     fp=fopen("A:payfile2.dat","r");
>     if(fp==NULL)
>   {
>      printf("File not found.\n");
>      exit(1);
>   }
>      while(!feof(fp))

This is a very dangerous loop condition which will certainly cause
on error the way you use it. The comp.lang.c FAQ explains why as
an answer to the question:
  12.2:   Why does the code
                while(!feof(infp)) {
                        fgets(buf, MAXLINE, infp);
                        fputs(buf, outfp);
                }
          copy the last line twice?

Quote:
>   {

> fscanf(fp,"%s%c%s%d%c%c%f\n",Ary[i].first,Ary[i].initial,Ary[i].last,Ary[i].age

>       ,Ary[i].sex,Ary[i].tenure,Ary[i].salary);

Hmmm, where to start explaining ? The "fscanf()" function is *very*
complex and *very* difficult and tricky to use. A simple approach like
yours is in almost all cases doomed to fail, because it takes long
study to master the intricaces of the "scanf()" family of function.

It is much easier and less error prone to use "fgets()" instead to
read one line of text and use a straight forward approach to decode
the line in simple steps. Look at the format description and use simple
steps or functions to extract one element after the other from the
string. Don't try to do it all in one line by using an "egg laying wool
milk pig" like "fscanf()".

Take a good look at the functions in <string.h>, because they are very
useful for decoding strings and for conversion to numbers.

Stephan
(initiator of the campaign against grumpiness in c.l.c)

Quote:
>      i++;
>   }

> }

> Any help would be much appreciated



Fri, 03 Nov 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help on disk info

2. Request for info on disk drive caching

3. disk usage info

4. more info for saving int array to disk

5. Hard disk serial number info

6. How to access disk boot record info?

7. Disk info

8. disk info

9. slightly OT: need help finding info on quering printers for info

10. homework help

11. help with java homework

12. Homework Help

 

 
Powered by phpBB® Forum Software