ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi my friends
I have written this program
#include<stdio.h>
#include<unistd.h>
FILE*a;
main()
{
int z,status;
a=fopen("result.txt","r+");
if(fork()==0)
{
printf("I am the child\n");
z=2+5;
fprintf(a,"%d",z);
}
else
{
wait(&status);
printf("I am the perant,I will prent the result\n");
fscanf(a"%d",&z);
printf("%d",z);
}
}
*****************
And the result was
I am the child
I am the perant
1073824832
**************
I think should be :
*****************
I am the child
I am the perant
7
******************
What is this number 1073824832!!!!
please help me if you can
Your fprintf statement (the one manishsingh4u commented about) is printing the address of z, (i.e., &z), not the value of z. The number you display is reasonable for an address value.
and please use the code tag. Your code is not readable.
Doesn't this look a lot better?
Code:
#include <stdio.h>
#include <unistd.h>
main()
{
FILE*a;
int z,status;
a=fopen("result.txt","r+");
if(!fork()){
printf("I am the child\n");
z=2+5;
fprintf(a,"%d",z);
}
else{
wait(&status);
printf("I am the parant,I will prent the result\n");
fscanf(a,"%d",&z);
printf("%d",z);
}
}
You made a very bad mistake: you didn't check the return value of fopen. On my system the program even segfaults (for some reason, fscanf doesn't seem to check for NULL-pointers).
fopen( ..., "r+" ) opens for reading and writing, but it doesn't create the file, and thus fails. If the file already exists, it works.
hi my friends
thnak you for replay
DO not worry about syntax errors my main problem is I don't want to prnit the adderss of z I want the perant to print value of 7
that is WHAT I WANT ,,
thank you again for replying...
I can't help you if you don't read my posts. It is not printing the address of z,
printf("%d",z);
is correct and will print 7. As I already wrote, the bug is in the fopen function call of and the lack of checking the return value of fopen.
You realize, of course, that just because a child shares a *copy* of the parent's data ... it doesn't share the data itself. The process space of a parent and a child are completely seperate.
#include<stdio.h>
#include<unistd.h>
int
main()
{
int status = 0, z = 8;
// the child will print "7"...
if(fork()==0)
{
z=2+5;
printf("I am the child, z= %d\n", z);
}
// ... the parent will print "8"
else
{
wait(&status);
printf("I am the parent, z= %d\n", z);
}
// ... and neither should print uninitialized garbage any more...
return 0;
}
Here's another version of your original program:
Code:
#include<stdio.h>
#include<unistd.h>
int
main()
{
FILE *fp = NULL;
int z = 0, status = 0;
// Fork the process
if(fork()==0)
{
// .. Child: write the value to a text file
z = 2 + 5;
printf("I am the child, z = %d\n", z);
if (fp = fopen ("result.txt", "w"))
{
fprintf (fp, "%d",z);
fclose (fp);
}
}
else
{
// .. Parent: wait until the child completes, then read the value from the file
wait (&status);
printf ("I am the parent, I will print the result...\n");
if (fp = fopen ("result.txt", "r"))
{
fscanf (fp, "%d", &z);
fclose (fp);
printf("z= %d", z);
}
}
return 0;
}
I have a very simple program which is not running fine as expected. I know there's some mistake from my side but where?
Code:
#include<stdio.h>
struct s
{
char name[20];
int age;
int fare;
char gender;
}s[3];
int main()
{
int i,sum=0;
for(i=0;i<3;i++)
{
printf("\nenter the name of passenger ");
scanf("%s",s[i].name);
printf("\nenter the age of the passenger ");
scanf("%d",&s[i].age);
printf("\nenter the fare to collect ");
scanf("%d",&s[i].fare);
printf("\nenter the gender of passenger ");
scanf("%c",&s[i].gender);
if(s[i].gender=='m')
{
sum=sum+s[i].fare;
}
}
printf("\nthe totale fare collected from male is : %d",sum);
return (0);
}
The program runs like this.
Code:
mann@Manish:~/Desktop$ cc struc.c -o struc
mann@Manish:~/Desktop$ ./struc
enter the age of the passenger 11
enter the fare to collect 222
enter the gender of passenger
enter the age of the passenger
mann@Manish:~/Desktop$ cc struc.c -o struc
mann@Manish:~/Desktop$ ./struc
enter the name of passenger Manish
enter the age of the passenger 25
enter the fare to collect 111
enter the gender of passenger
enter the name of passenger
I don't know why the program does not allow me to input for gender. that is it goes straight to name for next value for i.
Any suggesttions about where I am doing it wrong?
Last edited by manishsingh4u; 05-13-2006 at 03:24 PM.
There is a newline that is being imputed in there. Its left over from the first scanf call when you hit enter to compleate putting in text.
i get around this by doing something like this
Code:
int d;
char grab[10];
scanf("%s",grab);
sscanf(grab,"%d",d);
that way the first scanf will grab everything includeing the /n charecter. then the second will scan though the array and get the correct value you wanted.
of course this is not full proof but it at least stops anything from being left in the stdin buffer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.