Unix Programming - problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2005 > problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author problem
hirenshah.05@gmail.com

2005-09-27, 5:55 pm

I am trying to a program which takes the data from user and write in
file. I have written the code below. but when i am displaying data from
file; only first data is correct; others are random. i think there is
problem while writing in file.



struct st
{
char ch[60];
int n;
};

int main()
{
st s;
printf("enter");
scanf("%s",s.ch);
printf("enter no.");
scanf("%d",&s.n);
adddata(&s);
}



add data(st *x)
{
int fd;
fd=open("records",O_CREAT|O_RDWR,0644);
close(fd);
fd=open(path,O_CREAT|O_APPEND|O_RDWR);
fdw=write(fd,x,sizeof(s));
close(fd);
}


Then i have created another function to read from file "records" and
display all data entered.

But when i am adding data , Only first data enter is correct ; rest of
the data are random. I am not getting where is problem.

Pascal Bourguignon

2005-09-27, 5:55 pm

hirenshah.05@gmail.com writes:

> I am trying to a program which takes the data from user and write in
> file. I have written the code below. but when i am displaying data from
> file; only first data is correct; others are random. i think there is
> problem while writing in file.
>
>
>
> struct st
> {
> char ch[60];
> int n;
> };
>
> int main()
> {
> st s;
> printf("enter");
> scanf("%s",s.ch);


If the user types:

I-am-trying-to-do-a-program-which-takes-the-data-from-user-and-write-in-a-file.

then your program will crash because scanf will overwrite s.n and the
stack with the characters beyond the 60th.

NEVER use scanf "%s" !

It'd use fgets, or scanf("%59s",...);

> printf("enter no.");
> scanf("%d",&s.n);
> adddata(&s);


You've declared that main returns an integer so return it!
return(0);

> }
>
>
>
> add data(st *x)


what is the add type?
Why do you send us programs that don't even compile successfully?

> {
> int fd;
> fd=open("records",O_CREAT|O_RDWR,0644);
> close(fd);


And what would happen if some other process executed: rm records
right now? This open/close is useless!

> fd=open(path,O_CREAT|O_APPEND|O_RDWR);


What is path?
Why do you send us programs that don't even compile successfully?

> fdw=write(fd,x,sizeof(s));


What is s? What is fdw?
Why do you send us programs that don't even compile successfully?

> close(fd);
> }


Where is defined adddata?
Why do you send us programs that don't even compile successfully?


> Then i have created another function to read from file "records" and
> display all data entered.
>
> But when i am adding data , Only first data enter is correct ; rest of
> the data are random. I am not getting where is problem.



--
__Pascal Bourguignon__ http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.
Wayne C. Morris

2005-09-27, 5:55 pm

In article <1127835122.959124.312730@g49g2000cwa.googlegroups.com>,
hirenshah.05@gmail.com wrote:

> I am trying to a program which takes the data from user and write in
> file. I have written the code below. but when i am displaying data from
> file; only first data is correct; others are random. i think there is
> problem while writing in file.

[snip]
> add data(st *x)
> {
> int fd;
> fd=open("records",O_CREAT|O_RDWR,0644);
> close(fd);
> fd=open(path,O_CREAT|O_APPEND|O_RDWR);
> fdw=write(fd,x,sizeof(s));
> close(fd);
> }


The code you posted won't even compile, much less run. Next time, please
copy & paste the code from your program instead of retyping it; that way,
we can be sure we're commenting on the real mistakes in your program,
instead of the mistakes you made while retyping it.

I assume you meant sizeof(x) instead of sizeof(s). If so, that's your main
problem: x is a pointer, so sizeof(x) gives you the size of a pointer (4 on
most systems), NOT the size of the structure. As a result, your program
only writes the first 4 bytes of the structure to the file. You want to
use sizeof(*x).
hirenshah.05@gmail.com

2005-09-27, 5:55 pm

sorry about previous program here is my actual code


#define FLIGHT_NAME_MAX 60
#define TAIL_NUMBER_DIGITS 6
typedef struct flight_s
{
char flight[FLIGHT_NAME_MAX];
char tail_number[TAIL_NUMBER_DIGITS];
float price;
int seats;
int first, business;
} flight_t;
enum field_e { flight, tail_number, price, seats, first, business};
extern int add_flight(flight_t*);
extern int del_flight(enum field_e field, void *);
extern flight_t* get_item(enum field_e field, void *);
~



#include<stdio.h>
#include"inventory.h"
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd,a,c;
flight_t st;
void display();
void a_delete();

