fscanf reading everything except for first line - c

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!

Related

Error message in C invalid binary

I'm getting an error:
Type invalid operands to binary & (have 'int *' and 'int')
here is my program. The problem occurs at line 34 or the fscanf num1
#include <stdio.h>
#include <stdlib.h>
FILE *infile;
FILE *prnt;
main()
{
int num1, num2, nums;
char complex;
float fcost;
char name [11];
infile = fopen ("F:/DATA.txt", "r");
prnt = fopen ("F:/income.txt", "w");
if (infile == 0)
{
printf ("FILE NOT ON DISK\n");
system("pause");
return 0;
}
fprintf (prnt, "%-15s %-23s %6s\n\n", "ABAHLMAN", "Program 1", "PAGE 1");
fprintf (prnt, "\n");
fscanf (infile, " %i %i %i %f %c", &nums &num1 &num2 &fcost &name);
while (!feof (infile))
{
int area = (nums * 200) + (num1 * 300) + (num2 * 450);
float cost = fcost + (area * 75.00);
double income = 12 * ((nums *450) + (num1 * 550) + (num2 *700));
float payback = cost/ income;
fprintf (prnt, "%-10s %5f %7c %9.2f\n", name, payback, area, cost);
fscanf (infile, " %d %d %d %f %c", &nums &num1 &num2 &fcost &name);
}
fclose (infile);
fclose (prnt);
return 0;
}
You are not separating the arguments with comma in fscanf() statements. They should be:
fscanf (infile, " %i %i %i %f %s", &nums, &num1, &num2, &fcost, name);
and
fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name);
Note that name is an array and it gets converted into a pointer when you pass it to fscanf(). So, the & operator needs to be dropped. As noted in the comments, the format should be %c for name.
Also, see: What is array decaying?
I'd also suggest to use a standard definition for main() function. main() {..} is outdated and should be avoided.
Instead, you can write it as int main(int).
I see a few problems. First, no commas to separate the arguments for scanf.
Next, the last argument for fscanf should have %s format and be passed name without &.
Next, feof is not the way to control a loop, you should check the return value from fscanf anyway.
You also use the wrong format specifier %c for area in printf which should be %d
Finally be consisent with the use of %d or %i format specifier. Unless you want the user to input in other number bases than decimal, stick to %d.
So I suggest the loop should be
while (fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name) == 5)
{
int area = nums * 200 + num1 * 300 + num2 * 450;
float cost = fcost + area * 75.00;
double income = 12 * (nums * 450 + num1 * 550 + num2 * 700);
float payback = cost / income;
fprintf (prnt, "%-10s %5f %7d %9.2f\n", name, payback, area, cost);
}

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

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.

C: How can I store an unknown number of inputs using scanf so that each word of the input is stored in a different variable?

So far i have:
char r[4][10];
printf("Enter an option: \n");
scanf("%s %s %s %s %s", r[0],r[1],r[2], r[3], r[4]);
printf("%s %s %s %s %s\n", r[0], r[1], r[2], r[3], r[4]);
This works as long as 5 inputs are given. Right now if you type less then 5 inputs and hit enter the program continues to ask for input until it has all 5 values but I want the scanf to stop and the program to continue if less then 5 inputs are entered.
You'll do best to read a line with fgets() and parse it with sscanf(). If there will be no more than 5 words on a line:
char r[5][10];
char line[4096];
printf("Enter an option: \n");
if (fgets(line, sizeof(line), stdin) != 0)
{
int num = sscanf(line, "%9s %9s %9s %9s %9s", r[0],r[1],r[2], r[3], r[4]);
for (int i = num; i < 5; i++)
r[i][0] = '\0';
printf("%s %s %s %s %s\n", r[0], r[1], r[2], r[3], r[4]);
}
Note that you only had r[4][10] but were using subscript 4 — bad move.
A better way to handle it would be a loop that scans over the line, reading a word at a time until there is no more space in the array or no more words in the line. It's a little trickier, though.

read from .txt C

I'm having some trouble with C language..
I have one file txt with various lines in the form:
F 65 S 4 1 139.56 3704.26
and my program:
p = fopen("dados.txt", "r");
if ( p == NULL) {
printf("\n\nNao foi possivel abrir o arquivo.\n");
exit(1);
}else{
while ( !feof(p) ){
fscanf(p,"%c %d %c %d %d %f %f",
&sexo,&idade,&estadoCivil,&numFilhos,&freq,&mediaGasto,&mediaSalarial);
printf("%c %d %c %d %d %f %f\n",
sexo,idade,estadoCivil,numFilhos,freq,mediaGasto,mediaSalarial);
}
the return is:
looks bad...
if i change in fscanf: %c to %f
the return is:
looks great, but the variable idade is always 0... :S
wtf i have to do?
You have to add the newline to your scanf call:
fscanf(p,"%c %d %c %d %d %f %f\n",
&sexo,&idade,&estadoCivil,&numFilhos,&freq,&mediaGasto,&mediaSalarial);
Without the newline in scanf, the first line will be correct, but the following line assigns the newline from the input to sexo.

Resources