Using fwrite without calling fflush or writing to stderr first - c

I have a function which writes binary data to a file or stdout. However the call to fwrite() sometimes fails, UNLESS I fflush(stderr) or print a report to stderr before attempting to fwrite.
Is this normal behaviour, or indicative of some underlying memory problem? It's difficult to debug because as soon as I fprint to stderr the data I'm trying to fwrite to stdout, the "problem" disappears.
Here's a very simplified version of the function.
int writebinary(FILE *fpout, void *data, long ntowrite) {
long i;
/* lets say we know the input data is double float */
double *p = data;
/* including this test line makes the function work! */
for(i=0; i<ntowrite;i++) fprintf(stderr,"data[%d]=%g\n",i,p[i]);
/* attempt to write data[] as a single block of bytes */
m= fwrite(data,(size_t)(ntowrite*sizeof(double)),1,fpout);
if(m!=1) {
fprintf(stderr,"Write error: %d\n",ferror(fpout));
return(-1);
}
else return(m);
}
Any wisdom appreciated :)

This is not normal behavior.
Recommend the following. There are a number of things in the posting which are of concern (see far below) but do not point to the problem - but something is wrong.
int writebinary2(FILE *fpout, void *data, long ntowrite) {
size_t n = (size_t) ntowrite;
double *dp = (double *) data;
// pedantic checks
if ((!fpout) || (!data) || (dp != data) || (ntowrite <= 0) || (n != ntowrite)) {
fprintf(stderr,"Bad parameter\n");
return(0);
}
clearerr(stderr); // only needed for debug
clearerr(fpout);
/* attempt to write blocks of doubles */
size_t m = fwrite(dp, sizeof(double), n, fpout);
if (m != n) {
fprintf(stderr,"Write error: %d\n",ferror(fpout));
clearerr(fpout);
return(-1);
}
if (fflush(fpout)) {
fprintf(stderr,"Flush error: %d\n",ferror(fpout));
clearerr(fpout);
return(-1);
}
return(1);
}
ntowrite s/b size_t, an unsigned integer. ntowrite = 0 may cause confusion on m. ntowrite < 0 will cause issues.
m is not declared, assumed size_t.
format "data[%d]=%g\n" s/b "data[%ld]=%g\n"
i s/b size_t.
(size_t)(ntowrite*sizeof(double)) s/b ((size_t)ntowrite)*sizeof(double). See #7.
fprintf(stderr... should not be needed. That is the question.
fwrite(..., size, n ...) works even if size * n overflow. So safer than OP.

Related

Dynamic allocation of array of strings fails. realloc() error

I am trying to write a simple program that will read words from a file and print the number of occurrences of a particular word passed to it as argument.
For that, I use fscanf to read the words and copy them into an array of strings that is dynamically allocated.
For some reason, I get an error message.
Here is the code for the readFile function:
void readFile(char** buffer, char** argv){
unsigned int i=0;
FILE* file;
file = fopen(argv[1], "r");
do{
buffer = realloc(buffer, sizeof(char*));
buffer[i] = malloc(46);
}while(fscanf(file, "%s", buffer[i++]));
fclose(file);
}
And here is the main function :
int main(int argc, char** argv){
char** buffer = NULL;
readFile(buffer, argv);
printf("%s\n", buffer[0]);
return 0;
}
I get the following error message :
realloc(): invalid next size
Aborted (core dumped)
I have looked at other threads on this topic but none of them seem to be of help. I could not apply whatever I learned there to my problem.
I used a debugger (VS Code with gdb). Data is written successfully into indices 0,1,2,3 of the buffer array but says error : Cannot access memory at address 0xfbad2488 for index 4 and pauses on exception.
Another thread on this topic suggests there might be a wild pointer somewhere. But I don't see one anywhere.
I have spent days trying to figure this out. Any help will be greatly appreciated.
Thanks.
Your algorithm is wrong on many fronts, including:
buffer is passed by-value. Any modifications where buffer = ... is the assignment will mean nothing to the caller. In C, arguments are always pass-by-value (arrays included, but their "value" is a conversion to temporary pointer to first element, so you get a by-ref synonym there whether you want it or not).
Your realloc usage is wrong. It should be expanding based on the iteration of the loop as a count multiplied by the size of a char *. You only have the latter, with no count multiplier. Therefore, you never allocate more than a single char * with that realloc call.
Your loop termination condition is wrong. Your fscanf call should check for the expected number of arguments to be processed, which in your case is 1. Instead, you're looking for any non-zero value, which EOF is going to be when you hit it. Therefore, the loop never terminates.
Your fscanf call is not protected from buffer overflow : You're allocating a static-sized string for each string read, but not limiting the %s format to the static size specified. This is a recipe for buffer-overflow.
No IO functions are ever checked for success/failure : The following APIs could fail, yet you never check that possibility: fopen, fscanf, realloc, malloc. In failing to do so, you're violating Henry Spencer's 6th Commandment for C Programmers : "If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest ``it cannot happen to me'', the gods shall surely punish thee for thy arrogance."
No mechanism for communicating the allocated string count to the caller : The caller of this function is expecting a resulting char**. Assuming you fix the first item in this list, you still have not provided the caller any means of knowing how long that pointer sequence is when readFile returns. An out-parameter and/or a formal structure is a possible solution to this. Or perhaps a terminating NULL pointer to indicate the list is finished.
(Moderate) You never check argc : Instead, you just send argv directly to readFile, and assume the file name will be at argv[1] and always be valid. Don't do that. readFile should take either a FILE* or a single const char * file name, and act accordingly. It would be considerably more robust.
(Minor) : Extra allocation : Even fixing the above items, you'll still leave one extra buffer allocation in your sequence; the one that failed to read. Not that it matter much in this case, as the caller has no idea how many strings were allocated in the first place (see previous item).
Shoring up all of the above would require a basic rewrite of nearly everything you have posted. In the end, the code would look so different, it's almost not worth trying to salvage what is here. Instead, look at what you have done, look at this list, and see where things went wrong. There's plenty to choose from.
Sample
#include <stdio.h>
#include <stdlib.h>
#define STR_MAX_LEN 46
char ** readFile(const char *fname)
{
char **strs = NULL;
int len = 0;
FILE *fp = fopen(fname, "r");
if (fp != NULL)
{
do
{
// array expansion
void *tmp = realloc(strs, (len+1) * sizeof *strs);
if (tmp == NULL)
{
// failed. cleanup prior success
perror("Failed to expand pointer array");
for (int i=0; i<len; ++i)
free(strs[i]);
free(strs);
strs = NULL;
break;
}
// allocation was good; save off new pointer
strs = tmp;
strs[len] = malloc( STR_MAX_LEN );
if (strs[len] == NULL)
{
// failed. cleanup prior sucess
perror("Failed to allocate string buffer");
for (int i=0; i<len; ++i)
free(strs[i]);
free(strs);
strs = NULL;
break;
}
if (fscanf(fp, "%45s", strs[len]) == 1)
{
++len;
}
else
{
// read failed. we're leaving regardless. the last
// allocation is thrown out, but we terminate the list
// with a NULL to indicate end-of-list to the caller
free(strs[len]);
strs[len] = NULL;
break;
}
} while (1);
fclose(fp);
}
return strs;
}
int main(int argc, char *argv[])
{
if (argc < 2)
exit(EXIT_FAILURE);
char **strs = readFile(argv[1]);
if (strs)
{
// enumerate and free in the same loop
for (char **pp = strs; *pp; ++pp)
{
puts(*pp);
free(*pp);
}
// free the now-defunct pointer array
free(strs);
}
return EXIT_SUCCESS;
}
Output (run against /usr/share/dict/words)
A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
Aaron
Aaronic
Aaronical
Aaronite
Aaronitic
Aaru
Ab
aba
Ababdeh
Ababua
abac
abaca
......
zymotechny
zymotic
zymotically
zymotize
zymotoxic
zymurgy
Zyrenian
Zyrian
Zyryan
zythem
Zythia
zythum
Zyzomys
Zyzzogeton
Improvements
The secondary malloc in this code is completely pointless. You're using a fixed length word maximum size, so you could easily retool you array to be a pointer to use this:
char (*strs)[STR_MAX_LEN]
and simply eliminate the per-string malloc code entirely. That does leave the problem of how to tell the caller how many strings were allocated. In the prior version we used a NULL pointer to indicate end-of-list. In this version we can simply use a zero-length string. Doing this makes the declaration of readFile rather odd looking, but for returning a pointer-to-array-of-size-N, its' correct. See below:
#include <stdio.h>
#include <stdlib.h>
#define STR_MAX_LEN 46
char (*readFile(const char *fname))[STR_MAX_LEN]
{
char (*strs)[STR_MAX_LEN] = NULL;
int len = 0;
FILE *fp = fopen(fname, "r");
if (fp != NULL)
{
do
{
// array expansion
void *tmp = realloc(strs, (len+1) * sizeof *strs);
if (tmp == NULL)
{
// failed. cleanup prior success
perror("Failed to expand pointer array");
free(strs);
strs = NULL;
break;
}
// allocation was good; save off new pointer
strs = tmp;
if (fscanf(fp, "%45s", strs[len]) == 1)
{
++len;
}
else
{
// read failed. make the final string zero-length
strs[len][0] = 0;
break;
}
} while (1);
fclose(fp);
}
return strs;
}
int main(int argc, char *argv[])
{
if (argc < 2)
exit(EXIT_FAILURE);
char (*strs)[STR_MAX_LEN] = readFile(argv[1]);
if (strs)
{
// enumerate and free in the same loop
for (char (*s)[STR_MAX_LEN] = strs; (*s)[0]; ++s)
puts(*s);
free(strs);
}
return EXIT_SUCCESS;
}
The output is the same as before.
Another Improvement: Geometric Growth
With a few simple changes we can significantly cut down on the realloc invokes (we're currently doing one per string added) by only doing them in a double-size growth pattern. If each time we reallocate, we double the size of the prior allocation, we will make more and more space available for reading larger numbers of strings before the next allocation:
#include <stdio.h>
#include <stdlib.h>
#define STR_MAX_LEN 46
char (*readFile(const char *fname))[STR_MAX_LEN]
{
char (*strs)[STR_MAX_LEN] = NULL;
int len = 0;
int capacity = 0;
FILE *fp = fopen(fname, "r");
if (fp != NULL)
{
do
{
if (len == capacity)
{
printf("Expanding capacity to %d\n", (2 * capacity + 1));
void *tmp = realloc(strs, (2 * capacity + 1) * sizeof *strs);
if (tmp == NULL)
{
// failed. cleanup prior success
perror("Failed to expand string array");
free(strs);
strs = NULL;
break;
}
// save the new string pointer and capacity
strs = tmp;
capacity = 2 * capacity + 1;
}
if (fscanf(fp, "%45s", strs[len]) == 1)
{
++len;
}
else
{
// read failed. make the final string zero-length
strs[len][0] = 0;
break;
}
} while (1);
// shrink if needed. remember to retain the final empty string
if (strs && (len+1) < capacity)
{
printf("Shrinking capacity to %d\n", len);
void *tmp = realloc(strs, (len+1) * sizeof *strs);
if (tmp)
strs = tmp;
}
fclose(fp);
}
return strs;
}
int main(int argc, char *argv[])
{
if (argc < 2)
exit(EXIT_FAILURE);
char (*strs)[STR_MAX_LEN] = readFile(argv[1]);
if (strs)
{
// enumerate and free in the same loop
for (char (*s)[STR_MAX_LEN] = strs; (*s)[0]; ++s)
puts(*s);
// free the now-defunct pointer array
free(strs);
}
return EXIT_SUCCESS;
}
Output
The output is the same as before, but I added instrumentation to show when expansion happens to illustrate the expansions and final shrinking. I'll leave out the rest of the output (which is over 200k lines of words)
Expanding capacity to 1
Expanding capacity to 3
Expanding capacity to 7
Expanding capacity to 15
Expanding capacity to 31
Expanding capacity to 63
Expanding capacity to 127
Expanding capacity to 255
Expanding capacity to 511
Expanding capacity to 1023
Expanding capacity to 2047
Expanding capacity to 4095
Expanding capacity to 8191
Expanding capacity to 16383
Expanding capacity to 32767
Expanding capacity to 65535
Expanding capacity to 131071
Expanding capacity to 262143
Shrinking capacity to 235886

C write struct to file

I am trying to save struct data to file. I saved the data this way.
node_trx * trx_list;
trx_list = calloc(1, sizeof(node_trx *));
trx_list->amount = "123123123";
trx_list->currency = 123;
trx_list->next_node = NULL;
if (1 != fwrite(trx_list, length, 1, f)) {
//error
}
free(trx_list);
Here is my struct:
typedef struct {
char amount;
int currency;
struct node_trx * next_node; } node_trx;
Main problem is after i saved struct to file and then after read, when print values, it is printing wrong values. For example: i stored currency as 123, then printed 6788576 this kind of numbers.
here is my reading code:
int read_last_trx_from_file (const char * file_name, node_trx * * trx, unsigned * trx_len)
{
FILE * f;
*trx = NULL;
if (NULL == (f = fopen(tools_get_full_filename_const(file_name), "rb")))
{
return 2; //error
}
size_t fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
fprintf(stdout, "file size: %zd\n", fsize);
if (!fsize)
{
fclose(f);
return 3; //no data
} else {
if (fsize == 1) {
return 3; // no data
}
}
rewind(f);
if (NULL != (*trx = (node_trx *) calloc(1, fsize)))
{
if (1 != fread(*trx, fsize, 1, f))
{
fclose(f);
free(*trx);
return 2; //error
}
}
fclose(f);
*trx_len = fsize;
return 0; //OK }
Main function that calls read function:
int display_trx() {
node_trx * card_data;
if (3 != read_last_trx_from_file(LAST_TRX_OBJECT, &card_data, &data_set_len)) {
if (card_data != NULL) {
printf("%s AMOUNT \n", card_data->amount);
printf("%d CURRENCY \n", &card_data->currency);
}
}
}
After i read this way , when i print amount data, segmentation fault error occurs. so why segment error occured?
And when i print currency, it printing 734364636 this kinda numbers. So why it prints wrong numbers.
Or i only wrote pointer of struct to file?
Please help me guys.
There are two obvious errors in your code.
In struct declaration, the type of amount is char, but when you initialize it in trx_list->amount = "123123123";, you assigned a string, or char[10] array (there is an extra one for NULL terminator).
In function display_trx, second printf, the result of &card_data->currency is int *, not int. If you want to print out currency, why don't follow the first print, use card_data->currency (without &)? You get that large number because you are printing pointer value implicitly converted into int, or the address of currency in card_data.
And there is one error which compiler will not warn you (because it is not syntactically wrong.). As BLUEPIXY said in the comments, when allocating and initializing trx_list, you should really use calloc(1, sizeof(node_trx)). You are allocating space for what pointer trx_list points to, not the pointer itself, so there should not be an asterisk in sizeof.
My suggestion is using a "smart" compiler, such as gcc, and enable warnings. This is a good practice (at least for me). Thank you!

C read in a mathematical equation from command line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Currently, I'm writing a program to calculate the Integral of an equation by using the Trapezoidal rule and a combination of these to archive higher precision. Right now as you can see below, I have hard-coded the function. Is it possible to read in a mathematical equation and evaluate it? I know I could read in the input character list and then evaluate the function (like if char[i] == '+' do ....) but is there an easier way?
Thank you in advance!
void Integral_TN (double* TN_ptr,double a,double b,int n,int my_rank,int p){
int i;
double a_loc;
double b_loc;
double hN;
*TN_ptr = 0;
hN = (b-a)/n;
a_loc = a + my_rank*n/p*hN;
b_loc = a + (my_rank+1)*n/p*hN;
*TN_ptr += (function(a_loc)+function(b_loc))/2; /*Evaluate f at the borders*/
for(i = 1; i < n/p; i++){
*TN_ptr += function(a_loc + i*hN); /*Evaluate f at the inner nodes*/
}
*TN_ptr = *TN_ptr*hN;
}
double function(double x){
double y;
y = 1/(1+x*x);
return y;
}
skrtbhtngr already answered the stated question, but I'd like to address the underlying problem.
Apply the Unix philosophy. Use one tool to generate the data, and another to consume it -- here, to compute the integral using the trapezoid rule.
The easiest format you can use is the same supported by Gnuplot:
Empty lines are ignored
Lines beginning with # are ignored, and can be used for comments
Each line defines one sample
Essentially, you could describe a sine curve, very roughly, using
#x sin(x)
0.000 0.000000000
0.100 0.099833417
0.200 0.198669331
0.300 0.295520207
0.400 0.389418342
0.500 0.479425539
0.600 0.564642473
0.700 0.644217687
0.800 0.717356091
0.900 0.783326910
1.000 0.841470985
1.100 0.891207360
1.200 0.932039086
1.300 0.963558185
1.400 0.985449730
1.500 0.997494987
1.600 0.999573603
1.700 0.991664810
1.800 0.973847631
1.900 0.946300088
2.000 0.909297427
2.100 0.863209367
2.200 0.808496404
2.300 0.745705212
2.400 0.675463181
2.500 0.598472144
2.600 0.515501372
2.700 0.427379880
2.800 0.334988150
2.900 0.239249329
3.000 0.141120008
3.100 0.041580662
3.200 -0.058374143
3.300 -0.157745694
3.400 -0.255541102
3.500 -0.350783228
3.600 -0.442520443
3.700 -0.529836141
3.800 -0.611857891
3.900 -0.687766159
4.000 -0.756802495
4.100 -0.818277111
4.200 -0.871575772
4.300 -0.916165937
4.400 -0.951602074
4.500 -0.977530118
4.600 -0.993691004
4.700 -0.999923258
4.800 -0.996164609
4.900 -0.982452613
5.000 -0.958924275
5.100 -0.925814682
5.200 -0.883454656
5.300 -0.832267442
5.400 -0.772764488
5.500 -0.705540326
5.600 -0.631266638
5.700 -0.550685543
5.800 -0.464602179
5.900 -0.373876665
6.000 -0.279415498
6.100 -0.182162504
6.200 -0.083089403
You could use eg. awk to generate that, like I did:
awk 'BEGIN { printf "#x sin(x)\n" ; for (x=0.0; x<6.3; x+=0.1) printf "%.3f %11.9f\n", x, sin(x) }'
If you save that to a file (appending > data.txt to the above command), you can plot it in Gnuplot using
plot "data.txt" using 1:2 notitle with lines
Such data is easy to read in a C program. Because I only use POSIX.1 systems (Linux, BSDs, macOS), and POSIX.1 provides the very useful getline() function -- it lets you read in lines of any length, dynamically allocating a large enough buffer --, this particular implementation also requires POSIX.1 support. In other words, it works basically everywhere except in Windows.
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
/* Read x y(x) values, one pair per line, from a stream.
The arrays are dynamically allocated, and pointers stored
to *xptr and *yptr. The size of the arrays is stored at *nptr.
They are initially cleared to NULL/zero.
The function returns 0 if success, an errno error code otherwise:
EINVAL: Invalid function parameters
EIO: Read error
ENOMEM: Out of memory
EBADMSG: Malformed line
*/
int read_xy(double **xptr, double **yptr, size_t *nptr, FILE *in)
{
/* Line input buffer variables. */
char *line = NULL;
size_t size = 0;
ssize_t len;
/* Data array variables. */
double *x = NULL;
double *y = NULL;
size_t n = 0; /* Entries in x[] and y[] */
size_t nmax = 0; /* Entries allocated */
/* Temporary variables. */
double xval, yval, *newx, *newy;
/* We clear the output parameters to NULL or zero,
in case the caller is careless and does not check
the return value. Clearing them ensures they do
not contain garbage in such a case. */
if (xptr)
*xptr = NULL;
if (yptr)
*yptr = NULL;
if (nptr)
*nptr = 0;
/* We need in and nptr, and at least one of xptr and yptr. */
if (!in || !nptr || (!xptr && !yptr))
return errno = EINVAL;
/* If an error has already occurred in 'in',
we do not even try to read from it. */
if (ferror(in))
return EIO;
while (1) {
/* Read next input line. */
len = getline(&line, &size, in);
/* End of input or error? */
if (len < 0)
break;
/* Skip empty and comment lines. */
if (len == 0 ||
line[0] == '\n' || (line[0] == '\r' && line[1] == '\n') ||
line[0] == '#')
continue;
/* Parse the line. */
if (sscanf(line, " %lf %lf", &xval, &yval) != 2)
break;
/* Need to grow the dynamically allocated arrays? */
if (n >= nmax) {
/* Allocation policy.
We allocate room for at least 16 doubles,
then double the size up to 1048576 (=2^20),
then adjust to the next full multiple of 1048576.
This is not 'the best', but it is robust,
and not too wasteful.
*/
if (n < 16)
nmax = 16;
else
if (n < 1048576)
nmax = n * 2;
else
nmax = (n | 1048575) + 1048576;
/* Note: realloc(NULL, size) is equivalent to malloc(size).
If the realloc() call fails, it returns NULL,
but the original array is still valid.
Also note that free(NULL) is safe, and does nothing.
*/
newx = realloc(x, nmax * sizeof x[0]);
newy = realloc(y, nmax * sizeof y[0]);
if (newx)
x = newx;
if (newy)
y = newy;
if (!newx || !newy) {
/* One or both of the allocations failed. */
free(line);
free(x);
free(y);
return ENOMEM;
}
}
/* Save the parsed values to the arrays. */
x[n] = xval;
y[n] = yval;
n++;
}
/* We no longer need the line buffer. */
free(line);
/* Did a read error occur? */
if (ferror(in)) {
free(x);
free(y);
return EIO;
}
/* Was there no data to read? */
if (n < 1) {
free(x);
free(y);
return 0;
}
/* Reallocate the arrays to their exact sizes
(actually, allow for one extra double at the end,
because it is often useful to copy the initial
ones there if the data is considered cyclic).
*/
nmax = n + 1; /* One extra just because it is so often useful. */
newx = realloc(x, nmax * sizeof x[0]);
newy = realloc(y, nmax * sizeof y[0]);
if (newx)
x = newx;
if (newy)
y = newy;
if (!newx || !newy) {
free(x);
free(y);
return ENOMEM;
}
/* Save the array pointers. */
if (xptr)
*xptr = x;
else
free(x);
if (yptr)
*yptr = y;
else
free(y);
/* Save the number of samples read. */
*nptr = n;
/* If feof(in) is true, then we read everything
up to end of input. Otherwise, we stopped at
a line we could not parse.
*/
if (!feof(in))
return EBADMSG;
return 0;
}
That function, or something like it, should be in the course materials for every numerical computation course. They are just so darn useful. This particular one has no inherent limits to the size of data it can read, other than possible memory allocation limits set by the system administrator for each process. I know for a fact that it happily reads billions of lines of data, successfully, if you just have enough RAM.
Using the function is very simple. Here is an example main() that just reads such data from standard input -- remember that you can make it read from a file by appending < file to the command when you run it --, and prints the data out.
int main(void)
{
double *x, *y;
size_t i, n;
int result;
result = read_xy(&x, &y, &n, stdin);
switch (result) {
case 0: /* No errors */
break;
case EBADMSG:
if (n > 1)
fprintf(stderr, "Invalid line after %zu data samples.\n", n);
else
fprintf(stderr, "Cannot parse first input line.\n");
return EXIT_FAILURE;
case ENOMEM:
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
case EIO:
fprintf(stderr, "Read error.\n");
return EXIT_FAILURE;
case EINVAL:
fprintf(stderr, "Invalid parameters to the read_xy() function!\n");
return EXIT_FAILURE;
default:
fprintf(stderr, "%s.\n", strerror(result));
return EXIT_FAILURE;
}
printf("Read %zu samples:\n", n);
for (i = 0; i < n; i++)
printf("%.9f %.9f\n", x[i], y[i]);
return EXIT_SUCCESS;
}
Note that most of it is the switch (result) { .. } error-reporting code. This example is very careful to tell you, if an error occurs; the reason is, you, as the user, need to know when the program knows they might spew garbage, and will in real life prefer for the programs to abort than spew that garbage silently -- perhaps making you believe it is valid.
While it is possible to modify the above code to work even on Windows (you replace the getline() with fgets(), for example, and hope that the buffer size you use for it will suffice; and also may have to change some of the errno error codes). However, there is a reason there are no Windows machines in the Top 500 supercomputer list: POSIXy systems (Unix and Linux) are just better suited for scientific computing. So, if you do intend to work on some scientific computing, you might as well just set up a Linux or BSD virtual machine, and do your development there.
There is no easier way for what you wish to achieve. If you want to apply a specific formula to some values, you have to define a function for it and input the values.
If you wish to type in the whole expression (with values and operators) as input and get the desired result as output, you would have to go beyond basic programming. You would need to create a parser.
For instance, if you provide 3+2*4 as input, you might expect 11 as output without reading in separate values 3, 2 and 4. This can be made possible by implementing a custom parser in a parser-generator like YACC. Basically, you would create and define new rules on how the input should be interpreted.

Reading From Stdin Twice in C

int getLineCount() {
int ret = 0;
char c;
while ((c = fgetc(stdin)) != EOF)
if (c == '\n')
ret++;
return ret + 1;
}
void fill(char *WORD) {
int charIndex = 0;
char c;
while ((c = fgetc(stdin)) != EOF) {
*(WORD + charIndex++) = c;
}
}
int main() {
int lineNum = getLineCount();
char *WORD = (char*)calloc(lineNum * 18,sizeof(int));
fill(WORD);
return 0;
}
Here is the part of my code, and my question is(as you can see):
I'm trying to read stdin's content twice, but after the getLineCount function, it stays at the EOF and I can't read it again in fill function.
Im taking stdin from the user with this command in Linux;
$./output < text_file.txt
Is there any way to roll back stdin to starting character? If not, how can I fix this problem?
Thanks.
You can use rewind(stdin) to set the stream back to the start of file, but be aware that it is not guaranteed to work, especially if the stream is a pipe, a terminal or a device.
Your allocation scheme is incorrect: you could compute the size of the file and then allocate that many bytes, but your current (char*)calloc(lineNum * 18,sizeof(int)); allocates 18 times the size of type int for each line. Some files with short lines will fit in this array while others will invoke undefined behavior.
Note that c must be defined as int for c = fgetc(stdin); to properly store all values including the EOF special value.
Don't use rewind.
You can, of course, save the data you read from stdin (potentially in a file if it's too large for main memory) and operate on that.
Another possibility is this:
struct callback {
void (*call) (char, void *);
void * data;
};
void with_characters_from(FILE * file, struct callback const * callbacks, size_t count) {
int c;
while ((c = fgetc(file)) != EOF) {
char character = c & 0xFF;
for (size_t i = 0; i < count; ++i) {
callbacks[i].call(character, callbacks[i].data);
}
}
}
You inverse control, such that no longer your functions are "pulling data out of" stdin, but rather the data (characters) are "pushed to" them. Note that this can lead to callback hell, and in C you sacrifice a good portion of type safety (as well as code clarity .. no first class functions / closures ... sigh).
A small test:
struct counter_data {
char const character;
unsigned count;
};
void counter (char character, void * vptr) {
struct counter_data * data = vptr;
if (character == data->character) {
++(data->count);
}
}
int main() {
struct counter_data data [2] = {
{'a', 0}, {'x', 0}};
struct callback callbacks [2] = {
{&counter, &(data [0])},
{&counter, &(data [1])}};
with_characters_from (stdin, callbacks, 2);
printf("Counted %c %u times \n", data [0].character, data [0].count);
printf("Counted %c %u times \n", data [1].character, data [1].count);
return 0;
}
As already noted, for your particular example, you should consider a completely different approach: If possible compute the required size beforehand. If you exceed that size (which you should always test for), then use realloc in order to get a larger chunk of memory.

C program crashes when trying to refer to struct array

I'm trying to make a simple database program, but when I get to this line
int idSearch(product* itens, int id) {
int i = 0;
for(i; i < size; i++) {
if(itens[i].id == id) //<=== This line
return i;
}
return ITEM_NOT_FOUND;
}
the program stops responding.
size is set as a global variable in the begining of the program
FILE* flog;
FILE* db;
FILE* sizef;
int size = 100;
this function is called by
void newProduct(product* itens, char name[64], int id, float price) {
int pos = idSearch(itens, 0);
if(idSearch(itens, id) != ITEM_NOT_FOUND) {
printf("Erro, o produto ja existe");
return;
}...
items is defined as
itens = (product*) calloc(sizeof(product), size);
and product is a struct defined as such
typedef struct{
char name[64];
int id;
float price;
int amount;
} product;
Firstly I thought the problem was that I was not using the -> operator, but when I tried the compiler says its not right.
I'm using Code::Blocks with GCC compiler on a Windows 7 x64
**EDIT: the whole code can be found here: http://hastebin.com/atulajubar.tex
Hope to hear answers soon, Thanks in advance
**EDIT: You're calling calloc() wrong. The signature is: void *calloc(size_t nelem, size_t elsize);
You're giving it the size first, then the number of elements. Switch that around and see if your problem is resolved.
Also, when calling (AFTER THE FIX:) itens = (product*) calloc( size, sizeof(product) );,
it's important to check to see that itens is not NULL after doing this. If calloc isn't able to give you back the right amount of memory, it returns a NULL pointer I believe. Check this, because if you're getting NULL back, that's your issue.
One good, easy, portable way of checking that would be:
if(!itens){
fprintf(stderr, "Error! Could not allocate memory!\n");
exit(1);
}
Also, as WhozCraig suggested, please make sure your code contains #include <stdlib.h>, a requirement of calloc() as per its man page.
This is wrong:
if((db = fopen(DB_PATH, RB))==NULL)
{
if((db = fopen(DB_PATH, RWB))==NULL)
{
exit(1);
}
else
{
itens = (product*) calloc(sizeof(product), size);
fwrite(itens, sizeof(product), size, db);
rewind(db);
}
}
fread(itens, sizeof(product), size, db);
If you have a DB_PATH file in the current working directory the first fopen() will succeed and the items allocation will never take place. Only if the file is not found, but is then successfully created will items contain a valid allocation, assuming calloc worked.
That else condition should be removed:
// note calloc parameter order addressed.
itens = calloc(size, sizeof(product));
if (itens == NULL)
{
perror("Failed to allocate items.");
exit(EXIT_FAILURE);
}
if((db = fopen(DB_PATH, RB))==NULL)
{
if((db = fopen(DB_PATH, RWB))==NULL)
exit(EXIT_FAILURE);
fwrite(itens, sizeof(product), size, db);
rewind(db);
}
fread(itens, sizeof(product), size, db);
There is a significant amount of error checking left to handle, but this needs to be addressed regardless.

Resources