producer-consumer problem

create ascii code and write it to a file, pass pointer

Page 1 of 1

3 Replies - 34807 Views - Last Post: 20 September 2007 - 02:18 AM Rate Topic: -----

#1 mayoya   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 18-September 07

producer-consumer problem

Posted 18 September 2007 - 05:08 PM

I need to create a function void producer(char, file) produce the ASCII code of a digits (0..9) and write to a file (one character at a time in a loop),
and a function char consumer(file) that will consume one character from the file.
The main program should open the file (and close it at the end), and use two variables to decide how many times each of the producer/consumer are activated

I do not know how to pass a file or POINTER to a file between functions thants my code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
FILE * pFile;
int main()
{

	pFile = fopen ("myfile.txt","w"); //Create a file name "myfile.txt" in the current directory.
	if (pFile!=NULL)
	{
		int no_prod=0;
		for (int i = 0; i<10; i++)
		{
			fprintf(pFile, "%d is Char: %c\n", i, (char) i); //Write data to the the file...
		}
		no_prod = no_prod + 1;
		printf("the producer is called ", no_prod , "times \n");
	}
	
	 if (pFile!=NULL)
	{
		int no_consumer = 0;
		for (int i = 0; i<10; i++)
		{
			  printf("%d is Char: %c\n", i, (char) i);
		}
		 no_consumer = no_consumer + 1;
		 printf("the consumer is called ", no_consumer, "times \n");
	}
	fclose (pFile);
	system("pause");
	return 0;
}

void producer (char)
{

}
char consumer ()
{
	char x;
	return (x);
}

This post has been edited by mayoya: 19 September 2007 - 05:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: producer-consumer problem

#2 Armonster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-September 07

Re: producer-consumer problem

Posted 18 September 2007 - 05:36 PM

View Postmayoya, on 18 Sep, 2007 - 08:08 PM, said:

I need to create a function void producer(char, file) produce the ASCII code of a digits (‘0’..“9”) and write to a file (one character at a time in a loop),
and a function char consumer(file) that will consume one character from the file.
The main program should open the file (and close it at the end), and use two variables to decide how many times each of the producer/consumer are activated


Display the code you have so far. Read the forum rules.

This post has been edited by Armonster: 18 September 2007 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 vijaysnkar   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 11-August 07

Re: producer-consumer problem

Posted 19 September 2007 - 12:34 AM

Do paste your code within the code tag




it will be easy to read your query.

View Postmayoya, on 18 Sep, 2007 - 05:08 PM, said:

I need to create a function void producer(char, file) produce the ASCII code of a digits (0..9) and write to a file (one character at a time in a loop),
and a function char consumer(file) that will consume one character from the file.
The main program should open the file (and close it at the end), and use two variables to decide how many times each of the producer/consumer are activated

I do not know how to pass a file or POINTER to a file between functions thants my code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
FILE * pFile;
int main()
{

pFile = fopen ("myfile.txt","w"); //Create a file name "myfile.txt" in the current directory.
if (pFile!=NULL)
{
int no_prod=0;
for (int i = 0; i<10; i++)
{
fprintf(pFile, "%d is Char: %c\n", i, (char) i); //Write data to the the file...
}
no_prod = no_prod + 1;
printf("the producer is called ", no_prod , "times \n");
}

if (pFile!=NULL)
{
int no_consumer = 0;
for (int i = 0; i<10; i++)
{
printf("%d is Char: %c\n", i, (char) i);
}
no_consumer = no_consumer + 1;
printf("the consumer is called ", no_consumer, "times \n");
}
fclose (pFile);
system("pause");
return 0;
}

void producer (char)
{

}
char consumer ()
{
char x;
return (x);
}

Was This Post Helpful? 0
  • +
  • -

#4 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: producer-consumer problem

Posted 20 September 2007 - 02:18 AM

View Postmayoya, on 18 Sep, 2007 - 05:08 PM, said:

I need to create a function void producer(char, file) produce the ASCII code of a digits (0..9) and write to a file (one character at a time in a loop),
and a function char consumer(file) that will consume one character from the file.
The main program should open the file (and close it at the end), and use two variables to decide how many times each of the producer/consumer are activated

I do not know how to pass a file or POINTER to a file between functions thants my code:

(snip)





the format for a regular old function in C/C++ is :

return_type functionName(argument_1_type argument_1, ...)


if you want to pass a file pointer to a function and have it return a character, the syntax would be

char functionName(FILE* filePointer) {
	//  do something here, then return a char
}



you would then call the function in the body of your program (the main function) using

char returnedValue=functionName(pointerToFile);



in your case, pointerToFile would be pFile.

you also need to either prototype your functions before the main function, or write the entire body of the function before the main function (less preferable).

i would suggest searching the web or these forums on creating and using functions in C/C++.

-jjh

This post has been edited by jjhaag: 20 September 2007 - 02:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1