I'm trying to read the numbers after a colon and store the value in a variable, but when I print it, it prints a random 6 digit number. I only need to store the value, not the 'ms' or 'degrees'.
For example, the text file is similar to this, but repeats for 100 set of values:
time: 20 ms
temperature: 50.5 degrees
lightvalue: 30
value1: 0.59
value2: 1
value3: 0
----------------------
time: 40 ms
temperature: 37 degrees
lightvalue: 10
value1: 1.57
value2: 0
value3: 1
----------------------
I want to store each number in a separate variable.
Here is part of my code:
int time[10];
double temperature[10];
int lightvalue[10];
double value1[10];
int value2[10];
int value3[10];
for (int i = 0; i < 100; i++) {
fscanf(infile, "time: %i", &time);
fscanf(infile, "temperature: %d", &temperature);
fscanf(infile, "lightvalue: %i", &light);
fscanf(infile, "value1: %d", &val1);
fscanf(infile, "value2: %i", &val2);
fscanf(infile, "value3: %i", &val3);
//how to skip the "---------------" line?
}
Your code contains several errors. You have used different variable names in iteration. You used wrong specifiers.
for (int i = 0; i < 2; i++) {
fscanf(infile, "time: %d %*s\n", &time[i]);
fscanf(infile, "temperature: %lf %*s\n", &temperature[i]);
fscanf(infile, "lightvalue: %i\n", &light[i]);
fscanf(infile, "value1: %lf\n", &val1[i]);
fscanf(infile, "value2: %i\n", &val2[i]);
fscanf(infile, "value3: %i\n", &val3[i]);
fscanf(infile, "%*s\n"); // to skip the "---------------" line
}
To skip rest of the line, you may use %*s which will match the unused string.
Related
The 2 arrays wont store input, the increment of each index number is always at 0 as printf states it.
int main()
{
int ingredientsAmount;
double ingredientsPrice[10];
double ingredientsWeight[10];
scanf("%d", &ingredientsAmount);
for(int i = 0; i < ingredientsAmount; i++)
{
scanf("%lf", &ingredientsPrice[i]);
printf("Price stored at index %lf\n", i);
scanf("%lf", &ingredientsWeight[i]);
printf("Weight stored at index %lf\n", i);
}
return 0;
}
You are using the incorrect conversion specifier %lf with an object of the type int. Use the conversion specifier %d. Write
printf("Price stored at index %d\n", i);
and
printf("Weight stored at index %d\n", i);
Or maybe you mean the following
printf("Price stored at index %d is %f\n", i, ingredientsPrice[i]);
and
printf("Weight stored at index %d is %f\n", i, ingredientsWeight[i]);
Pay attention to that the for loop is unsafe because the value of the variable ingredientsAmount entered by the user can be greater than the sizes of the declared arrays.
At least you should write
if ( scanf("%d", &ingredientsAmount) != 1 || ingredientsAmount > 10 )
{
ingredientsAmount = 10;
}
I'm running user input through a series of functions provided by the ctype.h library, but scanf doesn't work for white space.
Since I can't use scanf for whitespace I believe fgets() should be what I'm looking for, but am unsure about the last parameter I would use for it. Any advice would be appreciated!
#include <stdio.h>
#include <ctype.h>
int main(void) {
char a;
puts("Enter a character:");
//scanf("%s", &a);
fgets(a, 1, ??);
printf("isblank('%c') = %d \n", a ,isblank(a));
printf("isdigit('%c') = %d \n", a ,isdigit(a));
printf("isalpha('%c') = %d \n", a ,isalpha(a));
printf("isalnum('%c') = %d \n", a ,isalnum(a));
printf("isxdigit('%c') = %d \n", a ,isxdigit(a));
printf("islower('%c') = %d \n", a ,islower(a));
printf("isupper('%c') = %d \n", a ,isupper(a));
printf("tolower('%c') = %d \n", a ,tolower(a));
printf("toupper('%c') = %d \n", a ,toupper(a));
printf("isspace('%c') = %d \n", a ,isspace(a));
printf("iscntrl('%c') = %d \n", a ,iscntrl(a));
printf("ispunct('%c') = %d \n", a ,ispunct(a));
printf("isprint('%c') = %d \n", a ,isprint(a));
printf("isgraph('%c') = %d \n", a ,isgraph(a));
return 0;
}
Output should look like this
Enter a character:
C
isblank('C') = 0
isdigit('C') = 0
isalpha('C') = 1024
isalnum('C') = 8
isxdigit('C') = 4096
islower('C') = 0
isupper('C') = 256
tolower('C') = 99
toupper('C') = 67
isspace('C') = 0
iscntrl('C') = 0
ispunct('C') = 0
isprint('C') = 16384
isgraph('C') = 32768
Program should intake a single character and convert it to integer through a series of functions.
The first argument to fgets() needs to be a buffer that can hold the entire line of input.
int main(void) {
char a;
char line[100];
puts("Enter a character:");
fgets(line, sizeof line, stdin);
a = line[0];
I am writing a little program for reading a file formatted like that :
2 2
1.0 2.0
5.0 5.1
6.5 3.1
5.1 2.3
3 1
4 1 2 3 5 2
1 4 5 2 6 5
1 4 5 2 3 6
I am using fscanf to read the first two integers and allocate an array to store all four float that follows. It works fine. But when the "cursor" arrives to the line that contains integers "3 1", it stops working for any reason...
float *c = NULL;
float **coord = NULL;
f = fopen("mesh.dat", "r");
if( f != NULL ){
/* the first two integers */
fscanf(f, "%d %d", &n1, &n2);
n = n1*n2;
c = malloc(2*n*sizeof(float));
coord = malloc(2*sizeof(float *));
for(i=0; i<2; i++){ coord[i] = &c[i*n1]; }
/* reading all coordinates */
for(i=0; i<n; i++){ fscanf(f, "%f %f", &coord[0][i], &coord[1][i]); }
/* reading the two integers */
fscanf(f, "%d %d", &n, &t);
printf("n = %d, t = %d\n", n, t);
}
fclose(f);
The program stops here. Because it doesn't read the integers "3 1".
Any idea ?? I'm tearing out my hair trying to understand...
This line:
fscanf(f, "%d %d", &n1, n2);
Should be
fscanf(f, "%d %d", &n1, &n2);
I am trying to do what's been done here Read co-ordinates from a txt files using C Program . The data that I am trying to input is in this format:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
The only difference in my point structure is that I have a an extra char to store the letter in the first column (which may or may not be the letter f). I guess im either declaring my char wrong, or I'm calling it in printf incorrectly. Either way, I only get the first line read and then my program terminates. Any ideas ?
Here is my MWE below
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 4000
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char t[1];
}Point;
int main(void) {
Point *points = malloc( MAX * sizeof (Point) ) ;
FILE *fp ;
fp = fopen( FILEPATHtri,"r");
int i = 0;
while(fscanf(fp, "%s %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<=i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[i].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
return 0;
}
Since there's only 1 char in there, not a string just use a single char in your code:
char t;
}Point;
Then when you read it in:
while(fscanf(fp, "%c %f %f %f ", &points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
I'll note that having an array of 1 char, at the end of a structure, sets you up for the struct hack which might not have been your intentions... A good reason to use just char t instead of char t[1]
Also this line:
for (n=0; n<=i; n++){
Should be
for (n=0; n<i; n++){
One last note... if you wanted to print the character out that you read in the prints at the bottom, you should be using n:
// note your previous code was points[i].t
printf("%c %f %f %f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
Check this
while(fscanf(fp, "%c %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
getch();
return 0;
}
modification are since only a single character is read %s modified to %c also in printf its not points[i].t its points[n].t . Also the limit checking in for loop is also corrected to n<i
I have a file that is organized line by line and the structure of all lines was defined by me but the contents of each line can increase or decrease according to two variables that the same line contains.
Line Example: 1 Andre 2 0 5 13 05 2011 4 13 05 2011
'1' represents the user ID
'Andre' represents the name associated to the ID
'2 0' subdivides: '2' represents how many movies the user as taken home AND '0' represents how many movies reservations the user has.
So far the line is following the pattern %d %s %d %d and they can be stored in the variables I want.
Now the hard part comes. Depending on the '2 0' part, the program knows that has to read the pattern %d %d %d %d ('5 13 05 2011' and '4 13 05 2011' - represent dates of movies taken home) 2 times and also the program knows that after reading 2 times the previous pattern it knows that i doesn't need to read anything else due to the zero part in '2 0'.
Example of line with more data: 1 Andre 2 1 5 13 05 2011 4 13 05 2011 7 14 05 2011
'2 1'
The '2' part tells the program that in that line it should read '5 13 05 2011' and '4 13 05 2011' to variable_one[i][4] (example)
The '1' part tells the program that in that line it should read '7 14 05 2011' to variable_two[i][4] (example)
I'm using fscanf(file, "%d %s %d %d", &id[i],&nomes[i],&livros_num[i][0],&livros_num[i][1]); to read '1 Andre 2 0' but how can i read the rest of the line according to '2 0'?
You can use fscanf.
Here's an example:
int int1;
char char_buf[BUF_SIZE];
fscanf ( file_stream, "%d%s", &int1, char_buf);
After your first call of fscanf, you need to loop depending on the variables you read. You seem to have already realized that the int's you read in your first call determine the number of extra reads you need in the line.
int i;
for ( i=0; i< livros_num[i][0]; i++ ) {
int something, day, month, year; // 'something' is unclear to me in your code
fscanf( file, "%d %d %d %d", &something, &day, &month, &year );
// store these variables wherever
}
// same procedure for second number
for ( i=0; i< livros_num[i][1]; i++ ) {
int something, day, month, year; // 'something' is unclear to me in your code
fscanf( file, "%d %d %d %d", &something, &day, &month, &year );
// store these variables wherever
}
fscanf( file, "%s\n" ); // I'm NOT sure if this is nessecary, to move the file to the next line
fgets(buf,sizeof(buf),f)
p = strtok (buf," ");
p1 = strtok (0," ");
p2 = strtok (0," ");
p3 = strtok (0," ");
n1 = atoi(p1);
n2= atoi(p2);
i=0;
while(n1--)
{
sscanf(p3,"%d %d %d %d", &v1[i],&v2[i],&v3[i],&v4[i] );
strok(0," ");
strok(0," ");
strok(0," ");
p3 = strok(0," ");
i++;
}
i=0;
while(n2--)
{
sscanf(p3,"%d %d %d %d", &v2[i],&v2[i],&v2[i],&v2[i] );
strok(0," ");
strok(0," ");
strok(0," ");
p3 = strok(0," ");
i++;
}
Well, when you read the '2 0' you get two pieces of information: the number times you need to loop for the dates of the movies taken home and the number of times you need to loop to read all the reservations.
int i;
int num_movies_taken, num_movies_reserved;
// read all the dates of the movies taken home
num_movies_taken = livros_num[i][0];
for (i = 0; i < num_movies_taken; i++)
{
fscanf("%d %d %d %d", ...);
}
// read all the reservations
num_movies_reserved = livros_num[i][1];
for (i = 0; i < num_movies_reserved; i++)
{
fscanf("%d %d %d %d", ...);
}
How about
int j;
fscanf(file, "%d %s %d %d", &id[i],&nomes[i],&livros_num[i][0],&livros_num[i][1]);
for (j=0;j<livros_num[i][0];++j)
{
fscanf(file,"%d %d %d %d",&variable_one[0],&variable_one[1],&variable_one[2],&variable_one[3]);
}
etc.