File IO code not outputing my text file! Help!

My coding isn't outputing first part the text file. This is what I have.

/* June 15th */

#include <stdio.h>


int main(){


FILE *weigh;
int num, sum = 0;

weigh = fopen("weightlog.txt", "r");
fscanf(weigh, "Goal Weight%d\n",&num);
while(num != 0){
sum += num;
fscanf(weigh,"%.2f\n", &num);
}
printf("Goal Weight: %dlbs\n", sum);
fclose(weigh);

system("PAUSE");
return 0;
}

This is what the text file looks like:

Weightlog.txt

Program Output

135
5
158.2 157.5
157.0 156.5
156.6 151.2
151.0 148.9
148.7 146.4
0

And I am trying to output first part of text which is 135 which is the goal weight. This is how it is supposed to output:

Progress Report After 5 weeks
----------------------------------------
Goal Weight: 135 lbs
Total Weight Lost: 11.0 lbs
Min lost in a week: 0.5 lbs
Max lost in a week: 5.4 lbs
Avg weight lost per week: 2.2 lb
Weight Goal is not met yet
Weight still to be lost: 11.4 lbs
Last edited on
Please use code tags they are the <> under format. Also that is C code not C++, are you aware of that? I ask because I seen a few people post code like that and thought it was C++ and it wasnt, so just want to make sure your not learning the wrong code.
Yeah I am sorry. I just noticed.
thats fine, just wanted to make sure. are you trying to learn C or C++?
The C++ equivelant to that is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream> //needed for ofstream and ifstream

using namespace std;

int main()
{
    ofstream save;

    save.open("weightlog.txt");

    string yourText = "Text";

    save << yourText << endl;

    save.close();

    cin.get();

    return 0;
}


If your trying to learn C++ of course.
It was for C.
I have this so far, but I am having a hard time figuring out the average or how to get about it in C.

#include <stdio.h>


int main(){


FILE * filePtr = NULL;

/* Name of file I am creating */
char * weigh = "Weightlog.txt";

// Creating file for weightlog
filePtr = fopen(weigh, "w");

/* Data for the text file */
fprintf(filePtr, "135\n");
fprintf(filePtr, "5\n");
fprintf(filePtr, "158.2 157.5\n");
fprintf(filePtr, "157.0 156.5\n");
fprintf(filePtr, "156.6 151.2\n");
fprintf(filePtr, "151.0 148.9\n");
fprintf(filePtr, "148.7 146.4\n");
fprintf(filePtr, "0\n");

/* Now lets close the file, so we can open it again in read mode safely */
fclose(filePtr);


/* Reading file */
filePtr = fopen(weigh, "r");

/* Get the first number and print it off */
int goal = 0;
fscanf(filePtr, "%d", &goal);
printf("Progress Report After 5 weeks\n");
printf("-----------------------------\n");
printf("Goal Weight: %d lbs\n", goal);


/* Close the file and exit */
fclose(filePtr);





system("PAUSE");
return 0;
}
Topic archived. No new replies allowed.