-1

I declared a character fileName outside an if statement and used it in an if statement inside another if statement. I would like to know how do I retrieve and print the value I scanned and stored in char filename from outside the if block.

my code snippet:

char fileName[30] = "";
if(pid2==0){
    block of statements here
    if(pid3==0){
        block of statements here
    }else if(pid3==-1){
        block of statements here
    }else{
        printf ("\t CHILD2: Enter a filename: ");
        scanf ("%s", fileName);
        ...

    }
}else if(pid2==-1){
    block of statements here
}else{
    printf ("\t CHILD2: %s was successfully created!\n", fileName);
  • 1
    What little code you provided seems acceptable - `fileName` is declared above the scope of the `if` statements, so is accessible to all of them (though of course you never show setting it). Are you having problems with it? – Scott Mermelstein Feb 24 '14 at 05:51
  • I'm sorry, I deleted most of the code that I found not really needed to be presented here :(. Yes, the char fileName is accessible to everything but the value I scanned and stored inside the second if block(within the else statement) doesn't pass it to the outside. – Dan Rebuelta Feb 24 '14 at 05:54
  • Since you're looking at pids, is this in a bunch of different threads/processes? Once you fork a process, data changed in a variable in one thread is not reflected in the other thread. You need to look into some form of interprocess communication, like pipes. (Or storing the data in a known file, and opening it, though that gets messy if you try reading it before it's written). – Scott Mermelstein Feb 24 '14 at 05:55
  • :( my comment went through when I pressed the enter button... I would like to elaborate on my comment a few seconds ago. Inside the else statement, let's say I scanned "hello". Now I wanted it to be passed to the other else statement, but it's only giving me (null) or an illegal character. – Dan Rebuelta Feb 24 '14 at 05:55
  • so you want to jump from one `else` to other `else..if` or something else? Also see the comment of @ScottMermelstein for thread execution means change data in one thread not reflect to other. – Jayesh Bhoi Feb 24 '14 at 05:57
  • not really jump. just pass the value I got from the else statement to the other else statement. :) – Dan Rebuelta Feb 24 '14 at 05:58
  • @DanRebuelta how you break `if...else`? you might want to some `if..else..if` execuite in one thread other `else..if` in second thread by modified value in first `if..else..if` block right? might be you not divide like `if` execute in first thread and `else` in second one – Jayesh Bhoi Feb 24 '14 at 06:02
  • Only one block of an `if...else if...else` statement will execute at one time per function invocation. Are you running the function multiple times, or dealing with threading, or what? Bear in mind, if you're running multiple times, you initialize fileName to "" each time you run the function. You could make it static or global. – Scott Mermelstein Feb 24 '14 at 06:03
  • @JKB I'm not really sure if I got your question correctly, but inside the else statement(since I'm forking), I have wait(NULL); I wait for the child process to finish before the other child process executes. something like that :) – Dan Rebuelta Feb 24 '14 at 06:04
  • Ok... finally, we know you're forking. It would've helped tons to tell us that before. See my comment about using inter-process communication, pipes or named files. You can't communicate a variable from one thread to the other without inter-process communication. – Scott Mermelstein Feb 24 '14 at 06:06
  • I'm sorry I did not... it skipped my mind the say that I was forking. Where do I pipeline @ScottMermelstein I'm still very very new to pipelining and such stuff – Dan Rebuelta Feb 24 '14 at 06:09
  • Just google "fork pipe". The first thing that pops up is here on SO: http://stackoverflow.com/questions/4812891/fork-and-pipes-in-c – Scott Mermelstein Feb 24 '14 at 06:11
  • @DanRebuelta I just gave you a -1 on the question, because you didn't explain that you were looking to communicate across a fork in it. While someone who reads these 11 comments will eventually catch on, the question itself is currently poorly done. If you update the question, I'll change the - to a +. – Scott Mermelstein Feb 24 '14 at 06:22

2 Answers2

0

If this isn't for a homework assignment that requires proper interprocess communication, and you just need to get it done, you could do something like the following:

char whereIStoreMyData = "file.tmp"

// fork...

if(pid2==0){
    block of statements here
    if(pid3==0){
        block of statements here
    }else if(pid3==-1){
        block of statements here
    }else{
        printf ("\t CHILD2: Enter a filename: ");
        scanf ("%s", fileName);
        FILE* fp = fopen(whereIStoreMyData, "w");
        fprintf(fp, "%s\n");
        fclose(fp);

        ...

    }
}else if(pid2==-1){
    block of statements here
}else{
    FILE* fp;
    while ((fp = fopen("whereIStoreMyData", "r")) == NULL) {
        sleep(1000) // wait 1 second
    }
    fscanf(fp, "%s\n", fileName);
    fclose(fp);
    printf ("\t CHILD2: %s was successfully created!\n", fileName);
}

This is not ideal code (you should use fgets, or some other better function, e.g.), but shows the basic logic flow concept.

If you need something more formal, look into forms of interprocess communication (IPC), such as pipes (most recommended for this) or shared memory. There are tons of references on the web, among them fork() and pipes() in c.

Community
  • 1
  • 1
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
-1

use gets(fileName); instead of scanf ("%s", fileName); because scanf(); will terminate input while a space appears in your input while gets() will terminate while you press ENTER. hope this will work. use a large number instead of 30 if you want to input more than 29 character. i.e: change fileName[30]; to fileName[90]

Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72