LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2012, 04:39 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
I am getting binary data in file.but i want normal data .PLEASE HELP


Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct data {
    int empID;
    char empname[20];
    struct data * next;

};
struct data *head =NULL;
static int add_to_list(struct data *);
static int filewrite(struct data *);
struct data *tail = NULL;
char buf[2026];
int main() {
    int id_number,i;
    char name[32];

    struct data *new;
    for(i=0;i<2;i++) {
        new = (struct data *)malloc(sizeof(struct data));
        if(!new){
            printf("malloc failed");
            return -1;
        }
        memset(new,0,sizeof(struct data));
        printf("enter the id");
        scanf("%d", &id_number);
        new->empID =id_number;
        printf("enter the name:");
        scanf("%s",name);
        strcpy(new->empname,name);
        add_to_list(new);
//      add_to_file(new);
        filewrite(new);
    }

    listdisp();

    return 0;
}
}

static int add_to_list(struct data *data){
    struct data *temp = NULL;

    temp = (struct data *)data;
    temp->next = head;
    head =temp;
}
int listdisp()
{
    struct data *temp = NULL;
    temp = head;
    while(temp) {
//      printf("name is %s\n" ,temp->empname);
        snprintf(buf,sizeof(buf) ,"%s ,%d" , temp->empname,temp->empID);
         //snprintf(buf,sizeof(buf) ,"%s" , temp->empID);

    //  printf("Employee id is: %d\n" ,temp->empID);
        printf("*****************Employee name and id is: %s\n" ,buf);


        temp = temp->next;
    }

static int filewrite(struct data *myvar)
{

FILE *fp;
fp= fopen("read1.txt","wb");
if(fp ==NULL)
{
    printf("unable to create a file");
    return -1;
}
    fwrite(buf,sizeof( struct data),1,fp);
    fclose(fp);
}



static int filewrite(struct data *myvar)
{

FILE *fp;
fp= fopen("read1.txt","wb");
if(fp ==NULL)
{
    printf("unable to create a file");
    return -1;
}
    fwrite(buf,sizeof( struct data),1,fp);
    fclose(fp);
}
 
Old 07-24-2012, 04:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
filewrite does not handle its input myvar, just writes the content of buf.
It was already told to you to use -W -Wall and other flags and eliminate all the warnings. Have you made it?
 
Old 07-24-2012, 04:48 AM   #3
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
filewrite does not handle its input myvar, just writes the content of buf.
It was already told to you to use -W -Wall and other flags and eliminate all the warnings. Have you made it?







Yes i have made tht in different program i was writing .but i didnt go u here wht shuld i do
 
Old 07-24-2012, 05:18 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
For a start, fix these:
Code:
bamba.c:35:1: warning: C++ style comments are not allowed in ISO C90
bamba.c:35:1: warning: (this will be reported only once per input file)
bamba.c: In function 'main':
bamba.c:39: warning: implicit declaration of function 'listdisp'
bamba.c: At top level:
bamba.c:43: error: expected identifier or '(' before '}' token
bamba.c: In function 'add_to_list':
bamba.c:51: warning: no return statement in function returning non-void
bamba.c: In function 'listdisp':
bamba.c:68: warning: ISO C forbids nested functions
bamba.c:69: error: invalid storage class for function 'filewrite'
bamba.c:68: warning: ISO C90 forbids mixed declarations and code
bamba.c:84: warning: ISO C forbids nested functions
bamba.c:85: error: invalid storage class for function 'filewrite'
bamba.c:84: error: redefinition of 'filewrite'
bamba.c:68: note: previous definition of 'filewrite' was here
bamba.c:96: error: expected declaration or statement at end of input
bamba.c:68: warning: unused parameter 'myvar'
bamba.c:84: warning: unused parameter 'myvar'
make: *** [bamba.o] Error 1
 
Old 07-24-2012, 05:25 AM   #5
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
For a start, fix these:
Code:
bamba.c:35:1: warning: C++ style comments are not allowed in ISO C90
bamba.c:35:1: warning: (this will be reported only once per input file)
bamba.c: In function 'main':
bamba.c:39: warning: implicit declaration of function 'listdisp'
bamba.c: At top level:
bamba.c:43: error: expected identifier or '(' before '}' token
bamba.c: In function 'add_to_list':
bamba.c:51: warning: no return statement in function returning non-void
bamba.c: In function 'listdisp':
bamba.c:68: warning: ISO C forbids nested functions
bamba.c:69: error: invalid storage class for function 'filewrite'
bamba.c:68: warning: ISO C90 forbids mixed declarations and code
bamba.c:84: warning: ISO C forbids nested functions
bamba.c:85: error: invalid storage class for function 'filewrite'
bamba.c:84: error: redefinition of 'filewrite'
bamba.c:68: note: previous definition of 'filewrite' was here
bamba.c:96: error: expected declaration or statement at end of input
bamba.c:68: warning: unused parameter 'myvar'
bamba.c:84: warning: unused parameter 'myvar'
make: *** [bamba.o] Error 1




i am writing my code in fedore ..i am not getting any errors as you rae getting
its done now.
the prob was that fwrite was making the code binary
so i have changed that function
 
Old 07-24-2012, 05:30 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Off: Please, use proper English words, it cannot be that hard.
On: Most likely the code you pasted in (in the first post) is broken/incomplete. Will you please check it.
 
Old 07-24-2012, 05:47 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by batman4 View Post
i am writing my code in fedore ..i am not getting any errors as you rae getting
its done now.
this is not related to OS (fedora), you must use -W -Wall and other flags with gcc.
Quote:
Originally Posted by batman4 View Post
the prob was that fwrite was making the code binary
so i have changed that function
you modified, show us (if you need help). And do not hesitate to say thanks
 
Old 07-24-2012, 07:53 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by NevemTeve View Post
Off: Please, use proper English words, it cannot be that hard.
It's also not that bad Just like you (I assume), I'm not native English speaking and did not have problems reading it.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
write a file fo data array (with float values) in a binary format in Python justinlapin Programming 3 04-06-2012 04:35 AM
Over my head with this problem - Access the data in .fbd data file BobNutfield Linux - Server 3 02-20-2011 01:48 PM
gnuplot, pull X data from file, specify Y data at cmd line? hedpe Programming 5 03-15-2007 11:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration