LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-10-2006, 09:36 AM   #1
clunix
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Rep: Reputation: 0
What is the problem here


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
 
Old 04-10-2006, 09:44 AM   #2
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
mann@Manish:~$ cc test.c -o test
test.c: In function ‘main’:
test.c:18: error: syntax error before string constant

Last edited by manishsingh4u; 04-10-2006 at 09:48 AM.
 
Old 04-10-2006, 10:02 AM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 04-10-2006, 10:24 AM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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);
	}
}
 
Old 04-10-2006, 12:37 PM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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.

Last edited by addy86; 04-10-2006 at 12:46 PM.
 
Old 04-10-2006, 01:06 PM   #6
clunix
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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...
 
Old 04-10-2006, 01:16 PM   #7
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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.
 
Old 04-11-2006, 12:49 AM   #8
clunix
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
thank you mr.add86
Let's us forget writing and reading from file
I will write program without files

#include<stdio.h>
#include<unistd.h>
main()
{
int status,z;

if(fork()==0)
{
printf("I am the child\n");
z=2+5;
}
else
{
wait(&status);
printf("I am the perant\n");
printf("Z=%d",z);
}
}

here it will not printf 7
it will print something like address
 
Old 04-11-2006, 01:16 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh - Z remains uninitialized in the parent.

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.

Last edited by paulsm4; 04-11-2006 at 01:18 AM.
 
Old 04-11-2006, 01:24 AM   #10
clunix
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
ok my friend"paulsm4",,,,
How can I solve this problem????????????
 
Old 04-11-2006, 01:38 AM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Code:
#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;
}

Last edited by paulsm4; 04-11-2006 at 01:49 AM.
 
Old 05-13-2006, 03:18 PM   #12
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
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.
 
Old 05-13-2006, 03:35 PM   #13
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.

Last edited by exvor; 05-13-2006 at 03:38 PM.
 
Old 05-13-2006, 03:46 PM   #14
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
Thanks exvor for your help.
 
  


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
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

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

All times are GMT -5. The time now is 11:38 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