Having strange behaving programs,at the bottom you can find them, but here is short explenation.
Main program will present a menu from which you can pick operation to do. When I choose 1 it is suppouse to create file of certain name(not exactly, you have to complile score.c program and on command line pass name of file which you wish to create, I'm using "score") with new 5 accounts of zero value, dispalay its inode number and syze than get back to main program.
What is does is :
- display option menu and ask for number of operation to perform
- display again menu
- its displays info about new accounts and hang, thats how is it looks like
here is output
* * * * * * * * * * * * * * * * * * * * * * * *
1. New Game
2. Display current score
3. Enter new score
4. Exit game
* * * * * * * * * * * * * * * * * * * * * * * *
Choose number for operation to perform : 1
* * * * * * * * * * * * * * * * * * * * * * * *
1. New Game
2. Display current score
3. Enter new score
4. Exit game
* * * * * * * * * * * * * * * * * * * * * * * *
Choose number for operation to perform : 0
0
0
0
0
Inumber : 7550163
Size in bytes : 20
However that is not true, after above discribed actions is actualy waiting for new number for operation. So it is just matter of processing.
Question
How can I force program to display information in correct order!!!
MENU ->if 1 -> CREATE NEW FILE -> NEW MENU
MAIN program
//MAIN PROGRAM
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void newGame()
{
if(fork()==0)
{
execl("score.out","score.out","score",NULL);
printf("\nback to newGame\n");
}
}
main ( int argc, char *argv[])
{
int fd, i, status;
do
{
printf("* * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("1. New Game\n");
printf("2. Display current score\n");
printf("3. Enter new score\n");
printf("4. Exit game\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * *");
printf("\n\nChoose number for operation to perform : ");
scanf("%d",&status);
switch(status){
case 1: newGame();
break;
case 2: printf("choosen display score\n");
break;
case 3: printf("choosen enter new value\n");
break;
default:
break;
}
}while( status != 4);
}
SCORE.C
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
main( int argc, char *argv[])
{
struct stat buf;
int fd, i;
int a=0;
fd = open(argv[1], O_RDWR | O_CREAT, 0600);
if (fd<0) {
perror("cannot open file");
exit(1);
}
for(i=0; i < 5; i++)
{
write(fd,&a, sizeof (int));
printf("%d\n", a);
}
close(fd);
if(stat(argv[1], &buf) !=0)
{
perror(argv[1]);
exit(2);
}
printf("Inumber : %d\n", buf.st_ino);
printf("Size in bytes : %d\n", buf.st_size);
}