Crash after first scanf novice c programming - c

My code ( gist link )
//pre-processor directives
#include <stdio.h>
#include <math.h>
//Main function
int main() {
int month, day, year, lowest_temp, highest_temp;
float temp;
char fname[30];
FILE * ifp;
printf("Tell me about your dragon's preferred temperature for flying: What is the coldest temperature they can fly in?\n");
scanf("%d", lowest_temp);
printf("What is the hottest temperature they can fly in?\n");
scanf("%d", highest_temp);
printf("Please enter the name of the weather data file for Dragon Island.\n");
scanf("%s", fname);
ifp = fopen(fname, "r");
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
return 0;
}
Crashes after first scanf no errors
If you can tell me what the error is I'd appreciate it. Also if you can help me with the next steps in my problem that would be awesome as well.
My assignment
https://gist.github.com/anonymous/e9292f14b2f8f71501ea/88e9e980632871f1f1dedf7589bb518f1bba43e8

scanf("%d", lowest_temp);
...
scanf("%d", highest_temp);
%d require address of int argument you pass it just int variable . Pass their address -
scanf("%d",&lowest_temp);
...
scanf("%d",&highest_temp);
And these both statements-
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
should be-
fscanf(ifp, "%d %d %d %f", &month &day &year &temp);
printf("%d %d %d %f", month ,day,year ,temp);

Related

C programming TEXT FILE not reading whole line

