|
查看: 940|回复: 1
|
C programming Open a file
[复制链接]
|
|
|
嗨,
我的 Load function 有问题, 谁可以帮我修改。
load function 那边是想打开 txt file 然后display成这样
pit location pit depth
--------------- ------------
Bear-Track 01 -5.18
Bear-Track 03 -32.30
Bear-Track 04 -2.34
#include <stdbool.h>
#include <stdlib.h>
void load(char, int);
double depth(double seconds2Bottom);
void menuSelect(char selection);
bool yesNo(const char prompt[]);
int main(void)
{
char selection;
double toBottom;
char name;
//char repeat;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n\n");
printf("Enter your choice (D/S/L/P/Q): ");
scanf("%c", &selection);
menuSelect(selection);
/*
if((selection) !='d' && (selection) != 'D')
printf("invalid entry");
else
while(1);
*/
/* Time to reach the bottom of the shaft */
do
{
printf("Entermine shaft location or name:");
scanf("%s", &name);
printf("\nEnter time to bottom: ");
scanf("%lf", &toBottom);
fflush(stdin);
printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));
return 0;
}
double depth(double seconds2Bottom)
{
/* Acceleration of gravity */
const double G = 9.81;
return -0.5 * G * seconds2Bottom * seconds2Bottom;
}
void load(char name, int seconds)
{
FILE *loadFile;
loadFile = fopen("Bear-Eldorado.pit", "w");
if (loadFile==NULL)
exit(1);
fprintf(loadFile, "%s %i\n", name, seconds);
fclose(loadFile);
return;
}
void menuSelect(char selection)
{
double seconds2Bottom;
char name;
int seconds;
switch(selection)
{
case 'd':
case 'D':
depth(seconds2Bottom);
break;
case 'l':
case 'L':
open(name, seconds);
break;
case 'q':
case 'Q':
exit(0);
}
}
bool yesNo(const char prompt[])
{
char answer;
printf("\n%s (Y/N)? ", prompt);
answer = getchar();
fflush(stdin);
return (answer == 'y' || answer == 'Y');
} |
|
|
|
|
|
|
|
|
|
|
发表于 27-10-2008 09:40 PM
|
显示全部楼层
load的用途是从txt file读取资料吧?
你写的是load是写入资料喔 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|