C to C++

Hi guys,

I'm only familiar with syntax in C++ and I have a program here in C that I need to use. I need to edit this code for my needs but I'm having difficulties as I don't know anything about C.

Basically, the program below accepts a text file as an input and outputs another text file (wherein you need to specify the file name). Since I'm doing a loop for this, I want to modify this code so that it won't have to ask me for the input file name and the output file name. I want the input file name and output file name to be fixed in the code below.

Also, when we run a program, it shows "Press any key to continue" after it executes the code. How do you skip this part so that you don't need to press enter every time? (I'm going to call this program in a loop that's why.)

I'd really appreciate help for this. Thank you very much!

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include "fft.c"

int main()
{
  int i;                    /* generic index */
  char file[FILENAME_MAX];  /* name of data file */
  int N;                    /* number of points in FFT */
  double (*x)[2];           /* pointer to time-domain samples */
  double (*X)[2];           /* pointer to frequency-domain samples */
  double dummy;             /* scratch variable */
  FILE *fp;                 /* file pointer */

  /* Get name of input file of time-domain samples x. */
  printf("Input file for time-domain samples x(n)? ");
  scanf("%s", file);

  /* Read through entire file to get number N of points in FFT. */
  if(!(fp = fopen(file, "r")))
    {
      printf("   File \'%s\' could not be opened!", file);
      exit(EXIT_FAILURE);
    }
  N=0;
  while(fscanf(fp, "%lg%lg", &dummy, &dummy) == 2) N++;
  printf("N = %d", N);

  /* Check that N = 2^n for some integer n >= 1. */
  if(N >= 2)
    {
      i = N;
      while(i==2*(i/2)) i = i/2;  /* While i is even, factor out a 2. */
    }  /* For N >=2, we now have N = 2^n iff i = 1. */
  if(N < 2 || i != 1)
    {
      printf(", which does not equal 2^n for an integer n >= 1.");
      exit(EXIT_FAILURE);
    }

  /* Allocate time- and frequency-domain memory. */
  x = malloc(2 * N * sizeof(double));
  X = malloc(2 * N * sizeof(double));

  /* Get time-domain samples. */
  rewind(fp);
  for(i=0; i<N; i++) fscanf(fp, "%lg%lg", &x[i][0], &x[i][1]);
  fclose(fp);

  /* Calculate FFT. */
  fft(N, x, X);

  /* Print time-domain samples and resulting frequency-domain samples. */
  printf("\nx(n):");
  for(i=0; i<N; i++) printf("\n   n=%d: %12f %12f", i, x[i][0], x[i][1]);
  printf("\nX(k):");
  for(i=0; i<N; i++) printf("\n   k=%d: %12f %12f", i, X[i][0], X[i][1]);

  /* Clear time-domain samples and calculate IFFT. */
  for(i=0; i<N; i++) x[i][0] = x[i][1] = 0;
  ifft(N, x, X);

  /* Print recovered time-domain samples. */
  printf("\nx(n):");
  for(i=0; i<N; i++) printf("\n   n=%d: %12f %12f", i, x[i][0], x[i][1]);

  /* Write frequency-domain samples X to a file, if desired. */
  printf("\nOutput file for frequency-domain samples X(k)?"
         "\n   (if none, abort program): ");
  scanf("%s", file);
  if(!(fp = fopen(file, "w")))
    {
      printf("   File \'%s\' could not be opened!", file);
      exit(EXIT_FAILURE);
    }
  for(i=0; i<N; i++) fprintf(fp, "%23.15e  %23.15e\n", X[i][0], X[i][1]);
  fclose(fp);
  printf("Samples X(k) were written to file %s.", file);

  /* Free memory. */
  free(x);
  free(X);

  return 0;
}
Last edited on
It won't do that normally, it must be your calling environment that is doing that.

EDIT: Oops, failed to actually answer your main question.

I'd just make it take some command line arguments and use those.
Last edited on
Hi Secondrate,

I just want to make a comment. There isn't really that terrible a difference between C and C++. At least in terms of the above program. I started with C and coded it for many years before I learned C++. At this point in the game I never compile my programs as C programs anymore, but rather as C++ programs. However, a lot of my code looks exactly as what you posted. The reason for this is that when I moved from C to C++ I never gave up stdio.h and stdlib.h. I've always heartily detested anything having to do with iostream, ifstream and any other rot beginning with i. So what I'm saying is that your problem isn't that you don't understand C. Quite the contrary. If you know C++ you know C. What you don't know how to use however is stdio.h and stdlib.h; and these were libraries incorporated into C++ from C. Its just that C++ only programmers simply never learned them.

All you really need to do is look up such functions as printf(), fscanf(), fopen(), rewind(), etc. They are easy to use. And if you fight with everything you've got in you against the almost uncontrolable urge/reflex to #include <iostream> at the top of your file, you'll find the program compiles to an exe about 1/20 the size of an iostream bloat job, and it'll run faster too.
Topic archived. No new replies allowed.