shm_fileuploader.c:
#include "sharedMemory.h"
struct filesharing_struct temp()
{
printf("Enter the name of the file. ");
scanf("%s", &fname);
}
sharedMemory.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
//void* get();
const int SIZE = 40960;
const char *name = "obaloch_filesharing";
struct filesharing_struct{
char flag;
int size;
char content;
char fname[50];
};
//struct filesharing_struct temp;
I am getting an error saying "error: ‘fname’ undeclared" and im not sure why since fname is declared in my header file. Is there anyway i can use fname in the from the .h file in the .c file?
You need to declare a variable whose type is the structure, and scan into its fname member.
Then you can return the structure from the function, to satisfy the return type.
struct filesharing_struct temp()
{
struct filesharing_struct temp;
printf("Enter the name of the file. ");
scanf("%s", &temp.fname);
return temp;
}
Related
I'm trying to get a struct variable from another file in c. But when I do define a variable inside of other file and trying to printing this variable in main file it didn't work.
Main File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
int main()
{
struct tst t;
this_test(t);
printf("%s", t.string);
return 0;
}
Other File Header
#ifndef _TESST_H
#define _TESST_H
struct tst
{
char *string;
};
void this_test(struct tst t);
#endif
Other File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
void this_test(struct tst t)
{
t.string = "this is test";
}
When I tried to execute this program it print nothing. How can I solve this problem?
The structure passed as a parameter to the function is only a copy. To modify the structure of the main function in the this_test function, you must pass its address. It's like the scanf function where you have to pass the address of the variables you want to modify.
#include <stdio.h>
struct tst
{
char *string;
};
void this_test(struct tst *t)
{
t->string = "this is test";
}
int main()
{
struct tst t;
this_test(&t);
printf("%s", t.string);
return 0;
}
Here is a sample c code related to stat.h. bits/stat.h that mentioned "Never include <bits/stat.h> directly; use <sys/stat.h> instead.". However struct stat is defined in bits/stat.h, and int __xstat (...) is defined in sys/stat.h. The code won't compile with any one of headers or even both of them. How to make it copiled while only changing the #include ... without changing any one of the functions?
#include <stdio.h>
#include <bits/stat.h>
#include <sys/stat.h>
int stat_1(char *filename, struct stat *stat_buf)
{
return __xstat(1, filename, stat_buf); // extern int __xstat (...) defined in sys/stat.h
}
char * test(const char *filename) {
char *result;
stat stat_buf; // struct stat defined in bits/stat.h
printf("DO something here");
if ( stat_1(filename, &sbuf) == -1 ) {
printf("DO something here");
}
return result;
}
int main() {
const char *fileName = "file.txt";
test(fileName);
return 0;
}
You should be calling stat see https://linux.die.net/man/2/stat. Not __xstat.
Interacting with names that start with __ is almost always a sign you are doing something wrong. They are under the hood implementation things
For stat and its associated struct, you should likely be includeing:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
I have a header file structs.h as follows:
#include <stdio.h>
typedef struct
{
char *name;
}
student;
In the file structs.c,
#include <stdio.h>
#include "structs.h"
int main()
{
student students[3];
scanf("%s", &students[0].name);
return 0;
}
The scanf statement gives an error since it says %s is of type char* but argument is of type char**. Removing the & gives a segmentation fault.
How else would I write this? It is a array within an array of struct variables but it should be able to take input...
I have a 'struct' which I would like to use in multiple sources files. I have declared the struct in the Header File and then have included in the sources files. It would be great if someone can assist me in this problem.
I am posting Header, Source and Error
#ifndef DATABASE_H
#define DATABASE_H
struct dataBase
{
char modelName;
float capacity;
int mileage;
char color;
};
extern struct dataBase Inputs;
#endif /* DATABASE_H */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "dataBase.h"
struct dataBase Inputs = NULL;
//size_t Inputs_Size = 0;
int main (void)
#include "hw4_asharma_display.h"
#include <stdio.h>
#include "dataBase.h"
void printLow(int size)
{
// Declaring Variables
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n",
Inputs[i].modelName,
Inputs[i].capacity,
Inputs[i].mileage,
Inputs[i].color);
}
hw4_asharma_display.c:14:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
~~~~~~^~
hw4_asharma_display.c:29:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
Inputs is not an array, so you can't just use the [i] index notation. You'll have to change its declaration from:
struct dataBase Inputs = NULL;
(btw the NULL part is pointless) to
struct dataBase Inputs[N];
Instead, if you meant to have only one element, keep the declaration:
struct dataBase Inputs;
but remove the [i] part:
printf("%c %f %d %c\n",
Inputs.modelName,
Inputs.capacity,
Inputs.mileage,
Inputs.color);
Also, you will have to fill each element before printing or you will get all zeroes and blanks.
I'm trying the write a service that pings the watchdog device using the linux watchdog driver.
In the function named 'LoadConfigurationFile' I pass a pointer to a struct that is defined above. That function then gets a string and stores it at the address of the variable in the struct with a library call(libconfig).
However when I access the variable 'printf("%s\n", options.devicepath);return 1;' instead of printing the contents of configuration file as expected: "/dev/watchdog", the program displays "�/watchdog".
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <linux/types.h>
#include <linux/watchdog.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <libconfig.h>
struct cfgoptions {
const char* devicepath;
};
struct cfgoptions options;
char *confile = "/etc/watchdogd.cfg";
int LoadConfigurationFile(struct cfgoptions *s);
int main(int argc, char *argv[])
{
struct cfgoptions *st_ptr;
st_ptr = &options;
if (LoadConfigurationFile(st_ptr) < 0)
/*exit(EXIT_FAILURE);*/
/**/
printf("%s\n", options.devicepath);return 1;
/**/
int LoadConfigurationFile(struct cfgoptions *s)
{
config_t cfg;
config_init(&cfg);
if (!config_read_file(&cfg, confile) && config_error_file(&cfg) == NULL)
{
fprintf(stderr, "daemon: cannot open configuration file: %s\n", config_error_file(&cfg));
config_destroy(&cfg);
return -1;
} else if (!config_read_file(&cfg, confile)) {
fprintf(stderr, "daemon:%s:%d: %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
config_destroy(&cfg);
return -1;
}
if (!config_lookup_string(&cfg, "watchdog-device", &s->devicepath))
s->devicepath = "/dev/watchdog";
config_destroy(&cfg);
return 0;
}
/etc/watchdogd.cfg:
watchdog-device = "/dev/watchdog"
int config_setting_lookup_string(const config_setting_t *setting,
const char *name, const char **value)
{
config_setting_t *member = config_setting_get_member(setting, name);
if(! member)
return(CONFIG_FALSE);
if(config_setting_type(member) != CONFIG_TYPE_STRING)
return(CONFIG_FALSE);
*value = config_setting_get_string(member);
return(CONFIG_TRUE);
}
config_setting_t *config_setting_get_member(const config_setting_t *setting,
const char *name)
{
if(setting->type != CONFIG_TYPE_GROUP)
return(NULL);
return(__config_list_search(setting->value.list, name, NULL));
}
const char *config_setting_get_string(const config_setting_t *setting)
{
return((setting->type == CONFIG_TYPE_STRING) ? setting->value.sval : NULL);
}
the problem is you're calling config_destroy. That loses all your data allocated during lookups..
— Function: void config_destroy (config_t * config)
These functions initialize and destroy the configuration object config.
config_init() initializes the config_t structure pointed to by config as a new, empty configuration.
config_destroy() destroys the configuration config, deallocating all memory associated with the configuration, but does not attempt to deallocate the config_t structure itself.
Pretty sure on this.
Try getting rid of that line and see what happens.
More: When libconfig allocates memory for the pointers you pass in, it has to keep the memory references around somewhere so it can clean them up later. That's what the cfg object is. It's just a big record keeping object for everything the library has allocated. When you destroy it, the library must free all allocated memory or else it will be leaked forever.