By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

C By Dissection : Accessing Input and Output files accessing student grade averages

P: 17
I am wondering if someone can help me with the following C programming problem.

I am trying to develop a program dealing with input/output and files .

I am trying to develop a program that creates a file of randomly distributed numbers. The file name is to be entered interactively. Program should use three functions.

I have develop the following modules, but am experiencing errors.

get.c

void get_info(char *filename, int *n_ptr)
{
printf(“\n%s\n\n%s”,
printf(“How many random number would you like? “);
scanf(“%d”, n_ptr);
printf(“\nIn what file would like them? “);
scanf(“%s”, filename);
}

careful.c

char reply[2];
FILE *fp;
{
If (strcmp(mode, “w”) == 0
&& (fp = fopen(filename, “r”)) != NULL) {
fclose(fp);
printf(“\nFile exist. Overwrite it? “);
scanf(“%1s”, reply);
if (*reply != ‘y’ && *reply != ‘Y’) {
printf(“\nBye!\n\n”);
exit (1);
}
}
fp = gfopen(filename, mode);
return fp;
}








graceful.c

FILE *gfopen(char *fn, char *mode)
{
FILE *fp;

if ((fp = fopen(fn, mode)) == NULL) {
fprintf(stderr, “Cannot open %s – bye!\n”, fn);
exit (1);
}
return fp;
}


main.c

#include "careful.h"

main(void)
{
char line[MAXLINE];
int error, n;

do {
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);
error = sscanf(line, "%d", &n) != 1 || n <= 0;
if (error)
printf("\nERROR: Do it again.\n");
} while (error);
}


prn_rand.c

#include "careful.h"

void prn_rand(FILE *ofp, int n)
{
printf("\nHello from prn_rand()\n\n");
}
Sep 26 '06 #1
Share this Question
Share on Google+
2 Replies


100+
P: 144
What kind of errors are you getting? Compile errors and runtime errors are important for debugging. This applies to you previous post as well. It is good to see that you have made an attempt at coding the problem, and have supplied your code. However, don't expect anyone to scrutinize the code in your homework when you only supply a vague assertion that there are errors. You are more likely to get help when you supply as much information as possible.
Sep 27 '06 #2

P: 17
What kind of errors are you getting? Compile errors and runtime errors are important for debugging. This applies to you previous post as well. It is good to see that you have made an attempt at coding the problem, and have supplied your code. However, don't expect anyone to scrutinize the code in your homework when you only supply a vague assertion that there are errors. You are more likely to get help when you supply as much information as possible.
I will try to include the errors next time. But it wouldn't have helped in this matter, the problem was in the .h file. It seem I had put an colon after a function declaration vice a semi colon. I had also used printf, when I should have used fprintf when dealing with a file.

Thank you for responding.
Sep 29 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.