hey i am trying to write a project that reads in a file and prints to a file. file in is suppose to have a number seed and an integer to say how many to generate.
then i open the file, generate print and count in one loop then i print the report.
the trouble is that when i run the program for sum reason it is not able to read the file in.. i am confused and the project is due tomorrow. oh and i printed num1 and num2 to screen just to see what their values are and they are always zero. so it cannont generate anything.
Code:
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
bool openFiles(FILE **pFPIn, FILE **pFPOut);
int read(int* num1, int* num2, FILE *pFPIn);
int generate(int arr[], int num1, int num2, FILE *pFPOut);
void report(int arr[], FILE *pFPOut);
int main(void)
{
int arr[95];
int num1, num2;
bool success;
FILE *pFPOut;
FILE *pFPIn;
if (openFiles (&pFPIn, &pFPOut))
{
read(&num1, &num2, pFPIn);
generate(arr, num1, num2, pFPOut);
report(arr, pFPOut);
fclose(pFPOut);
fclose(pFPIn);
}
else
{ printf("\nThere has to be two integers in file test.txt");
printf("\n\n");
}
printf("%d%d", num1, num2);
return 0;
}
bool openFiles(FILE **pFPOut, FILE **pFPIn)
{
bool success = false;
if ((*pFPIn = fopen("test.txt", "r")) != NULL)
{
if ((*pFPOut = fopen("test.out", "w")) != NULL)
{
success = true;
}
else
{
printf("\nUnable to open \"test.out\" for writing.\n\n");
fclose(*pFPIn);
}
}
else
printf("\nUnable to open \"test\" for reading.\n\n");
return success;
}
int read(int* num1, int* num2, FILE *pFPIn)
{
fscanf(pFPIn,"%d%d", num1, num2);
return ;
}
int generate(int arr[], int num1, int num2, FILE *pFPOut)
{
int i;
int temp;
srand(num1);
for (i = 0; i < num2; i++)
{ temp = rand() % 95 + 32;
fprintf(pFPOut,"%c", temp);
arr[temp-32]++;
}
}
void report(int arr[], FILE *pFPOut)
{
int i;
for (i = 32; i < 126; i++)
{
fprintf(pFPOut, "'%c' %5d", i, arr[i-32]);
}
return ;
}