C, Reading double values from text file - c

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.

Related

Crash after first scanf novice c programming

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);

Reading some data from string C

I am reading some .txt files that have data on it. My "strategy" for doing this is just reading the file line by line. I have no problems doing this task, however, at some point I have a string with different data (separated by spaces). I just want to read some data, because I do not need all the data. I used sscanf from string.h for doing this, this is an example of what I have:
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "1 189.37823 62.18428 2.486 25.33 -21.73 -21.68 -22.01 10.12 10.13 10.11 10.08 9.95 9.89 9.91 7 8.7 0 -42.85";
int id, xid;
double z, r, d, sfr, tmp;
sscanf(str, "%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
&id, &z, &r, &d, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp,
&tmp, &sfr, &xid, &tmp);
printf("id = %d, z = %lf, r = %lf, d = %lf, sfr = %lf, xid = %d\n", id, z, r, d, sfr, xid);
}
However, my solution is quite inelegant, I am just "reading" all the data, and for the data I don't need I use a temporal variable. Is there a more correct (and perhaps more efficient) way for doing this?
Use %*f to read real value and drop it.
sscanf(str, "%d %lf %lf %lf %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %lf %lf %*f",
&id, &z, &r, &d, &sfr, &xid);
If we may, just ignore the last one, because it's not necessary,
then we have:
sscanf( str, "%d %lf %lf %lf, &id, &z, &r, &d);
for(int i = 0; i < 12; i++) // easy to control skip how many number
sscanf( str, "%lf, &tmp);
sscanf( str, "%lf %lf", &sfr, &xid);

How do I display a message or respond towards an invalid input in C

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, result;
float b;
printf("**This is a simple arithmetic calculator.** \n");
printf("\n Please enter an integer: ");
scanf("%i ", a);
printf("Please enter a floating point number: ");
scanf("%f", b);
result = a + b
printf("Output: ");
printf("%i + %f = %lf \n", a, b, result);
printf("%i - %f = %lf \n", a, b, result);
printf("%i * %f = %lf \n", a, b, result);
}
I need to ensure that your program will not crash if the user enters an invalid input.
scanf is a function that has also got a return value which indicates how many inputs were inserted correctly.
So you can just do something like:
while (scanf("%i ", a) != 1)
{
printf("wrong input, try again");
}
use while loop or if for scanf
while(scanf("%i ", a) !=1){
printf("invalid input.\n");
}
and
while(scanf("%f", b) !=1){
printf("invalid input.\n");
}

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!

sscanf reading a string within a string

I have a small problem that I can't seem to fix. Say I have a string,
buffer = "1 1 X ./simple E"
And I want to extract the 2 ints, 2 chars and the filename,
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
I don't get back quite what I expect. I get "11 1 X (null)". Any help appreciated.
You are declaring char *d, which will fail because it has no valid place to point. Use an array that has enough space will do:
#include <stdio.h>
#include <string.h>
int main()
{
int a, b;
char c, e;
char d[20];
char buffer[] = "1 1 X ./simple E";
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
}
OUtput: 1 1 X ./simple E
c and e can be int's or chars. Note the overflow issues with d[100].
int a, b, c, e;
char d[100];
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
#include <cstdio>
#include <cstdlib>
int main() {
char buffer[] = "1 1 X ./simple E", c, d[10], e;
int a, b;
//sscanf(buffer, "%d %d %c %*[./]%s %c", &a, &b, &c, d, &e); //To ignore "./"
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e); //Don't ignore "./"
printf("%d %d %c %s %c\n", a, b, c, d, e);
return 0;
}
Output:
1 1 X ./simple E
No blank space delimiter need in the sscanf function argument.
sscanf(buffer, "%d%d%c%s%c", &a, &b, &c, d, &e);
%d is blank separated when when reading buffer, and there should not be a space between %c and %s, cause it swallow the space, leaving the buffer with no separator between char and string.

Resources