Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 24-07-2011, 10:47 PM   #1 (permalink)
CSE Freak!
 
rajsujayks's Avatar
 
Join Date: Aug 2008
Location: Coimbatore
Posts: 81
Exclamation How to make the program produce the required output..?


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.
Attached Files
File Type: txt EMPIN.txt (46 Bytes, 7 views)
File Type: txt EMPOUT.txt (115 Bytes, 7 views)
__________________
Spoiler:
Dell Inspiron 530 desktop | Intel C2Q Q6600 @ 2.4 Ghz | 2GB DDR2 667Mhz | ASUS ENGTS450 | 250GB SATA2 | Windows 7 Ultimate 64-bit | 350 watt PSU | HP Deskjet F4488 AllInOne | Nokia 6681 Sony Ericsson XPERIA MiniPro (Love you Nokia..! :-( Am sorry..)
rajsujayks is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-07-2011, 11:25 PM   #2 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: How to make the program produce the required output..?

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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 26-07-2011, 10:12 PM   #3 (permalink)
CSE Freak!
 
rajsujayks's Avatar
 
Join Date: Aug 2008
Location: Coimbatore
Posts: 81
Exclamation Re: How to make the program produce the required output..?

Quote:
Originally Posted by Liverpool_fan View Post
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...

Quote:
Originally Posted by Liverpool_fan View Post
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!!
__________________
Spoiler:
Dell Inspiron 530 desktop | Intel C2Q Q6600 @ 2.4 Ghz | 2GB DDR2 667Mhz | ASUS ENGTS450 | 250GB SATA2 | Windows 7 Ultimate 64-bit | 350 watt PSU | HP Deskjet F4488 AllInOne | Nokia 6681 Sony Ericsson XPERIA MiniPro (Love you Nokia..! :-( Am sorry..)

Last edited by rajsujayks; 26-07-2011 at 08:35 PM.
rajsujayks is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:32 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2