Help - Need to READ from text file then WRITE to same file.

This is a discussion on Help - Need to READ from text file then WRITE to same file. within the C Programming forums, part of the General Programming Boards category; It compiles without error, but after running the program, the text file ends up blank. The assignment follows... Write a ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Help - Need to READ from text file then WRITE to same file.

    It compiles without error, but after running the program, the text file ends up blank.

    The assignment follows...

    Write a program to read the employee file and create a payroll register. The register will contain the following data:

    a. Employee number (print left-justified)
    b. Department
    c. Pay rate
    d. Exempt
    e. Hours worked
    f. Base pay (pay rate * hours worked)

    Employe No. Department Pay Rate Exempt Hours Worked
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45


    My "DATA.txt" file is:
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45

    and my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // Function Declarations
    int getData (FILE* spDataIn,
                 int* empNo, int* dept, float* payRate, char* exempt, int* hours);
    int writeData (FILE* spDataOut,
                   int empNo, int dept, float payRate, char exempt, int hours, float basePay);
    void calcBasePay (float payRate, int hours, float* basePay);
    
    int main (void)
    {
    
    // Local Declarations
       FILE* spDataIn;
       FILE* spDataOut;
    
       int empNo;
       int dept;
       float payRate;
       char exempt;
       int hours;
       float basePay;
    
    // Statements
       printf("This program creates a payroll register...\n");
       if (!(spDataIn = fopen("DATA.txt", "r")))
       {
        printf("\aError opening employee file\n");
        return 100;
       } // if open input
    
       if (!(spDataOut = fopen("DATA.txt", "w")))
       {
        printf("\aError opening employee file\n");
        return 102;
       } // of open output
    
       while (getData
             (spDataIn, &empNo, &dept, &payRate, &exempt, &hours))
        {
         calcBasePay (payRate, hours, &basePay);
         writeData (spDataOut, empNo, dept, payRate, exempt, hours, basePay);
        } // while
    
        fclose(spDataIn);
        fclose(spDataOut);
    
        printf("End employee payroll register\n");
        return 0;
    } // main
    
    /*==================getData==================
      Reads employee data from text file.
        Pre   spDataIn is an open file.
              empNo, dept, payRate, exempt, hours, basePay pass by address
        Post  reads employee information
              if data read    --returns 1
              if EOF or error --returns 0
    
    */
    int getData (FILE* spDataIn, int* empNo, int* dept, float* payRate, char* exempt, int* hours)
    {
    // Local Declarations
       int ioResult;
    
    // Statements
       ioResult = fscanf(spDataIn, "%d %d %f %c %d", empNo, dept, payRate, exempt, hours);
    
       if(ioResult == EOF)
       return 0;
       else
       return 1;
    } // getData
    
    /*==================calcBasePay==================
      Determine Base Pay based on hours and payRate.
        Pre   payRate and hours contain the variables.
        Post  basePay copied to address.
    */
    void calcBasePay (float payRate, int hours, float* basePay)
    {
    // Statements
       *basePay = (payRate*hours);
       return;
    } // calcBasePay
    
    /*==================writeData==================
      Writes employee information, which now includes basePay, to output file.
        Pre   spDataOut is an open file
              empNo, dept, payRate, exempt, hours, basePay have values to write.
        Post  Data written to file.
    */
    int writeData (FILE* spDataOut, int empNo, int dept, float payRate, char exempt, int hours, float basePay)
    {
    // Statements
       fprintf(spDataOut, "%d %d %f %c %d %f\n", empNo, dept, payRate, exempt, hours, basePay);
       return 0;
    } // writeData
    Thank you!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Close the file after reading the data with fclose. Then open the file for reading.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    if (!(spDataIn = fopen("DATA.txt", "r")))
    if (!(spDataOut = fopen("DATA.txt", "w")))

    Open a different (temporary) file for writing.
    When you're done, delete the old DATA.txt file, and rename your temporary file to be DATA.txt
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  2. Replies: 1
    Last Post: 02-21-2012, 03:54 PM
  3. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 02:23 PM
  4. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 09:51 AM
  5. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21