char path[]="records";
do{
printf("\t\tINVENTORY OF FLIGHTS\n");
printf("\n1. ADD\n2. DELETE\n3. LIST\n4. EXIT");
scanf("%d",&c);


switch(c)
{
case 1:
printf("Enter Flight Name\n");
scanf("%s%",st.flight);
printf("Enter Tail Number\n");
scanf("%s",st.tail_number);
printf("Enter Price\n");
scanf("%f",&st.price);
printf("Enter Total Seats\n");
scanf("%d",&st.seats);
printf("Enter First Class Seats\n");
scanf("%d",&st.first);
st.business=st.seats-st.first;
a=add_flight(&st);
if(a=0)
printf("ERROR ENTRY NOT ADDED");
else
printf("ENRTY ADDED SUCESSFULLY");
free(&st);
break;
case 2: a_delete();
break;
case 3:
display();
break;
case 4: exit(1);
}
}while(1);
}




#include<stdio.h>
#include"inventory.h"
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/stat.h>

int add_flight(flight_t *x)
{
int fd=0,fdw;
char path[]="records";
fd=open(path,O_CREAT|O_RDWR,0644);
if(fd<0)printf("error");
close(fd);
fd=open(path,O_CREAT|O_APPEND|O_RDWR);
if(fd<0)perror("open");
fdw=write(fd,x,sizeof(flight_t));
if(fdw<=0)
perror("write");
close(fd);
return 1;
}


void display()
{
flight_t z;
struct stat tt;
int n=0,i,fd;
char path[]="records";
stat(path,&tt);
i=tt.st_size;
fd=open(path,O_CREAT|O_RDONLY,0644);
if(fd<0)
printf("error");
printf("Flight Name\tTail Number\tPrice\tTotal Seats\tFirst
Class\tBusiness\n")
;
while(i>=84)
{
lseek(fd,n,SEEK_SET);
read(fd,&z,sizeof(flight_t));

printf("%s\t\t%s\t\t%f\t%d\t%d\t\t%d\n",z.flight,z.tail_number,z.price,z.seats,
z.first,z.business);
n=n+85;
i=i-84;
}
close(fd);

}


void a_delete()
{
int n=0,i,fd,fdt,s;
char path[]="records";
flight_t tmp1,tmp2,buffer;
struct stat tt1,tt2;
printf("\nenter flight name to be deleted");
scanf("\n%s",tmp1.flight);
printf("%s",tmp1.flight);
stat(path,&tt1);
i=tt1.st_size;
fdt=open("temp",O_CREAT|O_RDWR,0644);
close(fdt);
fd = open(path,O_CREAT|O_RDWR,0644);
while(i>=84)
{
lseek(fd,n,SEEK_SET);
read(fd,&tmp2,sizeof(flight_t));
if((s=strcmp(tmp1.flight,tmp2.flight)!=0))
{
fdt=open("temp",O_CREAT|O_RDWR|O_APPEND);
write(fdt,&tmp2,sizeof(flight_t));
close(fdt);
}
n=n+85;
i=i-84;
}
close(fd);
stat("temp",&tt2);
i=tt2.st_size;
n=0;
fd=open(path,O_CREAT|O_RDWR|O_TRUNC,0644
);
close(fd);
fdt=open("temp",O_CREAT|O_RDWR,0644);
while(i>=84)
{
lseek(fdt,n,SEEK_SET);
fd=open(path,O_CREAT|O_RDWR|O_APPEND);
read(fdt,&buffer,sizeof(flight_t));
write(fd,&buffer,sizeof(flight_t));
close(fd);
n=n+85;
i=i-84;
}
close(fdt);
remove("temp");
}

Pascal Bourguignon

2005-09-27, 5:55 pm

hirenshah.05@gmail.com writes:

> sorry about previous program here is my actual code


Not good enough. You have concatenated several file. We couldn't
split the blob without spending some useless time on it. Are you a
chronophage?

Are you too lazy to write a shell one-liner?

for f in *.[hc] ; do echo "----------($f)-------------" ; cat $f ; done

or just:

shar *.[hc]



--
__Pascal Bourguignon__ http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
Floyd L. Davidson

2005-09-27, 5:55 pm

hirenshah.05@gmail.com wrote:
>sorry about previous program here is my actual code


Can you actually *read* that code? I can't imagine how.

You need to find a tutorial on programming *style*. Learn about
indenting, use of white space, how to check for errors, and why
you don't ever want to use scanf() for keyboard input.

I would also suspect that you are not compiling with maximum
warnings enabled on your compiler.

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com