Here is the input from TEXT FILE :
19 BISON-BURGER 10 15.000000
10 BRAISED-COD 5 17.000000
23 MOJITO 8 11.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
The output came out from compiler after input being key in was :
10 BRAISED-COD 5 17.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
Why the compiler skipping some of the lines ? Is there any changes I need to make?
Please help. Thank you so much.
Here is the full code :
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
int main()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
do
{
fp = fopen("addproduct.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
fp = fopen("addproduct.txt","r");
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
You're reading two lines from the file on each iteration of the output loop. Get rid of the extra scanf:
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
You're calling fscanf() twice each time through the while loop. First in the while condition, then in the body of the loop. You're only printing the variables read by the second one, so the lines read by the first fscanf() are being ignored. Get rid of the second fscanf().
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}

C programming Displaying txt FILE in compiler

The code works like a charm at the moment. But, the last 2 lines of output are identical as you can see here.
What is the problem here?
The datas came from a txt file that was build earlier.
1 CADBURY 999 1.900000
2 PEPSI 999 2.500000
3 IPHONE 976 2500.000000
4 SPIRULINA 100 50.000000
2 PAIPSI 100 0.900000
10 BLACKMORE 98 30.000000
17 TROPICANA 13 1.500000
17 TROPICANA 13 1.500000
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
int addProduct();
struct product {
int quantity, reorder, i, id;
char name[20];
float price;
};
int main() {
FILE *fp;
int i = 0;
struct product a;
system("cls");
char checker;
int counter;
do {
fp = fopen("addproduct.txt", "a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
} while(checker == 'Y');
if (checker == 'N') {
fp = fopen("addproduct.txt", "r");
while (!feof(fp)) {
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
while (!feof(fp)) does not work as expected: feof(fp) only becomes true after input has failed. During the last iteration, fscanf() fails, but you do not check its return value and the values from the previous iterations are used by the last printf().
You should instead write:
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
When you are printing out the contents of the file at the end of your program, the fscanf function does not set feof until it attempts to read past the end of the file.
This means that after you read the last line in the file, the feof is still not true.
So the loop continues and fscanf then attempts to read another line but fails. So the variables a.id, a.name etc are the same as what they were after the previous loop execution of fscanf.
You should check that fscanf has returned the expected number of fields before you print out the results.
For example,
...
if (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
...
would fix the issue.
edit: Sorry 4 parameters not 5, fixed

C, Reading double values from text file

Hi just getting some weird outputs from trying to read the inputs as double values in C. This issue does not occur when the inputs are integers is there anyway to make it work with double?
#include "stdafx.h"
int main(void)
{
double a, b, c, d, i;
FILE *inp;
inp = fopen("C:\\Users\\student\\Documents\\Visual Studio2012\\Projects\\ConsoleApplication3\\test.txt", "r");
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
while (i != EOF)
{
printf("a = %f & %d \n", a, i);
printf("b = %f & %d \n", b, i);
printf("c = %f & %d \n", c, i);
printf("d = %f & %d \n", d, i);
printf("%d \n", EOF);
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
}
fclose(inp);
return 0;
}
Figured it out, the %f in fscanf should be %lf
Change the specifier to %lf in fsacnf and printf statements. Like this -
fscanf(inp,"%lf %lf %lf %lf", &a, &b, &c, &d);
Other problems -
1.Also i is declared as double but in printf you print it with specifier %d ,so you pass wrong argument -
printf("a = %f & %d \n", a, i); // similar in all printf's
So according to me declare i as int and then print it.
2.Also you should always check return of fopen so check it.

Can somebody tell me why I have this runtime error?

I have created a program on Pelles C, however, when I run it, it is skipping straight to the end of the function simply saying "press any key to continue"
#include <stdio.h>
int main()
{
char letter;
int num1, num2;
printf("Enter any one keyboard character ");
scanf("%c", &letter);
printf("Enter 2 integers seperated by a space ");
scanf("%d %d", &num1, &num2);
printf("Numbers inputted were %d and %d \n" num1, num2);
printf("letter input %c", letter);
printf(" Stored at: %p \n", &letter);
return 0;
}
Can anybody tell me why this is happening ?
printf("Numbers inputted were %d and %d \n" num1, num2);
^
You missed a , before num1 in above printf statement.
printf("Numbers inputted were %d and %d \n",num1, num2);

fscanf reading everything except for first line

int k;
float regionID;
int t;
char string[100];
float avgTemp,totalTemp;
for(k = 0; k < MAX_STATIONS; k++){
if (fgets(string, sizeof(string), fp) == 0){
break;
}
fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID, &stationInfo[k].temperature, &stationInfo[k].year, &stationInfo[k].month, &stationInfo[k].day, &stationInfo[k].hour, &stationInfo[k].minute, &stationInfo[k].location.latitude, &stationInfo[k].location.longitude, &regionID);
printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID, stationInfo[k].temperature, stationInfo[k].year, stationInfo[k].month, stationInfo[k].day, stationInfo[k].hour, stationInfo[k].minute, stationInfo[k].location.latitude, stationInfo[k].location.longitude);
}
So, my program reads my file almost perfectly, but it will not read the first line of my file. Anyone know what might be causing this problem and how I might be able to fix it?
You are quite intentionally reading and discarding a line here:
if (fgets(string, sizeof(string), fp) == 0) {
I think you meant to use sscanf instead of fscanf, so that you use the data returned to you by fgets.
sscanf(string,"%d %f %d %d %d %d %d %f %f %f", ... );
The reason why your program is "not reading the first line of the file" is because it actually already read the first line when you called
if (fgets(string, sizeof(string), fp) == 0)
The change to fix this is simple:
for(k = 0; k < MAX_STATIONS; k++)
{
if (fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID,
&stationInfo[k].temperature,
&stationInfo[k].year,
&stationInfo[k].month,
&stationInfo[k].day,
&stationInfo[k].hour,
&stationInfo[k].minute,
&stationInfo[k].location.latitude,
&stationInfo[k].location.longitude,
&regionID) > 10)
{
break;
}
printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID,
stationInfo[k].temperature,
stationInfo[k].year,
stationInfo[k].month,
stationInfo[k].day,
stationInfo[k].hour,
stationInfo[k].minute,
stationInfo[k].location.latitude,
stationInfo[k].location.longitude);
}
Note the fscanf's return value:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
Hope this is what you are looking for!

Resources