Temporary Files... How do they work?

Hello. I have been given an assignment to code a program to calculate an average from a list of numbers and, off the list, determine which numbers are less than, greater than, or equal to the average. The professor said it might be useful to use temporary files. I do not know anything about temporary files in C++. Could you please give me an introduction on how these work. I have checked the reference part of this site and the code they use in the examples is not what I have learned.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
/* tmpfile example */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  pFile = tmpfile ();

  // temporary file created. code here.

  fclose (pFile);
  return 0;
}


I have learned this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;
 
int main ()
{
    int limit;
 
	int counter = 1;
    
    cout << "Please enter a number between 1 and 30: ";
    cin  >> limit;
 
while(counter <= 5)
{    
    for (int lineCtrl = 1; lineCtrl <= limit; lineCtrl++)
        {
         for(int numCtrl = 1;
                 numCtrl <= lineCtrl;
                  numCtrl++)
         cout << numCtrl;
         cout << endl;
 			
        }
    for(int lineCtrl = limit; lineCtrl >= 1; lineCtrl--)
    	{
	    for(int numCtrl = 1;
	    		  numCtrl <= lineCtrl;
	    		  numCtrl++)
	    	cout << numCtrl;
	    	cout << endl;
    	}
    	counter++;
}

    return 0;
}


All guidance/help is greatly appreciated.
A temporary file is just an ordinary file that you remove() when you are done with it.

I have no idea why you would need to use a temporary file for this, though... perhaps he just wants you to try doing something new and useful.

To manage the temporary file, you have correctly chosen to use the tmpfile() function.

To use the file, just printf() your data to it like you would any other file.
Then use fseek() to reset the fp to the beginning of the file.
After that you can scanf() your data back from the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
  {
  int n, x, sum;
  FILE* f = tmpfile();
  if (!f)
    {
    fprintf( stderr, "Fooey! I could not create a temporary file!\n" );
    return 1;
    }

  /* Create 10 random numbers */
  srand( time( NULL ) );
  for (n = 0; n < 10; n++)
    {
    fprintf( f, "%d\n", rand() % 100 );
    }

  /* Go back to the beginning of the file */
  fseek( f, 0, SEEK_SET );

  /* Read and display the numbers to the user */
  for (n = 0; n < 10; n++)
    {
    fscanf( f, "%d", &x );
    printf( "%d: %d\n", n, x );
    }

  /* Go back to the beginning of the file */
  fseek( f, 0, SEEK_SET );

  /* Read and sum the numbers */
  sum = 0;
  for (n = 0; n < 10; n++)
    {
    fscanf( f, "%d", &x );
    sum += x;
    }
  printf( "sum = %d\n", sum );

  /* All done: close and delete the temporary file */
  fclose( f );
  return 0;
  }

Hope this helps.
Thank you for all of the code to give an example, but I have not learned C++ in that way... i guess the difference is inserting "using namespace std;" in the code, which I use. From what I learned it uses cout << "Hello World!"; to display a message to the screen.
Sorry I didn't pay more attention.
Using a FILE* (with C++ streams) isn't that hard, but it requires a little bit of esoteric work.
I'll hack something up for you later today.
Last edited on
Ok. Thank you.
JSYK I haven't forgotten you...

You can google around "stdiostream" and "stdiobuf" for interesting stuff -- I've hacked up my own for you.

I'm testing now... (I've got to figure out what I did wrong with the putback functionality)
Outside of that it is ready for your use.


However, what I'm doing really isn't important to your assignment. You can complete it without my fancy class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  // create a temporary file
  fstream tf( "mytemp.dat" );
    // This is a text file. If you need to store binary data in the file,
    // make sure to open it with the ios::binary flag:
    //   fstream tf( "mytemp.dat", ios::binary );

  // write to it
  tf << "Hello, world!\n";

  // read from it
  tf.seekg( 0, ios::beg );
  string s;
  getline( tf, s );

  // delete it
  tf.close();
  remove( "mytemp.dat" );

  // show what we have done
  cout << s << endl;

  return 0;
  }

Hope this helps.
Here's the thing I told you about:
http://www.cplusplus.com/forum/articles/22316/

Again, you really don't need this for a school assignment...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <iostream>
#include <string>

#include "stdiostream.hpp"

using namespace std;
using namespace duthomhas;

int main()
  {
  stdiostream tf( tmpfile() );

  tf << "Hello, world!\n";

  tf.seekg( 0, ios::beg );
  string s;
  getline( tf, s );

  cout << s << endl;

  return 0;
  }

:-)
Topic archived. No new replies allowed.