saving a struct for post fetchingWhat's the difference between struct and class in .NET?When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?How to initialize a struct in accordance with C programming language standardsWhy are mutable structs “evil”?When to use struct?Difference between 'struct' and 'typedef struct' in C++?problem with flushing input stream Ctypedef struct vs struct definitionsint main(int argc, const char * argv[]) AND file input
How do I extract a value from a time formatted value in excel?
Was Spock the First Vulcan in Starfleet?
Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?
How to check is there any negative term in a large list?
Method to test if a number is a perfect power?
Term for the "extreme-extension" version of a straw man fallacy?
System.debug(JSON.Serialize(o)) Not longer shows full string
Is a stroke of luck acceptable after a series of unfavorable events?
How can a function with a hole (removable discontinuity) equal a function with no hole?
Unreliable Magic - Is it worth it?
Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?
Why Were Madagascar and New Zealand Discovered So Late?
How does Loki do this?
Pole-zeros of a real-valued causal FIR system
How do scammers retract money, while you can’t?
Gears on left are inverse to gears on right?
What is the intuitive meaning of having a linear relationship between the logs of two variables?
Opposite of a diet
Are student evaluations of teaching assistants read by others in the faculty?
Would a high gravity rocky planet be guaranteed to have an atmosphere?
Why not increase contact surface when reentering the atmosphere?
Short story about space worker geeks who zone out by 'listening' to radiation from stars
How to pronounce the slash sign
How did Doctor Strange see the winning outcome in Avengers: Infinity War?
saving a struct for post fetching
What's the difference between struct and class in .NET?When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?How to initialize a struct in accordance with C programming language standardsWhy are mutable structs “evil”?When to use struct?Difference between 'struct' and 'typedef struct' in C++?problem with flushing input stream Ctypedef struct vs struct definitionsint main(int argc, const char * argv[]) AND file input
I am newbie to C. I have a csv file with a particular structure. I created struct and and read the data from csv file and print it using defined struct. However, instead of printing the struct I need to save it so I an access it for later processing. So far, I have understood I need to use dynamic memory allocation but I am totally lost right now. any leads would be really useful.
The inputfile as follows,
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
My code so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input, const char *separators)
char *p;
struct O_data tmp;
if(sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
return NULL;
return data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data somedata;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
deserialize_data(&somedata, line, ",");
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
printf("index: %d, x: %f, y: %f, z: %fn", somedata.index, somedata.x, somedata.y, somedata.z);
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
c struct dynamic-memory-allocation
add a comment |
I am newbie to C. I have a csv file with a particular structure. I created struct and and read the data from csv file and print it using defined struct. However, instead of printing the struct I need to save it so I an access it for later processing. So far, I have understood I need to use dynamic memory allocation but I am totally lost right now. any leads would be really useful.
The inputfile as follows,
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
My code so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input, const char *separators)
char *p;
struct O_data tmp;
if(sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
return NULL;
return data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data somedata;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
deserialize_data(&somedata, line, ",");
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
printf("index: %d, x: %f, y: %f, z: %fn", somedata.index, somedata.x, somedata.y, somedata.z);
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
c struct dynamic-memory-allocation
add a comment |
I am newbie to C. I have a csv file with a particular structure. I created struct and and read the data from csv file and print it using defined struct. However, instead of printing the struct I need to save it so I an access it for later processing. So far, I have understood I need to use dynamic memory allocation but I am totally lost right now. any leads would be really useful.
The inputfile as follows,
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
My code so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input, const char *separators)
char *p;
struct O_data tmp;
if(sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
return NULL;
return data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data somedata;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
deserialize_data(&somedata, line, ",");
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
printf("index: %d, x: %f, y: %f, z: %fn", somedata.index, somedata.x, somedata.y, somedata.z);
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
c struct dynamic-memory-allocation
I am newbie to C. I have a csv file with a particular structure. I created struct and and read the data from csv file and print it using defined struct. However, instead of printing the struct I need to save it so I an access it for later processing. So far, I have understood I need to use dynamic memory allocation but I am totally lost right now. any leads would be really useful.
The inputfile as follows,
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
My code so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input, const char *separators)
char *p;
struct O_data tmp;
if(sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
return NULL;
return data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data somedata;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
deserialize_data(&somedata, line, ",");
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
printf("index: %d, x: %f, y: %f, z: %fn", somedata.index, somedata.x, somedata.y, somedata.z);
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
c struct dynamic-memory-allocation
c struct dynamic-memory-allocation
asked Mar 21 at 15:59
dipak sanapdipak sanap
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
you can use an array of struct O_data
using malloc then realloc to allocate then make longer that array having an unknown number of entries until you read all the file
Warning in deserialize_dat
sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z)
index is unsigned but you use %d
, must be %u
, it is the same in printf in main
tmp and p are unused, like the parameter separators
A proposal can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input)
return (sscanf(input, "%u,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
? NULL : data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data * somedata = NULL;
size_t nelts = 0;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
if ((somedata = realloc(somedata, (nelts + 1) * sizeof(struct O_data))) == NULL)
fprintf(stderr, "error not enough memory");
exit(EXIT_FAILURE);
deserialize_data(&somedata[nelts++], line);
free(line);
fclose(stream);
/* print and free */
for (size_t i = 0; i != nelts; ++i)
printf("index: %u, x: %f, y: %f, z: %fn",
somedata[i].index, somedata[i].x, somedata[i].y, somedata[i].z);
free(somedata);
exit(EXIT_SUCCESS);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra a.c
pi@raspberrypi:/tmp $ cat f
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
pi@raspberrypi:/tmp $ ./a.out f
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out f
==2439== Memcheck, a memory error detector
==2439== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2439== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2439== Command: ./a.out f
==2439==
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
==2439==
==2439== HEAP SUMMARY:
==2439== in use at exit: 0 bytes in 0 blocks
==2439== total heap usage: 9 allocs, 9 frees, 5,832 bytes allocated
==2439==
==2439== All heap blocks were freed -- no leaks are possible
==2439==
==2439== For counts of detected and suppressed errors, rerun with: -v
==2439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Note I realloc adding only one entry in the array each time, if there are a lot of values in the file it can be better to add several entries rather than just one in the realloc when needed
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284569%2fsaving-a-struct-for-post-fetching%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
you can use an array of struct O_data
using malloc then realloc to allocate then make longer that array having an unknown number of entries until you read all the file
Warning in deserialize_dat
sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z)
index is unsigned but you use %d
, must be %u
, it is the same in printf in main
tmp and p are unused, like the parameter separators
A proposal can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input)
return (sscanf(input, "%u,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
? NULL : data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data * somedata = NULL;
size_t nelts = 0;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
if ((somedata = realloc(somedata, (nelts + 1) * sizeof(struct O_data))) == NULL)
fprintf(stderr, "error not enough memory");
exit(EXIT_FAILURE);
deserialize_data(&somedata[nelts++], line);
free(line);
fclose(stream);
/* print and free */
for (size_t i = 0; i != nelts; ++i)
printf("index: %u, x: %f, y: %f, z: %fn",
somedata[i].index, somedata[i].x, somedata[i].y, somedata[i].z);
free(somedata);
exit(EXIT_SUCCESS);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra a.c
pi@raspberrypi:/tmp $ cat f
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
pi@raspberrypi:/tmp $ ./a.out f
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out f
==2439== Memcheck, a memory error detector
==2439== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2439== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2439== Command: ./a.out f
==2439==
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
==2439==
==2439== HEAP SUMMARY:
==2439== in use at exit: 0 bytes in 0 blocks
==2439== total heap usage: 9 allocs, 9 frees, 5,832 bytes allocated
==2439==
==2439== All heap blocks were freed -- no leaks are possible
==2439==
==2439== For counts of detected and suppressed errors, rerun with: -v
==2439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Note I realloc adding only one entry in the array each time, if there are a lot of values in the file it can be better to add several entries rather than just one in the realloc when needed
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
add a comment |
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
you can use an array of struct O_data
using malloc then realloc to allocate then make longer that array having an unknown number of entries until you read all the file
Warning in deserialize_dat
sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z)
index is unsigned but you use %d
, must be %u
, it is the same in printf in main
tmp and p are unused, like the parameter separators
A proposal can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input)
return (sscanf(input, "%u,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
? NULL : data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data * somedata = NULL;
size_t nelts = 0;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
if ((somedata = realloc(somedata, (nelts + 1) * sizeof(struct O_data))) == NULL)
fprintf(stderr, "error not enough memory");
exit(EXIT_FAILURE);
deserialize_data(&somedata[nelts++], line);
free(line);
fclose(stream);
/* print and free */
for (size_t i = 0; i != nelts; ++i)
printf("index: %u, x: %f, y: %f, z: %fn",
somedata[i].index, somedata[i].x, somedata[i].y, somedata[i].z);
free(somedata);
exit(EXIT_SUCCESS);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra a.c
pi@raspberrypi:/tmp $ cat f
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
pi@raspberrypi:/tmp $ ./a.out f
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out f
==2439== Memcheck, a memory error detector
==2439== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2439== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2439== Command: ./a.out f
==2439==
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
==2439==
==2439== HEAP SUMMARY:
==2439== in use at exit: 0 bytes in 0 blocks
==2439== total heap usage: 9 allocs, 9 frees, 5,832 bytes allocated
==2439==
==2439== All heap blocks were freed -- no leaks are possible
==2439==
==2439== For counts of detected and suppressed errors, rerun with: -v
==2439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Note I realloc adding only one entry in the array each time, if there are a lot of values in the file it can be better to add several entries rather than just one in the realloc when needed
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
add a comment |
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
you can use an array of struct O_data
using malloc then realloc to allocate then make longer that array having an unknown number of entries until you read all the file
Warning in deserialize_dat
sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z)
index is unsigned but you use %d
, must be %u
, it is the same in printf in main
tmp and p are unused, like the parameter separators
A proposal can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input)
return (sscanf(input, "%u,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
? NULL : data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data * somedata = NULL;
size_t nelts = 0;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
if ((somedata = realloc(somedata, (nelts + 1) * sizeof(struct O_data))) == NULL)
fprintf(stderr, "error not enough memory");
exit(EXIT_FAILURE);
deserialize_data(&somedata[nelts++], line);
free(line);
fclose(stream);
/* print and free */
for (size_t i = 0; i != nelts; ++i)
printf("index: %u, x: %f, y: %f, z: %fn",
somedata[i].index, somedata[i].x, somedata[i].y, somedata[i].z);
free(somedata);
exit(EXIT_SUCCESS);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra a.c
pi@raspberrypi:/tmp $ cat f
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
pi@raspberrypi:/tmp $ ./a.out f
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out f
==2439== Memcheck, a memory error detector
==2439== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2439== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2439== Command: ./a.out f
==2439==
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
==2439==
==2439== HEAP SUMMARY:
==2439== in use at exit: 0 bytes in 0 blocks
==2439== total heap usage: 9 allocs, 9 frees, 5,832 bytes allocated
==2439==
==2439== All heap blocks were freed -- no leaks are possible
==2439==
==2439== For counts of detected and suppressed errors, rerun with: -v
==2439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Note I realloc adding only one entry in the array each time, if there are a lot of values in the file it can be better to add several entries rather than just one in the realloc when needed
// How do I save some data to memory here to access it later like somedata[i] for ith struct later outside main.
you can use an array of struct O_data
using malloc then realloc to allocate then make longer that array having an unknown number of entries until you read all the file
Warning in deserialize_dat
sscanf(input, "%d,%f,%f,%f", &data->index, &data->x, &data->y, &data->z)
index is unsigned but you use %d
, must be %u
, it is the same in printf in main
tmp and p are unused, like the parameter separators
A proposal can be :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct O_data
unsigned int index;
float x;
float y;
float z;
;
struct O_data * deserialize_data(struct O_data *data, const char *input)
return (sscanf(input, "%u,%f,%f,%f", &data->index, &data->x, &data->y, &data->z) != 7)
? NULL : data;
int main(int argc, char *argv[])
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
struct O_data * somedata = NULL;
size_t nelts = 0;
if (argc != 2)
fprintf(stderr, "Usage: %s <file>n", argv[0]);
exit(EXIT_FAILURE);
stream = fopen(argv[1], "r");
if (stream == NULL)
perror("fopen");
exit(EXIT_FAILURE);
while ((nread = getline(&line, &len, stream)) != -1)
if ((somedata = realloc(somedata, (nelts + 1) * sizeof(struct O_data))) == NULL)
fprintf(stderr, "error not enough memory");
exit(EXIT_FAILURE);
deserialize_data(&somedata[nelts++], line);
free(line);
fclose(stream);
/* print and free */
for (size_t i = 0; i != nelts; ++i)
printf("index: %u, x: %f, y: %f, z: %fn",
somedata[i].index, somedata[i].x, somedata[i].y, somedata[i].z);
free(somedata);
exit(EXIT_SUCCESS);
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra a.c
pi@raspberrypi:/tmp $ cat f
2,33.1609992980957,26.59000015258789,8.003999710083008
5,15.85200023651123,13.036999702453613,31.801000595092773
8,10.907999992370605,32.000999450683594,1.8459999561309814
11,28.3700008392334,31.650999069213867,13.107999801635742
14,7.046000003814697,23.5939998626709,6.254000186920166
pi@raspberrypi:/tmp $ ./a.out f
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out f
==2439== Memcheck, a memory error detector
==2439== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2439== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2439== Command: ./a.out f
==2439==
index: 2, x: 33.160999, y: 26.590000, z: 8.004000
index: 5, x: 15.852000, y: 13.037000, z: 31.801001
index: 8, x: 10.908000, y: 32.000999, z: 1.846000
index: 11, x: 28.370001, y: 31.650999, z: 13.108000
index: 14, x: 7.046000, y: 23.594000, z: 6.254000
==2439==
==2439== HEAP SUMMARY:
==2439== in use at exit: 0 bytes in 0 blocks
==2439== total heap usage: 9 allocs, 9 frees, 5,832 bytes allocated
==2439==
==2439== All heap blocks were freed -- no leaks are possible
==2439==
==2439== For counts of detected and suppressed errors, rerun with: -v
==2439== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Note I realloc adding only one entry in the array each time, if there are a lot of values in the file it can be better to add several entries rather than just one in the realloc when needed
edited Mar 21 at 16:52
answered Mar 21 at 16:08
brunobruno
12k31326
12k31326
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
add a comment |
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
Hi, thanks for the reply. I am trying exactly what you suggested but I need some lead. I am totally lost there.
– dipak sanap
Mar 21 at 16:16
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
@dipaksanap I edited my answer with a proposal and executions. Note I Move the printf after the loop where you read in the file to show the values are memorized
– bruno
Mar 21 at 16:29
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
Hi @bruno , it works well ! Thanks a ton. I just have a small question, you mentioned to add several lines in case of large data. Do I just add (nelts + bignumber). Also, what does ? Null : data ; do ? Thanks.
– dipak sanap
Mar 21 at 17:47
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
@dipaksanap currently I know there is not enough space at each loop because grow one by one, if more you need to differentiate let's say the allocatedNelts and the usedNelts to know when you need to reallocate (they are equal) and yes realloc with allocatedNelts + number_greater_than_1
– bruno
Mar 21 at 17:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284569%2fsaving-a-struct-for-post-fetching%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown