The includes,
#include <time.h> /* para calcular data e duracao */
#include <sys/time.h> /* para duracao */
#include <stdio.h>
#include <string.h>/* para limpar a tela durante os menus */
#define FILENAME "RelatoriosClimaticos.bin"
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
I have a struct defined like this:
typedef struct RelatorioClimatico
{
char nomeLocal [50];
struct tm dataColeta;
int temperatura;
}RelatorioClimatico;
And i have this function that tries to write random values to a binary file:
void createRandomFilesByFileSize(FILE *fpp, long int requestedSize) //Opção 2
{
struct timeval tm_ini, tm_fim;
gettimeofday(&tm_ini, NULL);
long int x=0, registriesNeededForSize, registryBlock;
int y=0;
struct tm dataColeta;
int size = sizeof(RelatorioClimatico);
RelatorioClimatico *relatorioClimatico = malloc(sizeof(RelatorioClimatico) * 27849);
registriesNeededForSize = (int)(requestedSize/184);
registryBlock = (int)(requestedSize/184)/27849;
rewind(fpp);
while(x<registryBlock)
{
while(y<27849)
{
NameGen(relatorioClimatico[y].nomeLocal);
fflush(stdin);
relatorioClimatico[y].dataColeta = GeraData();
fflush(stdin);
relatorioClimatico[y].temperatura = rand() % 45;
fflush(stdin);
}
x++;
y=0;
fseek(fpp, size, SEEK_END);
fwrite(&relatorioClimatico, size*27849, 1, fpp);
x++;
}
rewind(fpp);
gettimeofday(&tm_fim, NULL);
double diff_sec = difftime(tm_fim.tv_sec, tm_ini.tv_sec);
double diff_milli = difftime(tm_fim.tv_usec, tm_ini.tv_usec)/1000000;
printf("Os %i relatorios foram gerados em %f segundos, e o arquivo resultando possui %ld bytes \n",registriesNeededForSize, diff_sec+diff_milli, ftell(fpp));
}
So what i am not being able to do is to create this typedefined RelatorioClimatico variable:
RelatorioClimatico *relatorioClimatico = malloc(sizeof(RelatorioClimatico) * 27849);
Where RelatorioClimatico is the struct, and i want it to be an array of exactly 27849 positions.
I tried this before but it gave me an error:
RelatorioClimatico relatorioClimatico[27849];
Any help would be greatly appreciated, thanks in advance
Related
I'm trying to write a car register with different function, in different files, lib.c, lib.h and main.c. However when I try to compile the program it shows this error with my car "unknown type name ‘Car’
34 | void addCar(Car* car)":
In my 'lib.c' file, this is my progress so far:
#include "lib.h"
void printMeny() {
//meny
printf("Meny\n");
printf("1. Lägg till ett fordon\n");
printf("2. Ta bort ett fordon\n");
printf("3. Sortera efter bilmärka\n");
printf("4. Information om ett fordon\n");
printf("5. Skriv ut hela registret\n");
printf("0. Avsluta programmet\n");
}
int getNumber() {
char buffer[10] = {0};
int number = 0;
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer, "%d", &number);
return number;
}
void getString(char* buf, size_t len) {
if(fgets(buf,len,stdin)) {
char* p;
if((p = strchr(buf, '\n')) != NULL)
{
*p = '\0';
}
}
}
void addCar(Car* car) {
printf("Namn:\n");
getString(car->owner.name, sizeof(car->owner.name));
printf("Ålder:\n");
car->owner.age = getNumber();
printf("Märke:\n");
getString(car->car_brand, sizeof(car->car_brand));
printf("Modell:\n");
getString(car->car_model, sizeof(car->car_model));
printf("Registreringsnummer:\n");
getString(car->plate_num, sizeof(car->plate_num));
}
Then in my lib.h:
//#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAR_MAX 10
typedef struct {
char name[40];
int age;
} Person;
typedef struct {
Person owner;
char car_brand[20];
char car_model[20];
char plate_num[20];
} Car;
void printMeny();
int getNumber();
void addCar(Car* car);
void getString(char* buffer, size_t len);
And finally in main.c;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib.h"
int main() {
int val = 0;
Car cars[CAR_MAX] = {0};
int numberOfCars = 0;
printMeny();
val = getNumber();
switch(val) {
case 0:
break;
case 1:
addCar(&cars[numberOfCars]);
numberOfCars++;
I am a beginner learning C.
I am trying to write two functions, one to allocate a string and the other to insert string1 in string2 from position i.
my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
//alocation
char * allouerChaine(int n)
{
char * s =(char*)malloc(n*sizeof(char *));
if(s!=NULL)
return s;
exit(-1);
}
char * strinsert(char *M, char* T ,int i)
{
char * temp=allouerChaine(strlen(M)+strlen(T));
strncpy(temp,M,i);
temp[i]='\0';
strcat(temp,T);
strcat(temp,M+i);
temp[strlen(M)+strlen(T)]='\0';
return temp;
}
int main(){
char *M;
char *T;
char*p;
int i;
printf("faites entre la chaine M: ");
scanf("%s",&M);
printf("\nfaites entre la chaine T: ");
scanf("%s",&T);
printf("faites entrer I: ");
scanf("%d",&i);
p=strinsert(M, T ,i);
printf("%s",p);
return 0;
}
when I try with static strings like :
p=strinsert("hello", "ss" ,2);
printf("%s",p);
the code works, which means I don't have a problem with my functions but I do have a problem inside the main.
The data in the txt-file are just two columns with numbers, no labels (x-y coordinates).
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct dataset{
float x;
float y;
} dataset;
int main(){
dataset* coordinates;
FILE* input;
input = fopen("data.txt", "r");
int i = 0;
while (fscanf(input, "%e %e", &coordinates[i].x, &coordinates[i].y) == 2)
i++;
fclose(input);
return 0;
}
Thank you for helping me out.
I'm learning "FIFO" in C this is my first code trying to pass an struct by argument to FIFO function but it's not working as expected... And I can't figure why. Someone could please give me hand an explain what am I doing wrong?
I wrote my code in portuguese if it's hard to you guys understand let me know I'll translate to english.
#define TAMANHO 3
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
typedef struct
{
char Nome[20];
} pretendentes;
pretendentes // nome da estrutura
pessoas[10]; // vetor de estrutura
int main(void)
{
setlocale(LC_ALL, "");
cadastroPretendentes(pessoas);
qstore(pessoas);
}
void qstore(pretendentes *Pessoas)
{
int pfinal = 0;
int pinicial = 0;
if(pinicial == TAMANHO)
{
printf("A fila está cheia.");
return;
}
pessoas[pinicial] = Pessoas.Nome;
pinicial++;
}
void cadastroPretendentes(pretendentes *Pessoas)
{
int i;
for(i = 0; i < TAMANHO; i++)
{
printf("Insira o nome do pretendente %d: ", i+1);
scanf("%s", (*(Pessoas + i)).nome);
}
}
assume that pessoas is fifo memory.
In cadastroPretendentes(), you are getting names and store into fifo memory and in qstore(), you are writing pessoas[0] with pessoas[0].
if qstore is called another 2 times, 1st and 2nd elements in array will be replaced with pessoas[0].
In this code, qstore is not required as cadastroPretendentes() gets name and stores in pessoas (fifo memory)
I just want to get my vendor ID, i.e. GenuineIntel using cpuid in C.
This is the function I want to use:
void __cpuid(
int cpuInfo[4],
int function_id
);
This is my wrong code:
int main(){
int cpuInfo[4];
__cpuid(cpuInfo, 1);
}
Assuming you are running on Windows, you need to add #include <intrin.h> to your code. See here.
#include <string.h>
#include <locale.h>
#include <intrin.h>
#include <stdio.h>
// Prototipos
int LeeIDFabricante (char * CadFabricante);
//void LeeIDModelo (char * CadenaModelo);
int main(int argc, char *argv[])
{
char CadFabricante[0x20];
char CadenaModelo[0x40];
int Resultado;
setlocale( LC_ALL, "Spanish" );
Resultado = LeeIDFabricante(CadFabricante);
CadFabricante[12]='\0';
printf("\nLa identificacion del fabricante es: %s. El maximo valor de CPUID es %d.\n", CadFabricante, Resultado);
//LeeIDModelo(CadenaModelo);
//printf("\nLa cadena de modelo es: %s\n", CadenaModelo);
printf("\nPulse tecla RETORNO para terminar\n");
getchar();
return 0;
}
int LeeIDFabricante (char *CadFabricante)
{
int p[4] = {-1};
__cpuid(p, 0);
memset(CadFabricante, 0, sizeof(CadFabricante));
*((int*)CadFabricante) = p[1];
*((int*)(CadFabricante+4)) = p[3];
*((int*)(CadFabricante+8)) = p[2];
return p[0];
}