How to make the program produce the required output..?

rajsujayks

CSE Freak!
I want to get the input and output as in the files attached.
This is my code:

Code:
//Employee Details...
//Execution syntax : "ProgramName InputFile OutputFile"
//Input File should contain grade and name of employee
//separated by a tab with one record per line..
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
    FILE *in,*out;
    char *name,*designation;
    float basic,hra,da,gross;
    char grade;

    //Opening files..
    in=fopen(argv[1],"r");
    out=fopen(argv[2],"w");
    if((in==NULL)||(out==NULL))
    {
        printf("\n\nEither one or both the specified files cannot be opened!");
        return 0;
    }
    while(feof(in)==0)
    {
        basic=10000;
        fscanf(in,"%c%s",&grade,name);
        switch(grade)
        {
            case 'E':
                strcpy(designation,"Engineer");
                hra=0.15;
                da=0.10;
                break;
            case 'S':
                strcpy(designation,"Supervisor");
                hra=0.20;
                da=0.10;
                break;
            case 'P':
                strcpy(designation,"Project Head");
                hra=0.25;
                da=0.15;
                break;
            case 'H':
                strcpy(designation,"Helper");
                hra=0.10;
                da=0.05;
                break;
            default:
                strcpy(designation,"Unknown");
                hra=0;
                da=0;
        }
        gross=basic+(basic*hra)+(basic*da);
        fprintf(out,"%s\t%s\t%f",name,designation,gross);
    }
    fclose(in);
    fclose(out);
    printf("\n\n\tFile %s generated!",argv[2]);
    return 0;
}

That is, if I give the command
Code:
C:\>emp empin.txt empout.txt
(assuming the file empin.txt is in C: drive and the code is compiled and saved in the name emp.exe in C: drive), the attached file should be produced...

Please check and tell me what I'm doing wrong. Thanks in advance. :)
 

Liverpool_fan

Sami Hyypiä, LFC legend
Well first of all

char *name,*designation;

These are simply character pointers and you haven't allocated any memory. Either statically allocate some highest possible size using arrays or dynamically allocate memory.

Secondly don't use "%c" in fscanf and in particular don't mix it with %s.
Either use the incredibly hackish way of %1s and pointer to character in fscanf, or just use fgets to read the line and then parse it.

Just don't mix characters and strings, it can get ugly.
 
OP
rajsujayks

rajsujayks

CSE Freak!
Well first of all

char *name,*designation;

These are simply character pointers and you haven't allocated any memory. Either statically allocate some highest possible size using arrays or dynamically allocate memory.

Secondly don't use "%c" in fscanf and in particular don't mix it with %s.
Either use the incredibly hackish way of %1s and pointer to character in fscanf, or just use fgets to read the line and then parse it.

Just don't mix characters and strings, it can get ugly.

Tried... But it doesn't seem to work..Something else is wrong I guess...

Well first of all

char *name,*designation;

These are simply character pointers and you haven't allocated any memory. Either statically allocate some highest possible size using arrays or dynamically allocate memory.

Secondly don't use "%c" in fscanf and in particular don't mix it with %s.
Either use the incredibly hackish way of %1s and pointer to character in fscanf, or just use fgets to read the line and then parse it.

Just don't mix characters and strings, it can get ugly.

NO! NO! It works...! Thanks a lot..! :) I fumbled with the code for about 2 hours and got it...! I always love that feeling.. ! Thank YOU!! :smile:
 
Last edited:
Top Bottom