Get vendor ID in C using cpuid - c

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];
}

Related

How can I solve my error problem when compiling the program?

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

How to read pointer to string in my main function, error "expected argument of char* in scanf"

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.

File writing and struct arrays in C

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

Reading a JPEG file with a buffer : segfault

Basically I'm trying to code a reader for JPEG files (using libjpeg) but I have a segfault, and I wanted to know if someone could see where is my error ? I'm sure it comes from the malloc but I don't know. I tried to put some debug by putting some printf but absolutely NONE of them appear, wtf ?
Thanks for your help mates...
main.c :
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include "fonctions.h"
int main (int argc, char** argv){
int *H;
int *W;
int *C;
printf("COUCOUCOUCOUCOU");
FILE *fichier = NULL;
char car;
fichier = fopen("cara.jpg", "r");
if (fichier == NULL)
printf("Probleme lecture");
lire(fichier, H, W, C);
fclose(fichier);
return 0;
}
lire.c :
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <jpeglib.h>
#include <jerror.h>
unsigned char** lire (FILE* file, int *H, int *W, int *C){
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int n = 0;
unsigned char** buffer;
printf("SHITSHITSHITSHIT\n");
fflush(stdout);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo); // Initialisation de la structure
jpeg_stdio_src(&cinfo,file); // file est de type FILE * (descripteur de fichier
// sur le fichier jpega decompresser)
jpeg_read_header(&cinfo,TRUE);// lecture des infos sur l'image jpeg
jpeg_start_decompress(&cinfo);// lancement du processus de decompression
*H = cinfo.output_height;
*W = cinfo.output_width;
*C = cinfo.output_components;
buffer=(unsigned char **)malloc( (*H) *sizeof(unsigned char*) );
while (n < *H)
{
buffer[n] = (unsigned char*) malloc( (*W) * (*C) *sizeof(unsigned char *) );
printf("DEBUG\n");
fflush(stdout);
jpeg_read_scanlines(&cinfo,buffer+n,1); // lecture des n lignes suivantes de l'image
// dans le buffer (de type unsigned char *)
n++;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return buffer;
}
i compile with :
gcc -c -I/usr/local/include *.c
gcc *.o -o programme -ljpeg
When you call lire, you pass H, W and C to the function. These are pointers and they are not initialized and therefore you get a crash here in lire.
*H = cinfo.output_height;
You need to call the lire function like this:
int H;
int W;
int C;
....
lire(fichier, &H, &W, &C);

Find if string exist in a vector of structs

Good afternoon,
I'm here with a doubt that i am not able to overcome and i've tried a number of ways. I'm doing a program that among other things the user has to enter data about a product where the serial number has to be unique and of type char. What is happening is that when i type the 1st product serial number the program returns that is already a product with this serial number despite being the 1st.
Here's the code I have:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char numeroserie;
char descricao;
float preco;
} stProduto;
/********************** Funcoes dos produtos *************************/
void inserirProduto(stProduto produtos[], int *contadorProdutos);
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar);
int main()
{
stProduto produtos[500];
int contadorProdutos=0;
inserirProduto(produtos,&contadorProdutos);
return 0;
}
void inserirProduto(stProduto produtos[], int *contadorProdutos)
{
char string1;
char posicao;
do
{
printf("Introduza o numero de serie do produto:");
scanf("%s",&string1);
posicao = procurarNumeroserie(produtos,*contadorProdutos, string1);
if (posicao == 0)
{
printf("Ja existe um produto com esse numero de serie!!!\n");
}
}
while(posicao == 0);
strcpy(produtos[*contadorProdutos].numeroserie,string1);
(*contadorProdutos)++;
}
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar)
{
int i;
char posicao;
posicao = 0;
for(i = 0 ; i < contadorProdutos ; i++)
{
if (strcmp(produtos[i].numeroserie, produtoAProcurar) == 0)
{
posicao=i;
i=contadorProdutos;
}
}
return posicao;
}
Someone help please !!I am already getting frustrated.
Thanks
You got it wrong, char is not a string type in c, it's an integer type.
A string is defined as a sequence of bytes non-nul that are followed by a nul byte.
They type char is 1 byte and hence a string can be an array of char, like the following
char string[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
/* ^ nul: it's mandatory
* for this to be a
* string
*/
The code you posted above seems to be Java originally with some fixes to make it compile, but if you enable compilation warnigs the compiler will really piss you off.
You can build a string from an array like I did above, or by using a pointer of char, in that case you need to point to an array or to dynamic memory allocated using malloc().
An example would be
char *hello;
hello = malloc(6);
if (hello == NULL)
return ERROR; /* or whatever is need but do not continue */
strcpy(hello, "Hello");
/* do whatever you want with hello */
free(hello);
I have managed to put i to work:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char numeroserie[9];
char descricao;
float preco;
} stProduto;
/********************** Funcoes dos produtos *************************/
void inserirProduto(stProduto produtos[], int *contadorProdutos);
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar[9]);
int main()
{
stProduto produtos[500];
int contadorProdutos=0;
inserirProduto(produtos,&contadorProdutos);
return 0;
}
void inserirProduto(stProduto produtos[], int *contadorProdutos)
{
char string1[9];
char posicao;
do
{
printf("Introduza o numero de serie do produto:");
scanf("%s",&string1);
posicao = procurarNumeroserie(produtos,*contadorProdutos, string1);
if (posicao == 0)
{
printf("Ja existe um produto com esse numero de serie!!!\n");
}
}
while(posicao == 0);
strcpy(produtos[*contadorProdutos].numeroserie,string1);
(*contadorProdutos)++;
}
int procurarNumeroserie(stProduto produtos[], int contadorProdutos, char produtoAProcurar[9])
{
int i;
char posicao;
posicao = 0;
for(i = 0 ; i < contadorProdutos ; i++)
{
if (strcmp(produtos[i].numeroserie, produtoAProcurar) == 0)
{
posicao=i;
i=contadorProdutos;
}
}
return posicao;
}
Putting numeroserie [8] i assure that the user just insert 8 characters ??
Thanks

Resources