Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C programming help?
I have this coding school project I am doing and problem 1 and 2 work fine, but 3 I have a lot of problems with since I am struggling to understand file pointers.
First the idea of the assignment:
You have to have a program read a file that has a different person's info on each line like this:
gender (F or M), height (cm), weight (kg)
M, 175, 79.45
Convert height and weight to pounds and feet and write that into a new document that displays is like so
Gender (F/M), Height (feet' inches"), weight (lbs)
F, 5' 3", 148.375 lbs
For every line in the File and then give the un-converted ranges of height and weight.
I am not sure how to get multiple values from file at once or the feet and inch thing in output.
This is what I have so far and is my logic sound or based off a decent understanding of pointers?
after defining everything inp = fopen(FILE_NAME, "R")
either have outp = ... or straight to loop
fscanf to get the gender(char), weight (double) and height (double), but don't know have to get multiple values from a value per line. Do I have multiple scanf?s
if statements to establish the max and min of the weight and height.
functions to get converted.
I think I have to use division and remainders to get height in the right format.
fprintf to write them into a new file
end loop at end of FILE
have outp = if not earlier or use fclose on both
printf to show the Range of weight and height
1 Answer
- husoskiLv 72 years agoFavorite Answer
The way I'd do this is to use fgets() to read a whole line as a string into a char[] array, then use either sscanf() or strtok() to extract the fields. That way, if there's a problem with one line you can display the whole input line that caused the problem. It's harder for a problem on one line to cause an error to occur on some later line.
If you haven't studied those library funcitons (fgets and sscanf are in <stdio.h> and strtok is in <string.h>) then probably you're supposed to use plain fscanf(). You can scan a single, valid, input line with:
char gender;
double cm_height, kg_weight;
int n;
FILE *infile;
. . . .
n = fscanf(infile, " %c , %lf , %lf", &gender, &cm_height, &kg_weight);
if (n != 3)
{
. . . [ some error occurred--not all fields formatted correctly ]
}
printf("read: gender=%d, cm_height=%g, kg_weight=%g\n", gender, cm_eight, kg_weight);
I just typed that on the fly, to give you some ideas. Please excuse any typos.
The key idea there is the (f)scanf format. You should know what the %c and %lf fields do, but there are two facts about scanf formats you might not have seen. Any whitespace character in the format causes ?scanf to discard whitespace characters in the input up to (but not including) the next nonblank. "Whitespace" includes spaces, tabs and newlines.
Any non-whitespace character causes scanf to match that specific character in the input. Nothing is stored and the matching character is discarded; but if the character does NOT match then ?scanf() halts without converting any more fields. That format string has commas to cause matching (and skipping) the comma field separators, with spaces around them to allow for accidental spaces during data entry.
Another point that doesn't always get taught is that each scanf function returns an int reporting how many fields were converted and stored. That's why the example tests for a return value of 3.