very new to C and compiled languages. I need to basically include a line of dynamic text from another file in this code:
#if T2T_NDEFFILE_PREDEF == URI
const uint8_t T2T_DATA_DEF[] = {
#include "/home/link"
// Terminator TLV
0xF3
};
#endif
I've tried using #include to link to the text file, this works however when the text in the file 'link' changes it obviously doesn't change in the compiled executable file. Is there any simple way to do this?
The #include directive simply copies the contents of another file into your source code before it is converted into an executable by the C compiler. In other words, it's a one time process, with the other file "baked into" your code.
If you need to re-load the contents of a file "dynamically" each time the program is run, you'll need to load it in yourself using C code. Here's an example, pulled from one of my own projects:
/*
[PUBLIC] Load the contents of a file into a malloc()'d buffer.
*/
unsigned char *txtLoadFile(const char *file_name, long *length)
{
FILE *fsrc = NULL;
unsigned char *data = NULL;
long size = 0;
/* Attempt to open the requested file. */
fsrc = fopen(file_name, "rb");
if (!fsrc) { return NULL; }
/* Get the length of the file in bytes. */
fseek(fsrc, 0, SEEK_END);
size = (long)ftell(fsrc);
rewind(fsrc);
/* Copy the data into memory (with an extra zero byte, in case it's text). */
data = (unsigned char*)malloc(size + 1);
if (data)
{
memset(data, 0, size + 1);
fread(data, 1, size, fsrc);
}
fclose(fsrc);
/* Return the result. */
if (length) { *length = size; }
return data;
}
This code should be largely self-explanatory, but there are a few things worth pointing out:
The file is being opened in rb (read-binary) mode - you may need to use r (read-text) instead, depending on what you're doing. If so, you'll probably want to store the data using a plain char* rather than an unsigned char * as I've done here.
An extra byte is allocated for the zero NULL-terminator character of the string.
The buffer is being stored in dynamically-allocated memory. Since you seem to be more familiar with dynamic languages such as Python or Ruby, I should remind you that you will need to free() the allocated memory once you're done with it.
Related
On my project I have this structure that has some things in it. I am asked to allocate memory, give it values and store those values in memory. Then (and I know this is weird) I am asked to move the values in memory to a file.
This is the gist of what's going on:
File1.h
typedef struct s1{
int a;
double b;
char c;
} THING;
File2.c
#include <stdlib.h>
#include <stdio.h>
#include "File1.h" // Note File1.h must be in the same directory as File2.c
int main(void)
{
THING *ptr = malloc(sizeof(THING));
if(ptr == NULL)
{
fprintf(stderr,"Memory could not be allocated!");
return 1;
}
ptr->a = 10;
ptr->b = 25.4;
ptr->c = 'A';
/*code goes here to move from memory to file*/
return 0;
}
I'm thinking I use something like this:
fwrite(&ptr, sizeof(ptr), 1, filename);
but for some reason that doesn't work. Nothing gets written.
Also, to check if it worked I would make the line like this (I'm guessing):
if( (fwrite(&ptr, sizeof(ptr), 1, filename)) != 1 )
{
printf("Thing not copied to file!");
}
Files are streams of bytes. To write data to a file, you must create a stream of bytes that holds the data in the file in some comprehensible format. Your THING structure isn't guaranteed to have any particular representation and so there is no guarantee that you will be able to read it back from the file and make sense of it.
Your code will work if you change sizeof(ptr) to sizeof(*ptr). But it will only be working by luck. You should learn how to serialize data from native data into a stream of bytes in some defined format.
I'd like to add an array of type 'struct classes' (definition included below) to a file. For instance, if allClasses[0].title is equal to "Math" and allClasses[0].class_id is equal to 1, I'd like the file to have the following input:
1Math/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0
If another class is added with a title of Science, then the file should now read
1Math/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/02Science/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0
What appears to happen is that, although the code will catch the char array part of the struct (math / science in the example), it will have trouble with the int and instead print out junk in its place (and the junk is often times longer than 1 character / 1 int long).
From experience, the code format (with a few adjustments, of course) works just fine when using a struct with variables that are only char arrays. However, it glitches out when using ints. Is this because of ASCII conversions, or something similar? How can I adjust the code so that I get the input with both the int and the char array?
void addClasses(char *given_title) {
FILE *fp;
fp = fopen("classes.db", "r");
if (numClasses == 0 && fp != NULL) {
findClasses();
}
strcpy(allClasses[numClasses].title, given_title);
allClasses[numClasses].class_id = numClasses + 1;
numClasses++;
fclose(fp);
fp = fopen("classes.db", "w");
for (int i = 0; i < numClasses; i++) {
struct classes *object = malloc(sizeof(struct classes) - 1);
memset(object, 0, sizeof( struct classes ));
object->class_id = allClasses[i].class_id;
strcpy(object->title, allClasses[i].title);
fseek(fp, numClasses * (sizeof(struct classes) - 1), SEEK_END);
fwrite(object, sizeof(struct classes) - 1, 1, fp);
}
fclose( fp );
}
The struct:
struct classes {
int class_id;
char title[30];
};
A bit of extra (possibly unnecessary) background on some of the components in the code: the bit at the beginning of the method tries to read the file and start to fill the array with any structs that were already put into the file before starting the program. I'm not including the code for that, since the aforementioned glitch happens even when I have a fresh classes.db file (and thus, even when findClasses() never runs).
Small note, by the way: I can't change the class_id into a char / char array. It needs to be an int.
If you want to add it in the text form:
fprintf(fp, "%d,\"%s\"\n", object -> class_id, object -> title);
when you open the file with "w" you create new empty file. When you write to the file you do need to fseek.
If you want to append to existing file use "a" or "a+" instead.
UPDATE IN BOTTOM====
So a while ago I made the following function, which I successfully used to get the grey values from images (w x h dimension) that were converted to .bin-files. It just gives an array of all pixel values.
It was, however, not as a function like this but put in the main() immediately.
// read the BIN-file as grayscale image
void decodeBIN(const char* filename, short image[], int w, int h){
int i = 0;
unsigned char buffer[16]; // no specific size attributed
FILE *ptr;
ptr = fopen(filename, "rb");
if (!ptr){
printf("\nUnable to open file!\n"); // error
}
while (!feof(ptr)){
fread(buffer,2,1,ptr); // read w bytes to buffer
image[i] = buffer[1];
//printf("%u ", image[i]); // DEBUG
i++;
}
fclose(ptr);
printf("\nBinary image read (npixels: %i).\n", i-1); // DEBUG
}
I decided to expand the code, so I rewrote it to the previous function and put it in a separate file for functions and also made a header file. The extra file for functions and the header file work 100% so that's not the issue. Now, this code does not work anymore and I get a stack smashing error. Some variables called after this function have also jumped to another value, so I figured the problem was with the buffer (I didn't know about the correct size for the buffer, but it worked...). After some experimentation and testing, I came up with the following function. I replaced the buffer with a char array named image2 to simply try and test it:
void decodeBIN(const char* filename, short image[], int w, int h){
int i = 0, res;
char image2[];
FILE *ptr;
ptr = fopen(filename, "rb"); //"MySnap_20180327-2239-010.bin"
if (!ptr){
printf("\nUnable to open file!\n"); // error
}
res = fread(image2,1,w*h,ptr) // need to read w*h pixels
while (i < w*h){ // DEBUG
printf("%i ", (int)image2[i]); // DEBUG
i++;
}
printf("\nRead %u bytes\n", res); // DEBUG
fclose(ptr);
printf("Binary image read (npixels: %i).\n", i); // DEBUG
}
I'm a bit lost in how it used to work and all of a sudden when I move the code from main() to a function it stops working, so any help is welcome!
Thanks in advance.
Disclaimer: I'm aiming to write this with the help of as few libraries as possible
===== UPDATE:
After the answer of #alainmerigot I got this code, which helped with getting the correct values:
void decodeBIN(const char* filename, unsigned char image[], int w, int h){
int i = 0, res;
FILE *ptr;
res = fread(image,sizeof(char),w*h,ptr) // need to read w*h pixels
fclose(ptr);
}
The segmentation fault and jumped variables are still in place though, so here a more upper-level oversight of what I'm doing:
char filenamePathed["[path of file]/file.bin"];
short img1[npixels]; // npixels = w*h
printf("i_file: %i\n", i_file); // correct value
decodeBIN(filenamePathed, img_curr, w, h); // decode bin
printf("i_file: %i\n", i_file); // value jumped
while (i < npixels){
img1[i] = (short)img_curr[i];
i++;
}
Perhaps it is good to know that I'm doing this iteratively for multiple files (time series)? I also need it to end up in a (short) format (or integer, but eventually needs to be memory-efficient and pixels have a range of 0-255 so int is a bit abundant imo).
The problem with your second function is that you write in array image2 while no space has been reserved for it. Declaring char image2[]; only says that an array exists and that the address of this array can be found in var image2, but no space is associated with it, hence the problem.
You can associate space with this array by several means.
Using permanent storage in the heap
image2 = malloc(x*y); // but do not forget to free(image2) at the end of the function
Using temporary storage in the stack (space is automatically freed when leaving the function).
image2 = alloca(x*y); // slightly faster than malloc and do not require to free() the image
But the best is to use a array with parametrized size (since C99). Your array should be declared as
char image2[w*h]; // will use the value of w and h to define array size
If you want to do other things than printing the image values in your function, you should store the image in permanent memory and have a mean to know the address of the array in your program. This is probably what you intended and is the reason why you have short image[] in your parameter list.
The solution is just simply to use image instead of image2 in fread().
But, the declaration of image should be coherent and image should be an array of char not short.
Beware also of declarations. In your first function, the image is an array of unsigned char and in the second an array of char. While the storage size is identical and fread() will store the same values, they are not equivalent. If used in an arithmetic context, image[i] will be interpreted differently and the results will likely be different. In general, images are unsigned.
Apparently, the problem was with the allocation of image, although I'm not sure why it was wrong.
I used to allocate it with unsigned char image[npixels]; and the solution to the error appeared to be unsigned char image[npixels*7];
Somehow it works, but if anyone has an explanation, please do so :)
Similar with this. But what if MAX_BOOKS would be unknown as well?
I want to get number of structures from a file.
My structure:
typedef struct material {
int mat_cislo;
char oznaceni[MAX_TEXT];
char mat_dodavatel[MAX_TEXT];
char dodavatel[MAX_TEXT];
float cena;
int mat_kusovnik;
} MATERIAL;
My code:
void nacist_material() {
FILE* pSoubor;
MATERIAL materialy_pocitadlo;
int i;
int b;
if((pSoubor = fopen(SOUBOR_MATERIAL, "rb")) == NULL ) {
printf("\nChyba při čtení souboru");
return;
}
pocet_zaznamu_materialu = 3;
printf("\n\n===>%d", pocet_zaznamu_materialu);
if(pocet_zaznamu_materialu > 0) {
printf("\nExistuje %d materialu", pocet_zaznamu_materialu);
free(pMaterialy);
pMaterialy = (MATERIAL *) malloc(pocet_zaznamu_materialu * sizeof(MATERIAL));
for(i = 0; i < pocet_zaznamu_materialu; i++) {
b = fread(&pMaterialy[i], sizeof(MATERIAL), 1, pSoubor);
}
printf("\n otrava %d", b);
}
else {
printf("\nNeexistuje předchozí záznam materialu");
}
fclose(pSoubor);
return;
}
Right now pocet_zaznamu_materialu is hard code to 3, because there are 3 structures in a file and it all works correctly. But what if number of structures in file changes?
Problem: I need to know - number of structures in file, how to a do it?
Thanks, sorry for eng
If the file is composed of nothing but a list of your desired struct stored contiguously, then the file's size, in bytes, will be a multiple of the size of your struct, and you can obtain the file size and then the number of structs in the file like so:
size_t len_file, num_structs;
fseek(fp, 0, SEEK_END);
len_file = ftell(fp);
rewind(fp);
num_structs = len_file/sizeof(MYSTRUCT);
This can be a real problem when you read from a dynamic file (another program writes at the end of file while you read it), a pipe or a network socket. In that case, you really have no way to guess the number of structs.
In that case, a common idiom is to use a dynamicaly allocated array of structs of an arbitrary size and then make it grow with realloc each time the currently allocated array is full. You could for example make the new size be twice the previous one.
That is the way C++ vectors manage their underlying array under the hood.
Have you considered adding a header to the file?
That is, place a special structure at the start of the file that tells you some information about the file. Something like ...
struct file_header {
char id[32]; /* Let this contain a special identifying string */
uint32_t version; /* version number in case the file structure changes */
uint32_t num_material; /* number of material structures in file */
};
Not only does this give you a relatively quick way to determine how many material structures you have in your file, it is also extensible. Perhaps you will want to store other structures in this file, and you want to know how many of each are in there--just add a new field and update the version.
If you want, you can even throw in some error checking.
I have a structure with the following definition:
typedef struct myStruct{
int a;
char* c;
int f;
} OBJECT;
I am able to populate this object and write it to a file. However I am not able to read the char* c value in it...while trying to read it, it gives me a segmentation fault error. Is there anything wrong with my code:
//writensave.c
#include "mystruct.h"
#include <stdio.h>
#include <string.h>
#define p(x) printf(x)
int main()
{
p("Creating file to write...\n");
FILE* file = fopen("struct.dat", "w");
if(file == NULL)
{
printf("Error opening file\n");
return -1;
}
p("creating structure\n");
OBJECT* myObj = (OBJECT*)malloc(sizeof(OBJECT));
myObj->a = 20;
myObj->f = 45;
myObj->c = (char*)calloc(30, sizeof(char));
strcpy(myObj->c,
"This is a test");
p("Writing object to file...\n");
fwrite(myObj, sizeof(OBJECT), 1, file);
p("Close file\n");
fclose(file);
p("End of program\n");
return 0;
}
Here is how I am trying to read it:
//readnprint.c
#include "mystruct.h"
#include <stdio.h>
#define p(x) printf(x)
int main()
{
FILE* file = fopen("struct.dat", "r");
char* buffer;
buffer = (char*) malloc(sizeof(OBJECT));
if(file == NULL)
{
p("Error opening file");
return -1;
}
fread((void *)buffer, sizeof(OBJECT), 1, file);
OBJECT* obj = (OBJECT*)buffer;
printf("obj->a = %d\nobj->f = %d \nobj->c = %s",
obj->a,
obj->f,
obj->c);
fclose(file);
return 0;
}
When you write your object, you're writing the pointer value to the file instead of the pointed-to information.
What you need to do is not just fwrite/fread your whole structure, but rather do it a field at a time. fwrite the a and the f as you're doing with the object, but then you need to do something special with the string. Try fwrite/fread of the length (not represented in your data structure, that's fine) and then fwrite/fread the character buffer. On read you'll need to allocate that, of course.
Your first code sample seems to assume that the strings are going to be no larger than 30 characters. If this is the case, then the easiest fix is probably to re-define your structure like this:
typedef struct myStruct{
int a;
char c[30];
int f;
} OBJECT;
Otherwise, you're just storing a pointer to dynamically-allocated memory that will be destroyed when your program exits (so when you retrieve this pointer later, the address is worthless and most likely illegal to access).
You're saving a pointer to a char, not the string itself. When you try to reload the file you're running in a new process with a different address space and that pointer is no longer valid. You need to save the string by value instead.
I would like to add a note about a potential portability issue, which may or may not exist depending upon the planned use of the data file.
If the data file is to be shared between computers of different endian-ness, you will need to configure file-to-host and host-to-file converters for non-char types (int, short, long, long long, ...). Furthermore, it could be prudent to use the types from stdint.h (int16_t, int32_t, ...) instead to guarantee the size you want.
However, if the data file will not be moving around anywhere, then ignore these two points.
The char * field of your structure is known as a variable length field. When you write this field, you will need a method for determining the length of the text. Two popular methods are:
1. Writing Size First
2. Writing terminal character
Writing Size First
In this method, the size of the text data is written first, followed immediately by the data.
Advantages: Text can load quicker by block reads.
Disadvantages: Two reads required, extra space required for the length data.
Example code fragment:
struct My_Struct
{
char * text_field;
};
void Write_Text_Field(struct My_Struct * p_struct, FILE * output)
{
size_t text_length = strlen(p_struct->text_field);
fprintf(output, "%d\n", text_length);
fprintf(output, "%s", p_struct->text_field);
return;
}
void Read_Text_Field(struct My_STruct * p_struct, FILE * input)
{
size_t text_length = 0;
char * p_text = NULL;
fscanf(input, "%d", &text_length);
p_text = (char *) malloc(text_length + sizeof('\0'));
if (p_text)
{
fread(p_text, 1, text_length, input);
p_text[text_length] = '\0';
}
}
Writing terminal character
In this method the text data is written followed by a "terminal" character. Very similar to a C language string.
Advantages: Requires less space than Size First.
Disadvantages: Text must be read one byte at a time so terminal character is not missed.
Fixed size field
Instead of using a char* as a member, use a char [N], where N is the maximum size of the field.
Advantages: Fixed sized records can be read as blocks.
Makes random access in files easier.
Disadvantages: Waste of space if all the field space is not used.
Problems when the field size is too small.
When writing data structures to a file, you should consider using a database. There are small ones such as SQLite and bigger ones such as MySQL. Don't waste time writing and debugging permanent storage routines for your data when they have already been written and tested.