0

ok after completing pset3 I am about to start pset4 im on week 5 of lectures, before going into the pset4 i thought of practice a bit about structures and wrote this code. I idea was to save the entered data in a .txt file so that I can use it later on like we did in pset3 ./generate 1000 > numbers.txt. While trying the same for this code I am not getting the program to run with the following command ./structures > data.txt, but do see a empty .txt file created

include

include

define NUMBER 2

int main (void) { typedef struct { string name; string address; int id; } data;

data students[NUMBER];

for (int i = 0; i < NUMBER; i++)
{
    printf("Enter students name:\n ");
    students[i].name = GetString();

    printf ("Enter the address:\n ");
    students[i].address = GetString();

    printf("The ID number:\n");
    students[i].id = GetInt();
}

}

0

Furrukh, Your program is running. It may seem like it's just sitting there blinking at you, but actually, it is waiting for your first response. Of course the next question is: why isn't it displaying the "Enter the student..." prompt? Well, it's also waiting for the stdout buffer to be flushed. More on that later. Alas, even if you flush the buffer you STILL won't see the prompts if you redirect your program's output to data.txt. By calling the program this way ./program > data.txt you are redirecting everything the program outputs to the file data.txt, including your prompts, so you will never see them.

One solution to that problem would be to use the linux "tee" command. That allows you to see the output and redirect it at the same time. The syntax is ./program | tee data.txt.

Now back to "flushing the buffer". (full disclosure: I know how to do this programatically, and I understand the concept, but I cannot explain it technically.) If you add fflush(stdout); after each of your printf statements, you will see the prompts. More generally, you could add a setbuf(stdout, NULL); at the beginning of your main function (then you would not need the fflush). More disclosure: I did test both solutions in this small context and they worked.

After all that, don't be surprised if data.txt does not contain what you want or expect.

Finally, thank you. I have been wondering all through this course why tee wasn't working. Today I finally got the answer.

| improve this answer | |
  • just a question these commands like tee, fflush and setbuf. are they going to be taught further into this course or you knew about them from some other prior knowledge? cause I am just on week 5 and I am of those less comfortable type and didnt knew about these commands – Furrukh Jamal Nov 16 '15 at 6:54
  • And with setbut(stdout, NULL); command though I am getting my printfs on the terminal but using ./program | tee data.txt I am only getting the printfs displayed on the txt file, is there a way to get the inputs too saved on the txt file? – Furrukh Jamal Nov 16 '15 at 7:06
  • I knew about tee from prior knowledge. I learned about fflush and setbuf by googling and reading man pages. They are not in the course materials. The tee is only capturing what you send to output, as with printf. So you need to send your student names to output with printf too. – DinoCoderSaurus Nov 16 '15 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .